笔记要点
出错分析与总结

工程组织

1.定义接口

  DepartmentMapper

package com.dao;

import com.bean.Department;

public interface DepartmentMapper {
public Department getDeptById(Integer id);
}

  EmployeeMapperPlus

package com.dao;
import com.bean.*;
public interface EmployeeMapperPlus {
public Employee getEmpByIdStep(Integer id); //分步查询 public Employee getEmpAndDept(Integer id);//关联查询 public Employee getEmpAndDept2(Integer id); //关联查询 ,使用association
}

2.定义XML映射文件

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; public Department getDeptByIdPlus(Integer id);
-->
<select id="getDeptByIdPlus" resultMap="">
select id,dept_name departmentName from tbl_dept
where id=#{id}
</select> </mapper>
EmployeeMapperPlus.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.EmployeeMapperPlus">
<!--ResultMap ;自定义结果集映射规则;
type: 自定义规则的Java类型;id: 唯一的标识,方便引用-->
<resultMap id="MySimpleEmp" type="com.bean.Employee">
<!--指定主键列的封装规则,id定义主键,底层会有优化规则;
column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性-->
<id column="id" property="id"/>
<!--定义普通列的封装规则-->
<result column="last_name" property="lastName"/>
<!--,其他不指定的列会自动封装;但是, 我们只要写ResultMap,就把剩下的映射全部都写上-->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap> <!--public Employee getEmpById(Integer id); 注意进行更改为resultMap-->
<select id="getEmpById" resultMap="MySimpleEmp">
select * from tbl_employee where id=#{id}
</select> <!--场景1,方法1:使用级联属性的方式
查询Employee的同时查询员工对应的部门Employee.dept=Department.id;
输出员工对应的部门的全部信息:
id last_name gender did dept_name
-->
<resultMap id="MyDifEmp" type="com.bean.Employee">
<!--column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性-->
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
<!--public Employee getEmpAndDept2(Integer id); //association 定义封装规则!--> <select id="getEmpAndDept" resultMap="MyDifEmp">
SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id,
d.id did,d.`dept_name` dept_name,email
FROM tbl_employee e,tbl_dept d
WHERE e.`d_id`=d.`id` AND e.id=#{id};
</select> <!--方法2:-使用association可以指定联合的javaBean的对象
-->
<resultMap id="MyDifEmp2" type="com.bean.Employee">
<!--column : 指定结果集的具体的那一列; property:指定的JavaBean对应的属性-->
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/> <!--使用association可以指定联合的javaBean的对象;(定义单个对象的封装规则!)
property="dept";指定那个属性是联合的对象;javaType="dept";指定那个属性对象的类型;-->
<association property="dept" javaType="com.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association> </resultMap>
<!--public Employee getEmpAndDept2(Integer id); //关联查询-->
<select id="getEmpAndDept2" resultMap="MyDifEmp2">
SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id,
d.id did,d.`dept_name` dept_name,email
FROM tbl_employee e,tbl_dept d
WHERE e.`d_id`=d.`id` AND e.id=#{id};
</select> <!--使用association进行分步查询,
1.先按照员工的id查询员工信息;
2.根据查询员工信息中的d_id值去部门表查出部门信息;
3;,部门设置到员工中
-->
<!--id last_name gender email d_id-->
<resultMap id="MyEmpByStep" type="com.bean.Employee">
<id column="id" property="id" />
<result column="last_name" property="lastName"/>
<!--association 定义关联对象的封装规则
select : 表明当前属性是调用select总置顶的方法查出的结果!
总的流程: 使用select 指定的方法(传入)查出对象,并封装给property
-->
<association column="d_id" property="dept"
select="com.dao.DepartmentMapper.getDeptById">
</association>
</resultMap> <!--public Employee getEmpByIdStep(Integer id);-->
<select id="getEmpByIdStep" resultMap="MyEmpByStep">
select * from tbl_employee
where id=#{id}
</select> <!--可以使用延迟加载,(按需加载,或者叫做懒加载)
Employee==>dept:
我们可以每次查询Employee对象的时候,都将一起查询出来;
部门信息在我们需要使用的时候再去查询,分段查询的基础之上加两个配置;
-->
<!-- 场景2: 查询部门的时候将部门对应的所有员工的信息也全部查询出来 --> </mapper>

3.编写测试关联查询的 代码

public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test06() throws Exception { SqlSession openSession = getSqlSessionFactory().openSession();
try {
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
System.out.println("---tp_30---多表关联查询,级联属性的封装结果!--------");
Employee employee = mapper.getEmpAndDept(1);
System.out.println(employee);
System.out.println("---tp_31--多表关联查询,使用association进行连接!--------");
Employee employee2 = mapper.getEmpAndDept2(1);
System.out.println(employee2);
openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交
} finally {
openSession.close();
}
}

测试结果

---tp_30---多表关联查询,级联属性的封装结果!--------
DEBUG 12-01 15:13:43,362 ==> Preparing: SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id, d.id did,d.`dept_name` dept_name,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=?; (BaseJdbcLogger.java:145)
DEBUG 12-01 15:13:43,380 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 15:13:43,391 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1', dept=Department{id=1, departmentName='开发部'}}
---tp_31--多表关联查询,使用association进行连接!--------
DEBUG 12-01 15:13:43,391 ==> Preparing: SELECT e.id id,e.last_name last_name,e.`gender` gender,e.`d_id` d_id, d.id did,d.`dept_name` dept_name,email FROM tbl_employee e,tbl_dept d WHERE e.`d_id`=d.`id` AND e.id=?; (BaseJdbcLogger.java:145)
DEBUG 12-01 15:13:43,392 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 15:13:43,393 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1', dept=Department{id=1, departmentName='开发部'}} Process finished with exit code 0

在全局配置文件中 开启延迟加载 (按需加载,或者叫做懒加载)

 <settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<!--显示地指定每个我们需要更改的配置的值,及时他是默认的;防止版本替换带来的问题-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/> </settings>

代码1   (仍使用上面的环境配置信息, 此代码不调用到Department 数据库 )

@Test
public void test07() throws Exception{
SqlSession openSession = getSqlSessionFactory().openSession();
try {
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
System.out.println("---tp_32---多表关联查询,使用association进行分布查询-----");
Employee employee = mapper.getEmpByIdStep(1);
System.out.println(employee.getEmail());
// System.out.println(employee.getDept()); openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交 }finally {
openSession.close();
}
}

结果1 (没有进行Dept 上数据库的关联查询)

---tp_32---多表关联查询,使用association进行分布查询-----
DEBUG 12-01 15:18:57,230 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:145)
DEBUG 12-01 15:18:57,249 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 15:18:57,308 <== Total: 1 (BaseJdbcLogger.java:145)
jerry@163.com

代码2  (仍使用上面的环境配置信息, 此代码可以调用到Department 数据库 )

  EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
System.out.println("---tp_32---多表关联查询,使用association进行分布查询-----");
Employee employee = mapper.getEmpByIdStep(1);
// System.out.println(employee.getEmail());
System.out.println(employee.getDept()); openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交

结果2

---tp_32---多表关联查询,使用association进行分布查询-----
DEBUG 12-01 15:19:50,044 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:145)
DEBUG 12-01 15:19:50,066 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 15:19:50,129 <== Total: 1 (BaseJdbcLogger.java:145)
DEBUG 12-01 15:19:50,129 ==> Preparing: select id,dept_name departmentName from tbl_dept where id=? (BaseJdbcLogger.java:145)
DEBUG 12-01 15:19:50,130 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 15:19:50,131 <== Total: 1 (BaseJdbcLogger.java:145)
Department{id=1, departmentName='开发部'}

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

  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_36-37]-_映射文件_select_resultMap关联查询__分步查询传递多列值&fetchType_discriminator鉴别器

    _分步查询传递多列值&fetchType_discriminator鉴别器 笔记要点出错分析与总结 Department.java bean public class Department { ...

  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. 第四章 MyBatis-SQL映射文件

    MyBatis 真正的强大在于映射语句,专注于SQL,功能强大,SQL映射的配置却是相当简单 SQL映射文件的几个顶级元素(按照定义的顺序) mapper - namespace cache - 配置 ...

随机推荐

  1. [转帖]hadoop学习笔记:hadoop文件系统浅析

    hadoop学习笔记:hadoop文件系统浅析 https://www.cnblogs.com/sharpxiajun/archive/2013/06/15/3137765.html 1.什么是分布式 ...

  2. [SVN] - 使用 TortoiseSVN 进行文件比对非常慢 之 解决

    背景 Windows 10 + TortoiseSVN-v1.9.7使用默认的 TortoiseMerge 进行文件比对,打开速度非常非常慢. 解决 禁用 TortoiseMerge / Settin ...

  3. C++语法笔记(上)

    客观事物中任何一个事物都可以看成一个对象,对象是由一组属性和一组行为构成的. c++中,每个对象都是由数据与函数这两部分构成,数据就是对象的属性,函数就是对象的行为. c++中对象的类型称为类,类是一 ...

  4. Python中nonlocal的用法

    class Text: def __init__(self): pass def big(self): n, m = 0, 0 def a(): nonlocal n n += 1 print(n) ...

  5. C语言处理字符串

    1. strtok 函数原型: char * strtok(char *str, const char * delim); 注意点: 两个入参必须为字符串数组: 第一次调用要传str, delim,后 ...

  6. Filter讲解4

    想要 浏览更多Fiddler内容:请点击进入Fiddler官方文档 阅读目录: 一.使用.NET代码扩展Fiddler 二.实现Fiddler接口 三.创建Fiddler扩展项目 四.在扩展程序选项卡 ...

  7. SQLSERVER 根据值查询表名

    CREATE PROCEDURE [dbo].[SP_FindValueInDB](@value VARCHAR(1024)) ASBEGIN-- SET NOCOUNT ON added to pr ...

  8. 血小板 live2d web使用

    关于此插件 看到一个很多网站都有动态的小人,目前除了即将废弃的flash就是canvas和h5动画了,h5动画能力有限,不能画出复杂的效果 那么canvas就是首选,全部手画也不可能,大部分使用库和工 ...

  9. # RESTful登录(基于token鉴权)的设计实例

    使用场景 现在很多基于restful的api接口都有个登录的设计,也就是在发起正式的请求之前先通过一个登录的请求接口,申请一个叫做token的东西.申请成功后,后面其他的支付请求都要带上这个token ...

  10. mysql授权单个表权限

    mysql> create user haochengtest@'%' identified by 'haocheng.123'; Query OK, 0 rows affected (0.01 ...