Mybatis的联合查询
数据库表结构
department
employee
要求一
现在的要求是输入 id 把 employee 表的对应员工数据查询出来,并且查询出该员工的所处部门信息
JavaBean
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;
setter和getter.......
}
public class Department {
private Integer id;
private String departmentName;
setter和getter.......
}
1、级联属性封装结果集
实现
这个要求很明显就要用到两个表,想要把部门信息封装到
Employee
对象的dept字段需要用到resultMap
属性
方法一
<!-- public Employee getEmployee(int id); -->
<select id="getEmployee" resultMap="emp1">
select e.*, d.id did, d.department_name
from employee e,
department d
where e.d_id = d.id
and e.id = #{id}
</select>
<resultMap id="emp1" type="employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="department_name" property="dept.departmentName"/>
</resultMap>
方法二
<!-- public Employee getEmployee(int id); -->
<select id="getEmployee" resultMap="emp2">
select e.*, d.id did, d.department_name
from employee e,
department d
where e.d_id = d.id
and e.id = #{id}
</select>
<resultMap id="emp2" type="employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<association property="dept" javaType="department">
<id column="did" property="id"/>
<result column="department_name" property="departmentName"/>
</association>
</resultMap>
测试
@Test
public void test1() {
SqlSession sqlSession = MyTest.getSqlSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
System.out.println(mapper.getEmployee(1));
}
结果
2、分步查询
方法
DepartmentMapper.xml
<!-- public Department getDepartment2(int id); -->
<select id="getDepartment2" resultType="department">
select * from department where id = #{id}
</select>
EmployeeMaper.xml
<!-- public Employee getEmployee2(int id); -->
<!-- 分步查询 -->
<select id="getEmployee2" resultMap="emp3">
select * from employee where id = #{id}
</select>
<resultMap id="emp3" type="employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<association property="dept" select="com.workhah.mapper.department.DepartmentMapper.getDepartment2" column="d_id"/>
</resultMap>
测试
@Test
public void test1() {
SqlSession sqlSession = MyTest.getSqlSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
System.out.println(mapper.getEmployee2(1));
}
结果
要求二
现在的要求是输入 id 把 department 表对应的部门信息查询出来,并且查询该部门下的所有员工信息
JavaBean
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
setter和getter.......
}
public class Department {
private Integer id;
private String departmentName;
private List<Employee> employees;
setter和getter.......
}
1、级联属性封装结果集
方法
<!-- public Department getDepartment(int id); -->
<select id="getDepartment" resultMap="dep1">
select d.*, e.id eid, e.last_name, e.email, e.gender
from department d
left join employee e on d.id = e.d_id
where d.id = #{id}
</select>
<resultMap id="dep1" type="department">
<id column="id" property="id"/>
<result column="department_name" property="departmentName"/>
<collection property="employees" ofType="employee">
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
测试
@Test
public void test2() {
SqlSession sqlSession = MyTest.getSqlSession();
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println(mapper.getDepartment(1));
}
结果
2、分步查询
EmployeeMaper.xml
<!-- public List<Employee> getEmployeeByDid(int did); -->
<select id="getEmployeeByDid" resultType="employee">
select *
from employee
where d_id = #{did}
</select>
DepartmentMapper.xml
<!-- public Department getDepartment3(int id); -->
<select id="getDepartment3" resultMap="dep2">
select *
from department
where id = #{id}
</select>
<resultMap id="dep2" type="department">
<id column="id" property="id"/>
<result column="depart_name" property="departName"/>
<collection property="employees" ofType="employee"
select="com.workhah.mapper.employee.EmployeeMapper.getEmployeeByDid" column="id"/>
</resultMap>
测试
@Test
public void test2() {
SqlSession sqlSession = MyTest.getSqlSession();
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println(mapper.getDepartment3(1));
}
结果
Mybatis的联合查询的更多相关文章
- MyBatis 示例-联合查询
简介 MyBatis 提供了两种联合查询的方式,一种是嵌套查询,一种是嵌套结果.先说结论:在项目中不建议使用嵌套查询,会出现性能问题,可以使用嵌套结果. 测试类:com.yjw.demo.JointQ ...
- Mybatis实现联合查询(六)
1. 疑问 在之前的章节中我们阐述了如何用Mybatis实现检查的查询,而我们实际的需求中,绝大部分查询都不只是针对单张数据表的简单查询,所以我们接下来要看一下Mybatis如何实现联合查询. 2. ...
- mybatis plus 联合查询
在xml中只需要需要写如下的代码即可实现分页: <select id="selectUserList" parameterType="map" resul ...
- mybatis的嵌套查询(嵌套查询nested select和嵌套结果nested results查询)区别
(转自:http://blog.csdn.net/canot/article/details/51485955) Mybatis表现关联关系比hibernate简单,没有分那么细致one-to-man ...
- mybatis的嵌套查询与嵌套结果查询的不同
原文:https://blog.csdn.net/qq_39706071/article/details/85156840 实体类: 嵌套查询mapper方法:嵌套查询的弊端:即嵌套查询的N+1问题尽 ...
- MyBatis 多表联合查询及优化 以及自定义返回结果集
下面就来说一下 mybatis 是通过什么来实现多表联合查询的.首先看一下表关系,如图: 这 里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了 ...
- MyBatis之三:多表联合查询
在这篇文章里面主要讲解如何在mybatis里面使用一对一.一对多.多表联合查询(类似视图)操作的例子. 注:阅读本文前请先大概看一下之前两篇文章. 一.表结构 班级表class,学生表student, ...
- Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务
第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...
- MyBatis 多表联合查询,字段重复的解决方法
MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...
随机推荐
- webservice--常用注解
定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...
- How is Quality Score Calculated?
Google determines Quality Score slightly differently for each of the different advertising networks ...
- Linux基础命令---mysqlimport导入数据库
mysqlimport mysqlimport指令可以用来将文本文件中的数据导入到数据库.在导入文本文件的时候,必须确保数据库中有一张表,而且他的名字和文本文件的名字是一样的. 此命令的适用范围:Re ...
- 【Python】【Module】hashlib
用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib # ######## ...
- C#内建接口:IEnumerable
这节讲一下接口IEnumerable. 01 什么是Enumerable 在一些返回集合数据的接口中,我们经常能看到IEnumerable接口的身影.那什么是Enumerable呢?首先它跟C#中的e ...
- Pycharm, 添加requirements.txt
引用:https://www.jetbrains.com/help/pycharm/managing-dependencies.html 1) 2) 3)
- Mysql主从复制参数详解
目录 一.简介 二.例子 同步 修改 三.参数 一.简介 change master to配置和改变slave服务器用于连接master服务器的参数,以便slave服务器读取master服务器的bin ...
- Linux中的正则
目录 一.匹配规则 二.举例 一.匹配规则 * 匹配 0 或多个字符 ? 匹配任意一个字符 [list] 匹配 list 中的任意单一字符 [^list] 匹配 除list 中的任意单一字符以外的字符 ...
- 2021 中国.NET开发者峰会 今日开幕
01 大会介绍 .NET Conf China 2021 是面向开发人员的社区峰会,基于 .NET Conf 2021的活动,庆祝 .NET 6 的发布和回顾过去一年来 .NET 在中国的发展成果展示 ...
- Python pyecharts绘制仪表盘
一.仪表盘gauge.add方法简介 gauge.add()方法签名 add(name,attr,value, scale_range=none, angle_range=none,**kwargs) ...