广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis之association和collection用法
  • 891
分享到

Mybatis之association和collection用法

2024-04-02 19:04:59 891人浏览 安东尼

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

摘要

目录association和collection用法1.单个关联查询association2.多个关联查询 collection3.鉴别器discriminatorassociati

association和collection用法

1.单个关联查询association

1.1实体之间的关联表示

package com.worldly.config.entity;
import java.io.Serializable;

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;

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"/>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Mybatis之association和collection用法

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

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

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

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

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作