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 ...
随机推荐
- 一次性清除页面上的所有setInterval
参考链接:https://www.cnblogs.com/liujinyu/p/3668575.html
- C++ 三大特性 继承(转载)
继承 继承:类的继承,就是新的类从已有类那里得到已有的特性.原有的类称为基类或父类,产生的新类称为派生类或子类. 基本语法 派生类的声明: class 派生类名:继承方式 基类名1, 继承方式 基类名 ...
- javascript bom操作
BOM BOM介绍 全称 Browser Object Mode 浏览器对象模式 操作浏览器的API接口.比如浏览器自动滚动 Windows对象的顶层部分是BOM的顶层(核心)对象,所有的对象都是通过 ...
- 生命不息,奔跑不止,持之以恒,勇攀高峰——JasonCeng的跑步打卡
生命不息,奔跑不止,持之以恒,勇攀高峰!JasonCeng的跑步打卡博文,持续更新,培养好习惯,一起变成更优秀的自己! 2019-12-20-5.05km Avg-speed(均速): 5'56'' ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- .NET Core IOC AOP
IOC简介 IOC思想 把类当做组件或服务来看待,组件内一定要高内聚,组件之间一定要低耦合,既然要保持低耦合,那就一定不要轻易的去new什么对象. 那组件之间的交互怎么处理呢?那最好的方式就是把new ...
- Python23之内置函数filter()和map()
首先我们了解一个概念:迭代 迭代是访问集合元素的⼀种⽅式.迭代器是⼀个可以记住遍历的位置的对象.迭代器对象从集合的第⼀个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 我们已经知道 ...
- vector 使用pair对
pair是一种序偶结构<x,y> 如果我们希望使用pair但又不需要map对其排序,可以在vector中使用pair对 插入pair对使用make_pair<typename,typ ...
- 关于Git无法提交 index.lock的解决办法
今天提交代码时,在一次提交,莫名其妙没成功后,再次用git commit -a命令时,出现以下错误,无论是用git还是TortoiseGit都会出现以下这个问题.. $ git commit -a f ...
- solr的命令
Start the Server If you didn’t start Solr after installing it, you can start it by running bin/solr ...