广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis联合查询的实现方法
  • 494
分享到

Mybatis联合查询的实现方法

2024-04-02 19:04:59 494人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录1、级联属性封装结果集实现2、分步查询方法3、级联属性封装结果集4、分步查询数据库表结构 department employee 要求一 现在的要求是输入 id 把 em

数据库表结构

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));
    }

结果

到此这篇关于 mybatis联合查询的实现方法的文章就介绍到这了,更多相关 Mybatis联合查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis联合查询的实现方法

本文链接: https://www.lsjlt.com/news/161863.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Mybatis联合查询的实现方法
    目录1、级联属性封装结果集实现2、分步查询方法3、级联属性封装结果集4、分步查询数据库表结构 department employee 要求一 现在的要求是输入 id 把 em...
    99+
    2022-11-12
  • Mybatis联合查询怎么实现
    本篇内容主要讲解“Mybatis联合查询怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mybatis联合查询怎么实现”吧!数据库表结构departmentemployee要求一现在的要求...
    99+
    2023-06-26
  • MyBatis 多表联合查询及优化方法
    目录背景正文关于优化这篇文章我打算来简单的谈谈 mybatis 的多表联合查询。起初是觉得挺简单的,没必要拿出来写,毕竟 mybatis 这东西现在是个开发的都会用,而且网上的文章也...
    99+
    2022-11-13
    MyBatis 多表联合查询 MyBatis 多表查询
  • MySQL联合查询实现方法详解
    联合查询简单说 就是将两次查询合并在一起 例如 我们这里有一个用户表 我们先编写一段SQL select name from staff where age > 21; 查询年龄大于21的 输...
    99+
    2022-11-01
  • MyBatis-Plus多表联查的实现方法(动态查询和静态查询)
    目录建库建表依赖配置代码测试1.静态查询2.动态查询 1.不传条件2.传条件建库建表 DROP DATABASE IF EXISTS mp; CREATE DATA...
    99+
    2022-11-13
  • MyBatis实现多表联合查询resultType的返回值
    目录多表联合查询resultType的返回值一般数据按参数类型返回根据某字段查询查询结果为多条记录,存放在list中返回多表联合查询解决方案多表联查,返回结果嵌套list多表联合查询...
    99+
    2022-11-13
  • mysql多表联合的查询方法
    本文主要给大家介绍mysql多表联合的查询方法,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下mysql多表联合的查询方法吧。   ...
    99+
    2022-10-18
  • SpringBoot中整合MyBatis-Plus-Join使用联表查询的实现
    目录1、mybatis-plus2、mybatis-plus-join3、引入依赖4、mybatis配置信息5、建库建表6、代码自动生成7、联表查询1、mybatis-plus 相信...
    99+
    2023-03-03
    MyBatis-Plus-Join 联表查询 MyBatis-Plus Join查询
  • MyBatis如何实现多表联合查询resultType的返回值
    这篇“MyBatis如何实现多表联合查询resultType的返回值”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“MyBat...
    99+
    2023-06-29
  • Mybatis Plus关联查询怎么实现
    本篇内容介绍了“Mybatis Plus关联查询怎么实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Mybatis-Plus 简介什么是 ...
    99+
    2023-06-22
  • MySQL中的聚合查询和联合查询怎么实现
    这篇文章主要介绍“MySQL中的聚合查询和联合查询怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“MySQL中的聚合查询和联合查询怎么实现”文章能帮助大家解决问题。一、聚合查询(行与行之间的计...
    99+
    2023-07-05
  • Mybatis多对一查询的实现方法
    目录架构环境搭建数据库实体类接口Mapper配置文件子查询方法按结果集查询架构 这里从学生的角度来说就是多对一的场景 那么在Java中是怎么样的呢? 环境搭建 数据库 CREATE...
    99+
    2022-11-13
  • MySQL联合查询如何实现
    这篇文章主要介绍“MySQL联合查询如何实现”,在日常操作中,相信很多人在MySQL联合查询如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL联合查询如何实现”的疑惑有所帮助!接下来,请跟着小编...
    99+
    2023-07-04
  • springboot整合mybatis实现简单的一对多级联查询功能
    本文的目的是用springboot整合mybatis实现一个简单的一对多查询。(查询一个用户有多少件衣服) 第一步:数据库中,可以直接在navicat中建立两张我们需要用到的表 ...
    99+
    2022-11-12
  • mybatis collection 多条件查询的实现方法
    mybatis collection 多条件查询的实现方法 前言: 业务需要通过mybatis 查询返回嵌套集合,嫌多次查询太麻烦,用自带的高级查询解决问题,下边是代码,已测试通过。 说下自己的理解,就是一...
    99+
    2022-10-18
  • MyBatis流式查询的三种实现方法
    导读:流式查询指的是查询成功后不是返回一个集合而是返回一个迭代器,应用每次从迭代器取一条查询结果。流式查询的好处是能够降低内存使用 如果没有流式查询,我们想要从数据库取 1000 万...
    99+
    2022-11-12
  • Mybatis Criteria使用and和or进行联合条件查询的操作方法
    之前用Mybatis框架反向的实体,还有实体里面的Example,之前只是知道Example里面放的是条件查询的方法,可以一直不知道怎么用,到今天才开始知道怎么简单的用。在我们前台查...
    99+
    2022-11-12
  • MyBatis-Plus实现连表查询的方法实例
    目录使用方法安装使用核心类MPJLambdaWrapper和MPJQueryWrapperMPJLambdaWrapper用法MPJQueryWrapper总结mybatis-plu...
    99+
    2022-11-13
  • Mysql中怎么实现子查询和联合查询
    Mysql中怎么实现子查询和联合查询,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。查询: 在select查...
    99+
    2022-10-18
  • mybatis-plus多表关联查询功能的实现
    学习目标: mybatis-plus多表关联查询 学习内容: mybatis-plus多表关联查询 实体类部分代码 @Data @AllArgsConstructor @NoAr...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作