MyBatis(十一) 嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则

(1)接口中编写方法

public Dept getDeptPlusById(Integer id);

(2)Mapper文件

 1   <resultMap type="com.eu.bean.Dept" id="MyPlus">
 2         <id column="id" property="id"/>
 3         <result column="dept_name" property="deptName"/>
 4         <collection property="emps" ofType="com.eu.bean.Emp">
 5             <id column="id" property="id"/>
 6             <result column="last_name" property="lastName"/>
 7             <result column="gender" property="geder"/>
 8         </collection>
 9     </resultMap>
10     
11     <!-- public Dept getDeptPlusById(Integer id); -->
12     <select id="getDeptPlusById" resultType="MyPlus">
13         SELECT *FROM dept d
14         JOIN emp e
15         ON d.id=e.d_id
16         WHERE d.id=#{id}
17     </select>

欢迎添加本人微信,带你加入Java学习交流群,还有学习资料等你获取。

原文地址:https://www.cnblogs.com/wanerhu/p/10747386.html