_分步查询传递多列值&fetchType_discriminator鉴别器

笔记要点
出错分析与总结

Department.java  bean
public class Department {
private Integer id;
private String departmentName;
private List<Employee> emps; }

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="{deptId=id}" fetchType="lazy">
</collection>
</resultMap> <select id="getDeptByIdStep" resultMap="MyDeptStep">
select id,dept_name departmentName from tbl_dept
where id=#{id}
</select>
<!--=======================================-->
<!--扩展,将多列的值传递过去,封装成map进行传递!
column="{key1=列1,key2=列2}"
fetchType="lazy" ,标识使用延迟加载;
-lazy: 延迟;
-eager:立即;
--> </mapper>

测试

public class test_tp36 {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test08() throws Exception {
SqlSession openSession = getSqlSessionFactory().openSession();
try {
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
System.out.println("---tp_37-使用鉴别器-");
Employee emp = mapper.getEmpByIdStep(1); //测试为女生 ,gender=0
System.out.println(emp); emp = mapper.getEmpByIdStep(4); //测试为男生,gender=1
System.out.println(emp); openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交
} finally {
openSession.close();
}
}
}

测试结果:

---tp_37-使用鉴别器-
DEBUG 12-04 13:10:50,471 ==> Preparing: select id,last_name,gender,email,d_id from tbl_employee where id = ? (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,488 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,498 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry', gender='1', dept=null}
DEBUG 12-04 13:10:50,499 ==> Preparing: select id,last_name,gender,email,d_id from tbl_employee where id = ? (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,499 ==> Parameters: 4(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,558 <== Total: 1 (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,558 ==> Preparing: select id,dept_name departmentName from tbl_dept where id=? (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,558 ==> Parameters: 2(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-04 13:10:50,561 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=4, lastName='葫芦娃', email='葫芦娃@163.com', gender='0', dept=Department{id=2, departmentName='测试部'}}

Mybatis3.1-[tp_36-37]-_映射文件_select_resultMap关联查询__分步查询传递多列值&fetchType_discriminator鉴别器的更多相关文章

  1. Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载

    笔记要点出错分析与总结工程组织 1.定义接口 interface DepartmentMapper package com.dao; import com.bean.Department; publi ...

  2. Mybatis3.1-[tp_32-33]-_映射文件_select_resultMap关联查询_association分步查询_延迟加载

    笔记要点出错分析与总结 工程组织 1.定义接口 DepartmentMapper package com.dao; import com.bean.Department; public interfa ...

  3. mybatis映射文件select_resultMap_关联查询_collection定义关联集合

    知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee {    pr ...

  4. mybatis3.1-[topic-18-20]-_映射文件_参数处理_单个参数&多个参数&命名参数 _POJO&Map&TO 三种方式及举例

    笔记要点出错分析与总结 /**MyBatis_映射文件_参数处理_单个参数&多个参数&命名参数 * _POJO&Map&TO 三种方式及举例 _ * 单个参数 : #{ ...

  5. mybatis映射文件_select_resultMap

    实体类: Employee.java类: package com.hand.mybatis.bean; public class Employee {        private Integer e ...

  6. MyBatis3_[tp-26-27]_映射文件_select_返回List_记录封装Map:返回单个元素的Map或者整体Map集合

    笔记要点出错分析与总结工程组织 1.定义接口 public interface EmployeeMapper { //多条记录封装到一个map中: Map<Integer,Employee> ...

  7. MyBatis 3.0_[tp-24-25]_映射文件_参数处理_#与$取值区别_#{}更丰富的用法

    笔记要点出错分析与总结 /**================Mybatis参数值的获取:#和$符号的区别=============== * #{}:可以获得map中的值或者pojo对象属性的值; * ...

  8. Mybatis XML映射文件

    mybatis为聚焦于SQL而构建,SQL映射文件常用的顶级元素如 resultMap,是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象. insert,映射插入语句 update, ...

  9. Hibernate的映射文件

    映射文件的结构和属性 一个映射文件(mapping file)由一个根节点<hibernate-mapping>和多个<class>节点组成, 首先看看根节点<hiber ...

随机推荐

  1. Web调试利器fiddler(转)

    http://blog.chinaunix.net/uid-27105712-id-3738821.html

  2. iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合

    通过对前面两偏线程理解的总结,自己对线程的理解也逐渐加深,梳理的清晰起来…… 通常在使用线程 的时候,都是要用到 执行对列,执行方式,执行任务, 现在开始新一轮的深入 3. 1. 1  同步 + 串行 ...

  3. 个人博客搭建全记录(Hexo,Github)

    搭建过程主要借鉴小歪的博客 博客主题airclod Hexo,Github建站记录 1. 准备 Github账号 注册登陆Github 创建Repository,Repository Name就是Yo ...

  4. 学习开始记录一下,java 还是python?

    2019.11.24开始正式开始学习JAVA. 在 bilibili站看了三天,大神们的对此问题的分析,介绍,我选择了JAVA开发语言. 在看了高淇老师的JAVA300视频,感觉比较对路,特别是第一章 ...

  5. 从零开始学Flask框架-001

    新建项目 目录结构 Index.html <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  6. Python习题001

    作业1 * 用条件语句写一个BMI(体重除以身高的平方)指数* 低于18.5:过轻* 18.5 - 25 正常* 25 - 28 过重* 28 - 32 肥胖* 高于32 严重肥胖 weight = ...

  7. Android—网络请求

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; ...

  8. AS3.0 位图(BMP)解析类

    /** * *-----------------------------* * | *** BMP格式解析类 *** | * *-----------------------------* * * 编 ...

  9. iview前台端口设置,跨域端口设置

    前台启动默认端口: 跨域端口: 完毕

  10. GoLand中同一个目录下的package无法调用

    代码结构: 三个代码的package 都是 pipefilter,执行split_filter_test.go 就会提示   undefined:xxxxxxx Golang实际都可以自己补全另一个文 ...