Mybatis联合查询的实现方法
作者:WorkHaH 发布时间:2021-11-27 23:26:44
标签:Mybatis,联合查询
数据库表结构
department
employee
要求一
现在的要求是输入 id 把 employee 表的对应员工数据查询出来,并且查询出该员工的所处部门信息
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 表对应的部门信息查询出来,并且查询该部门下的所有员工信息
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.......
}
3、级联属性封装结果集
方法
<!-- 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));
}
结果
4、分步查询
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));
}
结果
来源:https://www.cnblogs.com/workhah/p/15759172.html


猜你喜欢
- 前言 找工作的时候,曾经用C语言练习过一段时间的算法题目,也在几个还算出名的OJ平台有过还算靠谱的排名。
- 简评:Android Support Library 26 中终于实现了一个等待已久的功能: RecyclerView 的快速滚动 。And
- 在我们日常开发过程中,通常会涉及到数据权限问题,下面以我们常见的一种场景举例:一个公司有很多部门,每个人所处的部门和角色也不同,所以数据权限
- using System;using System.Collections;using System.Xml;namespace Jb51.
- 由于机器内存坏了,换了个内存,重新安装了一个64位的Fedora16,把原来的32位的Fedora15格掉了。于是在重新安装Android
- EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。ehcach
- Javaweb分页技术实现分页技术就是通过SQL语句(如下)来获取数据,具体实现看下面代码//分页查询语句select * from 表名
- 页面拖动到最后一页 再向下滑动回复到 第一页,第一页向前滑动回到 最后一页同时,底部红色小圆点随着页面的滑动距离比例随时改变位置布局:<
- springboots使用的版本是2.0.1,注意不同版本可能有差异,并不一定通用添加Mybatis的起步依赖:<!--mybatis
- Android 动态菜单先上效果图比较简单,主要就是属性动画的使用和坐标角度的小细节。实现实现效果: 图标按照路径一路缩放渐变过来即可。核心
- 本文实例讲述了Android开发获取重力加速度和磁场强度的方法。分享给大家供大家参考,具体如下:Android获取重力加速度和磁场强度主要依
- 本文将介绍Java在ICPC快速IO实现方法,下面看看
- spring mvc @PathVariable / 带斜杠方式获取遇上这个问题,百度google了一下,抄袭里面的内容,可以实现,在此备忘
- Android中Progress的简单实例Android中Progress网上的demo都是瞎扯淡,当然,你们也可以认为我的demo是瞎扯淡
- 详解java.lang.reflect.Modifier.isInterface()方法java.lang.reflect.Modifier
- 发现问题最近在进行压测发现,有一些接口时好时坏,通过sentry日志平台及sky walking平台跟踪发现,用户张三获取到的用户上下文确是
- 使用@Indexed加快启动速度Spring读取@Component组件(派生性),有两种实现方式,一种是反射,一种是ASM。反射性能低主要
- 目录 * 仓库的配置1、 下载sonatype Nexus来搭建 * 2 安装nexus服务3、创建 * 仓库4、配置 * 信息中央仓库的配置三个仓
- 配置文件m103替换为hdfs服务地址。要利用Java客户端来存取HDFS上的文件,不得不说的是配置文件hadoop-0.20.2/conf
- 正则表达式是一种描述词素的重要表示方法。虽然正则表达式并不能表达出所有可能的模式(例如“由等数量的 a 和 b 组成的字符串”),但是它可以