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 ...
随机推荐
- python的网络工具scapy
文档 https://scapy.readthedocs.io/en/latest/api/scapy.sendrecv.html 阅读文档 https://blog.csdn.net/Al_xin/ ...
- package ‘RPMM’ is not available (for R version 3.6.0)
当我们用启动R安装一些R包的时候 提示: Warning: unable to access index for repository https://mirrors.eliteu.cn/CRAN/s ...
- QT qml---- loader使用方法
"简洁是智慧的灵魂,冗长是肤浅的藻饰"------------------<哈姆莱特>莎士比亚 Import Statement: import QtQuick 2.5 ...
- 使用jetpack 4.2.2对jetson tx2进行刷机
一.前言 加班加点几天今天终于成功刷机,记录一下成功的一些过程,以方便同样卡住的朋友参考. 延续官网教程[1]中对设备的叫法,pc机称为host,tx2称为target. 二.过程 1. host相关 ...
- Python3实现自动查询成绩(主要使用的包有Tesseract-OCR、PIL、execjs、pytesseract、BeautifulSoup)
前提:本文仅作为技术训练,不可利用技术做非法的事. 某考试的成绩查询页面如下:查询成绩需要的数据有准考证号或者身份证.考生姓名.验证码.现在使用python来实现自动查询指定人员的考试成绩(不知道准考 ...
- 线性表——顺序表的实现与讲解(C++描述)
线性表 引言 新生安排体检,为了 便管理与统一数据,学校特地规定了排队的方式,即按照学号排队,谁在前谁在后,这都是规定好的,所以谁在谁不在,都是非常方便统计的,同学们就像被一条线(学号)联系起来了,这 ...
- ESP32 - GPIO中断触发与事件回调
最近为项目增加了GPIO外部触发中断功能,原理是为GPIO32注册了上升沿触发事件,事件触发后,会向RTOS队列写入数据.在RTOS事件中检测到该队列中有新加入的事件,就读出,并执行相应代码. #de ...
- notepad++安装markdown
notepad++ 安装markdown安装markdown插件一.下载最新的markdown插件, github:https://github.com/nea/MarkdownViewerPlusP ...
- Python3 - 随便说一下
Ⅰ编程语言基础知识 ⅡPython 语言概述 Ⅰ编程语言基础知识 编程语言总体分以为机器语言.汇编语言.高级语言: 机器语言:计算机硬件能够直接使用的编程语言,二进制的集合,属于低级语言. 汇编语言: ...
- PAT(B) 1078 字符串压缩与解压(Java)
题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...