Mybatis联合查询的实现方法

目录
  • 1、级联属性封装结果集
    • 实现
  • 2、分步查询
    • 方法
  • 3、级联属性封装结果集
  • 4、分步查询

数据库表结构

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联合查询内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • mybatis Plus 多表联合查询的实现示例

    本文主要介绍了mybatis Plus 多表联合查询,分享给大家,具体如下: //实体类package com.sk.skkill.entity; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import java.io.Serializable; import java.util.D

  • mybatis如何使用Criteria的and和or进行联合查询

    目录 Criteria的and和or进行联合查询 使用criteria 查询xx and ( xx or xx)形式的sql Criteria的and和or进行联合查询 DemoExample example=new DemoExample (); DemoExample.Criteria criteria=example.createCriteria(); criteria.andidEqualTo(id); criteria.andStatusEqualTo("0"); DemoE

  • MyBatis-Plus多表联合查询并且分页(3表联合)

    这3张表的关系是模型表Model  ===> 训练表Training ===>应用表Application(大概的逻辑是:选择应用,然后训练,然后成为模型) 首先我们先建立实体Model(我使用的data注解不需要get set  @TableField(exist = false) 注解下的属性 是相关联表的属性) package cn.com.befery.dataai.po; import java.util.Date; import org.springframework.boot.j

  • springboot + mybatis-plus实现多表联合查询功能(注解方式)

    第一步:加入mybatis-plus依赖 第二步:配置数据源 spring: thymeleaf: cache: false encoding: utf-8 prefix: classpath:/templates/ suffix: .html enabled: true datasource: url: jdbc:mysql://192.168.1.152:3306/timo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&

  • Mybatis联合查询的实现方法

    目录 1.级联属性封装结果集 实现 2.分步查询 方法 3.级联属性封装结果集 4.分步查询 数据库表结构 department employee 要求一 现在的要求是输入 id 把 employee 表的对应员工数据查询出来,并且查询出该员工的所处部门信息 public class Employee { private Integer id; private String lastName; private String email; private String gender; privat

  • mybatis 模糊查询的实现方法

    mybatis 模糊查询的实现方法 mybatis的逆向助手确实好用,可以省去很多编写常规sql语句的时间,但是它没办法自动生成模糊查询语句,但开发中模糊查询是必不可少的,所以,需要手动对mapper编写模糊查询功能. 这里先明确MyBatis/Ibatis中#和$的区别: 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解

  • Java Fluent Mybatis 聚合查询与apply方法详解流程篇

    前言 接着上一篇文章:Java Fluent Mybatis 分页查询与sql日志输出详解流程篇 我把分页已经调整好了,现在实验一下官方给出的聚合查询方法. GitHub代码仓库:GitHub仓库 数据准备 为了聚合查询的条件,添加了几条数据. MIN 我们试着获取最小的年龄. 方法实现 @Override public Integer getAgeMin() { Map<String, Object> result = testFluentMybatisMapper .findOneMap(

  • MyBatis 多表联合查询及优化方法

    目录 背景 正文 关于优化 这篇文章我打算来简单的谈谈 mybatis 的多表联合查询.起初是觉得挺简单的,没必要拿出来写,毕竟 mybatis 这东西现在是个开发的都会用,而且网上的文章也是一搜罗一大堆,根本就用不着我来重复.但是吧,就我前几天在做一个多表联合查询的时候,竟然出了很多意想不到的问题,而且这些问题的出现,并不是对 mybatis 不了解,而是在用的过程中会或多或少的忽略一些东西,导致提示各种错误. 背景 老规矩,开始之前,还是要先说说这件事的背景.也就是最近几天,公司要做一个后台

  • mybatis collection 多条件查询的实现方法

    mybatis collection 多条件查询的实现方法 前言: 业务需要通过mybatis 查询返回嵌套集合,嫌多次查询太麻烦,用自带的高级查询解决问题,下边是代码,已测试通过. 说下自己的理解,就是一个主查询结果集里面嵌套了子查询的结果集,可以是多个子查询,每个子查询的条件从主查询结果集中获取,返回值各自定义.collection 标签的property是主查询里面集合的名字,如果有多个就再写个collection,column是子查询参数,单参数直接写主查询结合返回结果,例如直接写上us

  • cakephp2.X多表联合查询join及使用分页查询的方法

    本文实例讲述了cakephp2.X多表联合查询join及使用分页查询的方法.分享给大家供大家参考,具体如下: 格式化参数: public function getconditions($data){ $this->loadModel("Cm.LoginHistory"); $conditions = array(); foreach ($data as $key=>$val){ if($key=='start_date'){ $conditions['LoginHistor

  • Symfony2联合查询实现方法

    本文实例讲述了Symfony2联合查询实现方法.分享给大家供大家参考,具体如下: 1.yml文件 Acme\MspadminBundle\Entity\MspArticle: type: entity table: msp_article manyToOne: Channel: targetEntity: MspChannel inversedBy: Articles joinColumn: name: channel_id referencedColumnName: channel_id Us

  • mybatis模糊查询、分页和别名配置的方法

    mybatis模糊查询(3种) 第一种 select * from user where username like "%" #{name} "%" 第二种 select * from user where username like "%${value}%" 第三种 <!--concat拼接字符串 mysql独有的函数--> select * from user where username like concat("%&

  • MySQL联合查询实现方法详解

    联合查询简单说 就是将两次查询合并在一起 例如 我们这里有一个用户表 我们先编写一段SQL select name from staff where age > 21; 查询年龄大于21的 输出结果如下 然后我们再写一段sql select name from staff where status =1; 查询 status 状态字段等于1 的 输出效果如下 然后我们可以二合一一下 select name from staff where age > 21 union all select n

随机推荐