介绍resultMap中使用collection查询集合属性

业务需求,查询部门中的多个人员

public class Department {

private Integer id;
private String departmentName;
private List<Employee> emps;

setter和getter省略

}

public class Employee {

private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;

setter和getter省略

}

1。在department接口写方法

public Department getDeptByIdPlus(Integer id);

2.找到映射文件写

<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
<id column="did" property="id"/>
<result column="dname" property="departmentName"/>
<!--
collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
-->

<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
<!-- 定义这个集合中元素的封装规则 -->
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<!-- public Department getDeptByIdPlus(Integer id); -->
<select id="getDeptByIdPlus" resultMap="MyDept">
SELECT d.id did,d.dname dname,
e.id eid,e.last_name last_name,e.email email,e.gender gender
FROM department d
LEFT JOIN tbl_employee e
ON d.id=e.did
WHERE d.id=#{id}
</select>

3进行测试

@Test
public void test06() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();

try{
DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
Department department = mapper.getDeptByIdPlus(1);
System.out.println(department);
System.out.println(department.getEmps());

}finally{
openSession.close();
}
}

}

运行结果如下:

最新文章

  1. 路由集合中已存在名为“ XXXX” 的路由
  2. VC编程小技巧之框架窗口及其他
  3. angular中的代码执行顺序和$scope.$digest();
  4. jQuery 实现菜单
  5. mongodb存储过程
  6. 【.NET框架】Dapper ORM 用法—Net下无敌的ORM
  7. onclick事件分析
  8. C#单链表(数据结构)
  9. CodeForces 711A Bus to Udayland (水题)
  10. luoguP2266 爱的距离
  11. 记一次npapi插件无窗口(windowless )化下的妙巧思路然后解决问题的超爽体验过程
  12. poj 01背包
  13. 锐动视频SDK在金融业务加密双录管理系统通用解决方案
  14. Android面试之高级篇
  15. 5、AngularJS 直接绑定显示html ($sce、$sanitize服务)
  16. CentOS7搭建Zookeeper环境
  17. 【LOJ 2145】「SHOI2017」分手是祝愿
  18. JAVA图书管理系统汇总共27个
  19. 传统企业IT为什么对微服务叶公好龙的心态?(转)
  20. 逻辑卷(lv)管理(LVM)

热门文章

  1. Hadoop基础概念
  2. jsp+servlet实现文件上传下载
  3. EL作用域对象
  4. 纯CSS绘制3D立方体
  5. ArrayList实现原理分析
  6. 2019-11-29-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199
  7. matlab 中figure的图像 抗锯齿
  8. linux误卸载openssl后的恢复
  9. Vue文件路径引入
  10. 树莓派开机自动启动Chomium浏览器并打开指定网页