Mybatis3.1-[tp_36-37]-_映射文件_select_resultMap关联查询__分步查询传递多列值&fetchType_discriminator鉴别器
_分步查询传递多列值&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鉴别器的更多相关文章
- Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载
笔记要点出错分析与总结工程组织 1.定义接口 interface DepartmentMapper package com.dao; import com.bean.Department; publi ...
- Mybatis3.1-[tp_32-33]-_映射文件_select_resultMap关联查询_association分步查询_延迟加载
笔记要点出错分析与总结 工程组织 1.定义接口 DepartmentMapper package com.dao; import com.bean.Department; public interfa ...
- mybatis映射文件select_resultMap_关联查询_collection定义关联集合
知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee { pr ...
- 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 XML映射文件
mybatis为聚焦于SQL而构建,SQL映射文件常用的顶级元素如 resultMap,是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象. insert,映射插入语句 update, ...
- Hibernate的映射文件
映射文件的结构和属性 一个映射文件(mapping file)由一个根节点<hibernate-mapping>和多个<class>节点组成, 首先看看根节点<hiber ...
随机推荐
- 14、vue-pdf的使用
安装 npm install --save vue-pdf vue-pdf默认只显示第一页,可以写按钮翻页,也可以v-for多页显示 项目结构 实例一 按钮分页 <template> &l ...
- c# 无法加载DLL:找不到指定的模块(异常来自HRESULT:0X8007007E)
c# 无法加载DLL“xxxx”:找不到指定的模块(异常来自HRESULT:0X8007007E)的一个解决方法 以前的一个c#项目,今天运行的时候突然发现调用DLL时出现了下面的错误. 心中很诧异, ...
- vue-cli3 + ts 定义全局方法
一.定义全局方法不生效 虽然在main.ts当中定义了全局方法,但是在使用的时候根本找不到,也是无语了. 二.解决方法 我在网上找了很多方法,其中很多大神都是这样做的: 但是,我这样写了还是不生效 ...
- mysql 严格模式 Strict Mode
mysql 严格模式 Strict Mode 找到MySQL安装目录下的my.cnf(windows系统则是my.ini)文件 在sql_mode中加入STRICT_TRANS_TABLES则表示开启 ...
- 【剑指offer】面试题 52. 两个链表的第一个公共结点
面试题 52. 两个链表的第一个公共结点 NowCoder 题目描述 输入两个链表,找出它们的第一个公共结点. Java 实现 ListNode Class class ListNode { int ...
- [转帖]一文尽懂 USB4
一文尽懂 USB4 https://www.ithome.com/0/451/062.htm 今年 3 月份,USB Promoter Group(领导小组)首次发布了 USB4 规范,即下一代 US ...
- [转帖]使用Nginx转发TCP/UDP数据
使用Nginx转发TCP/UDP数据 https://www.cnblogs.com/guigujun/p/8075620.html 编译安装Nginx 从1.9.0开始,nginx就支持对TCP的转 ...
- nmap使用帮助翻译
Nmap 7.60 ( https://nmap.org )Usage: nmap [扫描类型] [操作] {目标说明}目标说明: 可以识别主机名.IP地址.网络,等等. 例如: scanme.n ...
- go 错误
错误 Go 程序使用 error 值来表示错误状态. 与 fmt.Stringer 类似,`error` 类型是一个内建接口: type error interface { Error() strin ...
- 【exgcd】卡片
卡片 题目描述 你有一叠标号为1到n的卡片.你有一种操作,可以重排列这些卡片,操作如下:1.将卡片分为前半部分和后半部分.2.依次从后半部分,前半部分中各取一张卡片,放到新的序列中.例如,对卡片序列( ...