Mybatis之association和collection用法

 更新时间:2022年02月07日 14:40:47   作者:Javxuan  
这篇文章主要介绍了Mybatis之association和collection用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

association和collection用法

1.单个关联查询association

1.1实体之间的关联表示

package com.worldly.config.entity;
import java.io.Serializable;
/**
 * @Description
 * @Author xiaoqx <worldly_xuan@163.com>
 * @Version V1.0.0
 * @Since 2017/11/26
 */
public class Employee implements Serializable {
    private Integer id;
    private String name;
    private String email;
    private String tel;
    //关联的部门实体,查询某个人的时候可以把所在部门信息查询出来
    private Department dep;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public Department getDep() {
        return dep;
    }
    public void setDep(Department dep) {
        this.dep = dep;
    }
    @Override
    public String toString() {
        return "{\"Employee\":{"
                + "\"id\":\"" + id + "\""
                + ", \"name\":\"" + name + "\""
                + ", \"email\":\"" + email + "\""
                + ", \"tel\":\"" + tel + "\""
                + ", \"dep\":" + dep
                + "}}";
    }
}

1.2 两种关联查询方式

//第一中方式:直接进行关联查询把关联实体的属性在xml中配置
//然后关联查出来
<resultMap id="emp2ResultMap" type="com.worldly.config.entity.Employee">
        <id column="emp_id" property="id"></id>
        <result column="emp_name" property="name"/>
        <result column="emp_email" property="email"/>
        <result column="emp_tel" property="tel"/>
        <association property="dep" column="emp_dep" javaType="com.worldly.config.entity.Department">
            <id column="dep_id" property="id"/>
            <result column="dep_name" property="name"/>
            <result column="dep_addr" property="addr"/>
        </association>
    </resultMap>
    <select id="selectEmployAll" resultMap="emp2ResultMap">
        SELECT
            *
        FROM
            t_emp e
        INNER JOIN t_dep d ON e.emp_dep = d.dep_id
    </select>
//第二中查询方式,采用 association中的select来查询
<resultMap id="empResultMap" type="com.worldly.config.entity.Employee">
        <id column="emp_id" property="id"></id>
        <result column="emp_name" property="name"/>
        <result column="emp_email" property="email"/>
        <result column="emp_tel" property="tel"/>
        <association column="emp_dep" property="dep" javaType="com.worldly.config.entity.Department" select="selectDepByCondition"></association>
    </resultMap>
    <select id="selectEmployeeList" resultMap="empResultMap" databaseId="mysql">
        select * from t_emp
    </select>
    <resultMap id="depResultMap" type="com.worldly.config.entity.Department">
        <id column="dep_id" property="id"></id>
        <result column="dep_name" property="name"/>
        <result column="dep_addr" property="addr"/>
    </resultMap>
    <select id="selectDepByCondition" resultMap="depResultMap">
        SELECT
        *
        FROM
        t_dep d
        WHERE
        d.dep_id = #{emp_dep}
    </select>

1.3 两种方式的优劣

a.查询条件相同,所用的时间:从测试结果显示,关联查询要比嵌套查询快一点(结果不一定准确,可能关联表多的时候,结果会有所变化)

a.查询条件相同,所用的时间:从测试结果显示,关联查询要比嵌套查询快一点(结果不一定准确,可能关联表多的时候,结果会有所变化)

这里写图片描述

这里写图片描述

b.适用的情况

2.多个关联查询 collection

2.1实体之间的关联表示

package com.worldly.config.entity;
import java.util.List;
/**
 * @Description
 * @Author xiaoqx <worldly_xuan@163.com>
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2017/12/16
 */
public class Department {
    private int id;
    private String name;
    private String addr;
    List<Employee> employeeList;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNamel() {
        return name;
    }
    public void setNamel(String name) {
        this.name = name;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
    public List<Employee> getEmployeeList() {
        return employeeList;
    }
    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }
    @Override
    public String toString() {
        return "{\"Department\":{"
                + "\"id\":\"" + id + "\""
                + ", \"name\":\"" + name + "\""
                + ", \"addr\":\"" + addr + "\""
                + ", \"employeeList\":" + employeeList
                + "}}";
    }
}

2.2 两种关联查询方式

//第一种方式嵌套查询
<resultMap id="depResultMap2" type="com.worldly.config.entity.Department">
        <id column="dep_id" property="id"></id>
        <result column="dep_name" property="name"/>
        <result column="dep_addr" property="addr"/>
        <collection column="dep_id" property="employeeList" javaType="java.util.List" ofType="com.worldly.config.entity.Employee"
           select="selectEmpBydepId"/>
    </resultMap>
    <select id="selectDepByCondition" resultMap="depResultMap2">
        SELECT
            *
        FROM
            t_dep d
        WHERE
            d.dep_id = #{param}
    </select>
    <resultMap id="empResultMap" type="com.worldly.config.entity.Employee">
        <id column="emp_id" property="id"></id>
        <result column="emp_name" property="name"/>
        <result column="emp_email" property="email"/>
        <result column="emp_tel" property="tel"/>
    </resultMap>
    <select id="selectEmpBydepId" resultMap="empResultMap">
        SELECT
            *
        FROM
            t_emp e
        WHERE
            e.emp_dep = #{dep_id}
    </select>
//第二中方式关联查询
<resultMap id="dep2ResultMap" type="com.worldly.config.entity.Department">
        <id column="dep_id" property="id"></id>
        <result column="dep_name" property="name"/>
        <result column="dep_addr" property="addr"/>
        <collection property="employeeList" ofType="com.worldly.config.entity.Employee">
            <id column="emp_id" property="id"></id>
            <result column="emp_name" property="name"/>
            <result column="emp_email" property="email"/>
            <result column="emp_tel" property="tel"/>
        </collection>
    </resultMap>
    <select id="selectDepWithEmp" resultMap="dep2ResultMap">
        SELECT
            *
        FROM
            t_dep d
        INNER JOIN t_emp e ON d.dep_id = e.emp_dep
        WHERE
            d.dep_id = #{param}
    </select>

2.3 多条件查询

    <resultMap id="depResultMap2" type="com.worldly.config.entity.Department">
        <id column="dep_id" property="id"></id>
        <result column="dep_name" property="name"/>
        <result column="dep_addr" property="addr"/>
        <result column="dep_status" property="status"/>
        <collection column="{depId=dep_id,status=dep_status}" property="employeeList" javaType="java.util.List" ofType="com.worldly.config.entity.Employee"
           select="selectEmpBydepId"/>
    </resultMap>
    <select id="selectDepByCondition" resultMap="depResultMap2">
        SELECT
            *
        FROM
            t_dep d
        WHERE
            d.dep_id = #{param}
    </select>
    <resultMap id="empResultMap" type="com.worldly.config.entity.Employee">
        <id column="emp_id" property="id"></id>
        <result column="emp_name" property="name"/>
        <result column="emp_email" property="email"/>
        <result column="emp_tel" property="tel"/>
    </resultMap>
    <select id="selectEmpBydepId" resultMap="empResultMap">
        SELECT
            *
        FROM
            t_emp e
        WHERE
            e.emp_dep = #{depId} AND e.emp_status=#{status}
    </select>

多条件查询,用{}来包装方法

这里写图片描述

3.鉴别器discriminator

3.1 鉴别器适用的场景

3.2 鉴别器的实现 

association和collection关联查询用法

这里只做最简单的用法,其它方法请自行查询;

一对多 collection

 <collection property="要查询的实体集合" javaType="java.util.List"
                    ofType="要查询的实体所在包路径"
                    select="要查询的mapper方法"
                    column="关联的实体中的字段=关联的数据库中的字段"/>

举例

 <collection property="stsManageStudentList" javaType="java.util.List"
                    ofType="com.crm.project.domain.StsManageStudent"
                    select="com.crm.project.mapper.StsManageStudentMapper.selectStsManageStudentList"
                    column="manageId=manage_id"/>

一对一 & 多对一

<association property="要查询的实体" column="数据库中的关联字段"
                     javaType="要查询的实体所在包路径"
                     select="要查询的mapper方法"/>

举例

<association property="stsStudent" column="student_id"
                     javaType="com.crm.project.domain.StsStudent"
                     select="com.crm.project.mapper.StsStudentMapper.selectStsStudentById"/>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java中使用Files类的copy()方法实现复制文件

    Java中使用Files类的copy()方法实现复制文件

    这篇文章主要介绍了Java中使用Files类的copy()方法实现复制文件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-05-05
  • Spring boot定时任务的原理及动态创建详解

    Spring boot定时任务的原理及动态创建详解

    这篇文章主要给大家介绍了关于Spring boot定时任务的原理及动态创建的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Java详细介绍单例模式的应用

    Java详细介绍单例模式的应用

    单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式
    2022-09-09
  • java8新特性-lambda表达式入门学习心得

    java8新特性-lambda表达式入门学习心得

    这篇文章主要介绍了java8新特性-lambda表达式入门学习心得,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Java中如何检查数组是否包含某整数

    Java中如何检查数组是否包含某整数

    这篇文章主要介绍了在 Java 中检查数组是否包含某整数,在本文中,我们使用了几个内置的方法,如anyMatch()、contains()、binarySearch()等,我们将在给定的数组中找到一个值,结合示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • JAVA模拟多线程给多用户发送短信

    JAVA模拟多线程给多用户发送短信

    这篇文章主要介绍了JAVA模拟多线程给多用户发送短信,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 使用Jackson反序列化遇到的问题及解决

    使用Jackson反序列化遇到的问题及解决

    这篇文章主要介绍了使用Jackson反序列化遇到的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • SpringBoot下token短信验证登入登出权限操作(token存放redis,ali短信接口)

    SpringBoot下token短信验证登入登出权限操作(token存放redis,ali短信接口)

    这篇文章主要介绍了SpringBoot下token短信验证登入登出权限操作(token存放redis,ali短信接口),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Java mongodb连接配置实践

    Java mongodb连接配置实践

    这篇文章主要介绍了Java mongodb连接配置实践,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Spring Boot集成Druid出现异常报错的原因及解决

    Spring Boot集成Druid出现异常报错的原因及解决

    Druid 可以很好的监控 DB 池连接和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池。本文讲述了Spring Boot集成Druid项目中discard long time none received connection异常的解决方法,出现此问题的同学可以参考下
    2021-05-05

最新评论