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 ...
随机推荐
- 利用unordered_map维护关联数据
在leetcode上刷339题Evaluate Division(https://leetcode.com/problems/evaluate-division/#/description)时在脑中过 ...
- App内容分享
1.发送文本内容 发送简单的数据到其他应用,比如社交分分享的内容,意图允许用户快速而方便的共享信息. //分享简单的文本内容 public void btnShareText(View view) { ...
- nexus 私服 拉不了 jar 包,报 Not authorized
问题: 无法下载导入jar包,idea reload 时 报: Could not transfer artifact com.xxx:parent:pom:1.0-SNAPSHOT from/to ...
- Shell脚本定期清空大于1G的日志文件
一个关于如何在指定文件大于1GB后,自动删除的问题. 批处理代码如下: #!/bin/bash # 当/var/log/syslog大于1GB时 # 自动将其备份,并清空 # 注意这里awk的使用 i ...
- ES6常用的数值转换方法
<script type="text/javascript"> // Number常用方法 /* Number.isFinite() 用来检查一个数值是否为有限的(fi ...
- arcgis api for js回调函数如何等待同步
arcgis js开发往往会遇到同步异步的问题,有可能在上一步使用了arcgis js模块回调函数,下一步需要用上一步回调函数的结果,但是因为JavaScript是异步执行的,它并不会等待上一步的回调 ...
- 【简】题解 AWSL090429 【价值】
先考虑当要选的物品一定时 显然有个贪心 wi越小的要越先选 所以先按wi从小到大拍序 因为发现正着递推要记录的状态很多 并且wi的贡献与后面选了几个物品有关 考虑正难则反 倒着递推 提前计算wi的贡献 ...
- Taro 微信小程序 上传文件到minio
小程序前端上传文件不建议直接引用minio的js npm包,一来是这个包本身较大,会影响小程序的体积,二来是ak sk需要放到前端存储,不够安全,因此建议通过请求后端拿到签名数据后上传. 由于小程序的 ...
- Python模块和函数
目录 一.基础 二.特殊函数 一.基础 #导入模块 import xxx #调用 xxx.dd() from xxx import xx as dd #导入某个函数,as给函数加别名,调用xx() ...
- CF656C Without Text 题解
Content 输入一个字符串 \(s\),遍历每一个字符,如果这个字符是小写字母,那么答案就加上这个字母的字母表序:否则,如果这个字符是小写字母,那么答案就减去这个字母的字母表序.求最后的答案. 字 ...