Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载
笔记要点
出错分析与总结
工程组织
1.定义接口
- interface DepartmentMapper
- package com.dao;
- import com.bean.Department;
- public interface DepartmentMapper {
- public Department getDeptByIdStep(Integer id); //使用Collection,执行分步查询
- }
- interface EmployeeMapperPlus
- package com.dao;
- import com.bean.*;
- import java.util.List;
- public interface EmployeeMapperPlus {
- public List<Employee> getEmpsByDeptId(Integer deptId); //按照部门的id,返回employee一个列表
- }
2.定义XML映射文件
- DepartmentMapper.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.dao.DepartmentMapper">
- <!--public Department getDeptById(Integer id);-->
- <select id="getDeptById" resultType="com.bean.Department">
- select id,dept_name departmentName from tbl_dept
- where id=#{id}
- </select>
- <!--==========================================================================================-->
- <!--
- public class Department {
- private Integer id;
- private String departmentName;
- private List<Employee> emps;
- }
- JavaBean中: did dept_name || eid last_name email gender
- -->
- <!--public Department getDeptByIdPlus(Integer id);-->
- <!--进行collection的联合查询/ 分步查询和延迟查询 -->
- <resultMap id="MyDept" type="com.bean.Department">
- <id column="did" property="id"/>
- <result column="dept_name" property="departmentName"/>
- <!--collection 用于定义关联集合类型的属性的封装规则!
- ofType用于指定集合中的类型;-->
- <collection property="emps" ofType="com.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>
- <select id="getDeptByIdPlus" resultMap="MyDept">
- SELECT d.id did,d.dept_name dept_name,e.id eid,e.last_name last_name,e.email email,gender
- FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id
- WHERE d.id=#{id};
- </select>
- <!--========================================-->
- <!--Department中有属性: private List<Employee> emps;
- public Department getDeptByIdStep(Integer id); //执行Collection 的分步查询-->
- <resultMap id="MyDeptStep" type="com.bean.Department">
- <id column="id" property="id"/>
- <result column="detp_name" property="departmentName"/>
- <collection property="emps" select="com.dao.EmployeeMapperPlus.getEmpsByDeptId"
- column="id">
- </collection>
- </resultMap>
- <select id="getDeptByIdStep" resultMap="MyDeptStep">
- select id,dept_name departmentName from tbl_dept
- where id=#{id}
- </select>
- </mapper>
EmployeeMapperPlus.xml c新增内容
- <select id="getEmpsByDeptId" resultType="com.bean.Employee">
- select * from tbl_employee where d_id = #{DeptId}
- </select>
3.编写测试代码
- @Test
- public void test07() throws Exception{
- SqlSession openSession = getSqlSessionFactory().openSession();
- try{
- DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
- // System.out.println("---tp_34---多表关联查询,使用collection 定义关联集合封装规则-----");
- // Department department = mapper.getDeptByIdPlus(1);
- // List<Employee> emps = department.getEmps();
- // for(Employee e:emps)
- // System.out.println(e);
- System.out.println("---tp_35---多表关联查询,使用collection分步查询&延迟加载--");
- Department dept = mapper.getDeptByIdStep(1);
- System.out.println(dept.getDepartmentName()); //只有一行时,进行按需加载
- // System.out.println(dept); //当要加载全部的时候,就不会延迟加载了
- openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交
- }finally {
- openSession.close();
- }
- }
测试结果 (全部调用时的结果)
- ---tp_35---多表关联查询,使用collection分步查询&延迟加载--
- DEBUG 12-04 12:01:58,977 ==> Preparing: select id,dept_name departmentName from tbl_dept where id=? (BaseJdbcLogger.java:145)
- DEBUG 12-04 12:01:58,997 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
- DEBUG 12-04 12:01:59,069 <== Total: 1 (BaseJdbcLogger.java:145)
- 开发部
- DEBUG 12-04 12:01:59,069 ==> Preparing: select * from tbl_employee where d_id = ? (BaseJdbcLogger.java:145)
- DEBUG 12-04 12:01:59,070 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
- DEBUG 12-04 12:01:59,072 <== Total: 2 (BaseJdbcLogger.java:145)
- Department{id=1, departmentName='开发部'}
Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载的更多相关文章
- Mybatis3.1-[tp_32-33]-_映射文件_select_resultMap关联查询_association分步查询_延迟加载
笔记要点出错分析与总结 工程组织 1.定义接口 DepartmentMapper package com.dao; import com.bean.Department; public interfa ...
- Mybatis3.1-[tp_36-37]-_映射文件_select_resultMap关联查询__分步查询传递多列值&fetchType_discriminator鉴别器
_分步查询传递多列值&fetchType_discriminator鉴别器 笔记要点出错分析与总结 Department.java bean public class Department { ...
- mybatis3.1-[topic-18-20]-_映射文件_参数处理_单个参数&多个参数&命名参数 _POJO&Map&TO 三种方式及举例
笔记要点出错分析与总结 /**MyBatis_映射文件_参数处理_单个参数&多个参数&命名参数 * _POJO&Map&TO 三种方式及举例 _ * 单个参数 : #{ ...
- mybatis映射文件_select_resultMap
实体类: Employee.java类: package com.hand.mybatis.bean; public class Employee { private Integer e ...
- MyBatis3_[tp-26-27]_映射文件_select_返回List_记录封装Map:返回单个元素的Map或者整体Map集合
笔记要点出错分析与总结工程组织 1.定义接口 public interface EmployeeMapper { //多条记录封装到一个map中: Map<Integer,Employee> ...
- MyBatis 3.0_[tp-24-25]_映射文件_参数处理_#与$取值区别_#{}更丰富的用法
笔记要点出错分析与总结 /**================Mybatis参数值的获取:#和$符号的区别=============== * #{}:可以获得map中的值或者pojo对象属性的值; * ...
- MyBatis映射文件的resultMap如何做表关联
MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射 ...
- mybatis映射文件select_resultMap_关联查询_collection定义关联集合
知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee { pr ...
- 5.20 mybatis反向生成的映射文件xml(如果需要自己定义其他sql语句时如下)
解决mybatis-generator 生成的mapper.xml覆盖自定义sql的问题 mybatis-generator是个好工具,一建即可生成基本增删改成功能的mapper.xml.但这些是不够 ...
随机推荐
- TS - 问题解决力 - 上篇
本文是已读书籍的内容摘要,少部分有轻微改动,但不影响原文表达. <麦肯锡工作法 - 个人竞争力提升50%的7堂课> ISBN: 9787508644691 https://book.dou ...
- IOPS 测试工具 FIO
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎. fio-2.8下载: wget http://brick.kernel.dk/snaps/fio-2.8 ...
- laravel 提交空字符串会被转成null解决方法
在app\Http\Kernel.php文件夹中,注释全局中间件: \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull:: ...
- Rsync同步部署web服务端配置
Rsync同步部署web服务端配置 1,参数详解: -v, --verbose 详细模式输出. -q, --quiet 精简输出模式. -c, --checksum 打开校验开关,强制对文件传输进行校 ...
- JAVA 后台面试之操作系统问题集锦
1.进程和线程以及他们的区别 2.进程间通信的几种方式 3.线程同步的方式 4.死锁 5.分页和分段有什么区别?(内存管理) 6.操作系统中进程调度的策略有哪几种? 7.页面置换算法: 8.系统颠簸 ...
- java war包 远程debug出现的问题解决,学会查看日志
开启远程debug之后,8005 关闭tomcat 又启动不了了.. netstat -lnp 未发现8005接口 eclipse 内远程链接到服务器,debug 下发现服务器线程启动也存在问题.很多 ...
- python基础学习(九)
19.解包 # 解包 unpacking user1 = ["张三", 21, "1999.1.1"] # tuple 类型 user2 = ("李四 ...
- (零)引言——关于effective Java 3th
去年4月份那时候,读过本书的第二版本,那时候寻思着好好读完,但是事与愿违,没有读完! 现在起,寻思着再次开始读吧: 现在第三版也出版了,还有第二版的翻译问题,遂决定读第三版的英文版吧: PDF版本可以 ...
- Word 固定行间距公式图片显示不全、Word Eculid 字体导致行间距过大、Word 行间距过大
1. 前言 1.有些文章行间距要求是固定值,比如,固定值15磅,但是这样会导致有些公式.图片显示不全.例如下图: 2.Euclid这个字体很容易导致行间距超大. 2. 解决方案 1.把固定值15磅改为 ...
- python 之 前端开发(盒子模型、页面布局、浮动、定位、z-index、overflow溢出)
11.312 盒子模型 HTML文档中的每个元素都被比喻成矩形盒子, 盒子模型通过四个边界来描述:margin(外边距),border(边框),padding(内填充),content(内容区域),如 ...