mybatis-查询(resultMap,关联集合)-15
来源:互联网 发布:苏州软件学院 编辑:程序博客网 时间:2024/06/05 03:19
场景:查询部门下的所有员工
第一种方式:嵌套结果集方式
第二种方式:分步查询方式
第一种方式:嵌套结果集方式
javaBean
public class Department { private Integer id; private String name; private List<Employee> employees;}
接口
public Department getDepartmentByIdPlus(Integer id);
sql映射文件
<!-- private Integer id; private String name; private List<Employee> employees; --> <!-- 嵌套结果集的方式--> <!--public Department getDepartmentByIdPlus(Integer id);--> <resultMap id="myDept" type="com.stayreal.mybatis.Department"> <id column="did" property="id"/> <result column="dept_name" property="name"/> <!-- collection定义关联集合类型的属性封装规则 offType:指定集合中的元素类型 --> <collection property="employees" ofType="com.stayreal.mybatis.Employee"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <select id="getDepartmentByIdPlus" resultMap="myDept"> select d.id did,d.dept_name dept_name,e.id eid,e.last_name last_name, e.email email,e.gender gender from tbl_dept d left JOIN tbl_employee e on d.id = e.d_id where d.id = #{id} </select>
junit
DepartmentMapper mapper = session.getMapper(DepartmentMapper.class); Department dept = mapper.getDepartmentByIdPlus(2);// 分步查询 System.out.println(dept.toString()); System.out.println(dept.getEmployees());// Department{id=2, name='ceshi'}[Employee{id=1, //lastName='Jerry', email='Jerry@qq.com', gender='1'}, //Employee{id=3, lastName='Jerry', email='Jerry@qq.com', //gender='1'}]
第二种方式:分步查询方式
public Department getDepartmentByIdStep(Integer id);
<!--public Department getDepartmentByIdStep(Integer id);--> <resultMap id="myDeptStep" type="com.stayreal.mybatis.Department"> <id column="id" property="id"/> <result column="dept_name" property="name"/> <collection property="employees" select="com.stayreal.mybatis.EmployeeMapperPlus.getEmpsByDeptId" column="id"> </collection> </resultMap> <select id="getDepartmentByIdStep" resultMap="myDeptStep"> select id,dept_name name from tbl_dept where id = #{id} </select>
Department dept = mapper.getDepartmentByIdStep(2);// 分步查询 collection//Department{id=2, name='ceshi'}//[Employee{id=1, lastName='null', email='Jerry@qq.com', gender='1'}, Employee{id=3, lastName='null', email='Jerry@qq.com', gender='1'}]
阅读全文
0 0
- mybatis-查询(resultMap,关联集合)-15
- mybatis-查询(resultMap,关联单个对象)-14
- MyBatis中的关联查询 之resultMap
- mybatis 关联查询 resultMap 同名冲突
- mybatis之resultMap的关联查询
- mybatis关联查询的的resultMap 用法
- Mybatis关联、集合查询
- Mybatis-04-结果集映射resultMap/动态SQL/关联查询
- mybatis中resultMap的使用 (关联和映射)
- mybatis学习之多表关联查询和resultmap的常用标签和属性 和分页查询
- mybatis 关联查询 Collection定义关联集合封装规则
- mybatis中resultMap简单查询
- MyBatis关联查询,表字段相同,resultMap映射问题的解决办法
- MyBatis关联查询,表字段相同,resultMap映射问题的解决办法
- MyBatis关联查询,表字段相同,resultMap映射问题的解决办法
- mybatis进阶(3)--一对一查询(使用resultMap)
- MyBatis之ResultMap简介,关联对象
- 【Mybatis】表关联和resultMap的使用
- React Native 应用在键盘弹出时优雅地响应
- Android Studio目录结构和Gradle构建Android项目
- android selector的item属性大全
- 跳出常规语言,采用舞步学院非逻辑思维聊天体系
- 手把手用Git将项目上传到GitHub上(二)
- mybatis-查询(resultMap,关联集合)-15
- linux 下 /bin /sbin 的区别 和一些主目录的作用
- hive mapjoin使用
- 解决IIS部署网站引用woff/woff2/svg字体报404错误
- 【算法】Learning to rank小结
- 慎用DelphiXE的TTask.WaitForAll/WaitForAny 一不小心会造成内存泄露!
- 一次MMO手游的性能诊断全纪录
- 几种异地数据备份软件/系统方案
- Sublime Text 3 配置 PHP IDE环境 并使用Xdebug进行调试