_分步查询传递多列值&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. PCA分析的疑问

    R 与python scikit-learn PCA的主成分结果有部分是反的 通过R和python分别计算出来的PCA的结果存在某些主成分的结果是相反的,这些结果是没有问题的,只是表示这个分量被反转了 ...

  2. [转帖]PCIe 6.0 v0.3版本草案已完稿:2021年转正、x16带宽飙至128GB/s

    PCIe 6.0 v0.3版本草案已完稿:2021年转正.x16带宽飙至128GB/s https://www.cnbeta.com/articles/tech/899389.htm 硬件发展突飞猛进 ...

  3. supervisor+daphne+djangochannels

    参照官网配置:https://channels.readthedocs.io/en/latest/deploying.html 1.supervisor 主要是用来管理进程,比如我们想让一个进程一直执 ...

  4. 5-24 c++语言之【基础知识】

    最近一段时间继续开始了c++的学习,作为c plus plus 难免会与c语言做一个对比,很明显的感受到c++语言注重代码的复用性和拓展性,而c语言更加注重其算法的高效性,这也是今后需要注意的地方,避 ...

  5. hadoop 空间配置

    hadoop-------------- 分布式计算框架. common // hdfs //存储 mapreduce //MR,编程模型. yarn //资源调度. 集群部署----------- ...

  6. Oracle通过ODBC链接SqlServer数据库

    原网址:https://www.cnblogs.com/jijm123/p/11598515.html 第一步.创建ODBC数据源 这一步要考虑数据源是32位还是64位的问题,其实就是选择不同的exe ...

  7. AS3动画效果常用公式

    缓动公式: sprite.x += (targetX – sprite.x) * easing;//easing为缓动系数变量 sprite.y += (targetY – sprite.y) * e ...

  8. pyrhon 第一个小购物车例子

    product_list=[[],[],[],[]] shopping_list=[] salary = input("请输入你的工资:") if salary.isdigit() ...

  9. SSL 杂谈

    在电子邮件系统中的开发和维护中,由于安全性的需要,经常会遇到 SSL 相关的问题,这里整理下 SSL 的一些基础知识 什么是 SSL SSL (Secure Sockets Layer) 是一种在应用 ...

  10. VS.NET(C#)--1.4项目与解决方案

    项目与解决方案 项目 除创建网站,VS2005可创建项目.然后把项目放入解决方案中.VS2005可编译很多类型项目,分别是: 1.Windows应用程序 --在用戶计算机上运行的客户端应用程序,可显示 ...