Mybatis3.1-[tp-30-31]-select_resultMap_关联查询_级联属性封装结果__association定义关联对象封装规则
笔记要点
出错分析与总结
在全局配置中,映射dao包下的全部: <mapper>
<package name="com.dao"/>
</mappers>
工程组织

1.定义接口
package com.dao;
import com.bean.*;
public interface EmployeeMapperPlus { public Employee getEmpAndDept(Integer id);//关联查询
public Employee getEmpAndDept2(Integer id); //关联查询 ,使用association
public Employee getEmpById(Integer id); }
2.定义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> </mapper>
3.编写测试代码
package com.test.resultMap; import com.bean.Employee;
import com.dao.EmployeeMapperPlus;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.IOException;
import java.io.InputStream; public class test_tp30 {
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("--------多表关联查询,级联属性的封装结果!--------");
Employee employee = mapper.getEmpAndDept(1);
System.out.println(employee);
System.out.println("--------多表关联查询,使用association进行连接!--------");
Employee employee2 = mapper.getEmpAndDept2(1);
System.out.println(employee2);
openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交
}finally {
openSession.close();
}
}
}
测试结果
--------多表关联查询,级联属性的封装结果!--------
DEBUG 12-01 10:37:03,938 ==> 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 10:37:03,958 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 10:37:03,968 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'}
--------多表关联查询,使用association进行连接!--------
DEBUG 12-01 10:37:03,969 ==> 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 10:37:03,969 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-01 10:37:03,970 <== Total: 1 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='jerry', email='jerry@163.com', gender='1'} Process finished with exit code 0
Mybatis3.1-[tp-30-31]-select_resultMap_关联查询_级联属性封装结果__association定义关联对象封装规则的更多相关文章
- mybatis_09关联查询_一对一
复杂查询时,单表对应的po类已不能满足输出结果集的映射. 所以有些时候就需要关联查询_一对一:通过条件查询结果每个字段都唯一 一对一:模型里面有模型 一对多:模型里面有集合 多对多:集合里面有集合 方 ...
- JavaWeb_(Mybatis框架)关联查询_六
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- mybatis_11关联查询_多对多
一对一:模型里面有模型 一对多:模型里面有集合 多对多:集合里面有集合 映射思路 pojo类设计思路 v 将用户信息映射到user中. v 在user类中添加订单列表属性List<Ord ...
- mybatis_10关联查询_一对多
在使用mybatis框架的时候,很多时候需要一个查询结果里的属性包含多个对象,即一条查询结果有属性是集合,这个时候就需要使用collection标签 模型里面有集合 案例: 第一步:在Orders中添 ...
- 【mysql】关联查询_子查询_排序分组优化
1. 关联查询优化 1.1 left join 结论: ①在优化关联查询时,只有在被驱动表上建立索引才有效! ②left join 时,左侧的为驱动表,右侧为被驱动表! 1.2 inner join ...
- Mybatis之关联查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- MyBatis学习总结(三)——多表关联查询与动态SQL
在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...
- Mybatis框架学习总结-表的关联查询
一对一关联 创建表和数据:创建一张教师表和班级表,这里假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关系. CREATE TABLE teacher( t_id INT PRIM ...
- MyBatis:一对多关联查询
MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...
随机推荐
- Web书写Test Case时需要考虑的检查点
通常书写Test Case时需要考虑的检查点: 一. 对于屏幕显示来说包括:1.检查显示的布局:2.检查域和按钮的顺序:3.检查域的尺寸:4.检查字体的大小和风格:5.检查文本的含义:6.检查拼写错误 ...
- svn服务器端—管理员分配权限
1.SVN服务器搭建和使用 下载地址:http://subversion.apache.org/packages.html 滚动到浏览器底部,下载并安装:VisualSVN服务器端和tortoiseS ...
- c# .net 查找并安装CA根证书
https CA根证书 用的是证书指纹来查找. 在用 collection.Find 之前 ,X509Store 一定要打开(Open),否则找到的数量(X509Certificate2Collect ...
- iptables拦截tcp报文syn/ack/rst/psh/fin
https://www.cnblogs.com/Qingluan/p/5137136.html https://blog.csdn.net/weixin_34216107/article/detail ...
- [转帖]深度分析HBase架构
深度分析HBase架构 https://zhuanlan.zhihu.com/p/30414252 原文链接(https://mapr.com/blog/in-depth-look-hbase-a ...
- STL源码剖析——iterators与trait编程#3 iterator_category
最后一个迭代器的相应类型就是iterator_category,就是迭代器本身的类型,根据移动特性与实行的操作,迭代器被分为了五类: Input Iterator:这种迭代器所指的对象,不允许外界改变 ...
- 欧拉图Eulerian Graph
一.节点的度 无向图:节点的度为该节点所连接的边数 有向图:节点的度分为入度和出度. 二.欧拉图定义 具有欧拉回路的图称作欧拉图,具有欧拉路径而无欧拉回路的图称为半欧拉图. 欧拉回路: 通过图中每 ...
- C/C++中vector与list的区别
1.vector数据结构vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存 ...
- 【SCALA】1、我要开始学习scala啦
因为scala也是基于jvm上运行的,所以能跑java,原则上就能跑scala 1.国际惯例,先来个hello world走走 package demo1 //object之下全为静态,scala没有 ...
- ES与关系型数据库的通俗比较
1.在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,我们可以画一些简单的对比图来类比传统关系型数据库: Relational DB -> D ...