MyBatis resultType 的使用


          <resultMap type="Teachers" id="testResultMap">
              <id column="id" property="ids"/>
              <result column="name" property="names"/>
          </resultMap>
          <select id="testResultMap" resultMap="testResultMap" >
              select * from teacher
          </select>
public class Teachers {
    private int ids;
    private String names;

使用<resultMap>查詢關聯集合對象(N+1)

1. 在Teacher 中添加List<Student>

public class Teacher {
private int id;
private String name;
private List<Student> list;

}

2. 在StudentMapper.xml 中添加通過tid 查詢

<select id="selByTid" parameterType="int"
resultType="student">
select * from student where tid=#{0}
</select>

3. 在TeacherMapper.xml 中添加查詢全部

<resultMap type="teacher" id="mymap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="list"
select="com.bjsxt.mapper.StudentMapper.selByTid"
column="id"></collection>
</resultMap>
<select id="selAll" resultMap="mymap">
select * from teacher
</select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM