欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis之a(chǎn)ssociation和collection用法

 更新時(shí)間:2022年02月07日 14:40:47   作者:Javxuan  
這篇文章主要介紹了Mybatis之a(chǎn)ssociation和collection用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

association和collection用法

1.單個(gè)關(guān)聯(lián)查詢association

1.1實(shí)體之間的關(guān)聯(lián)表示

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;
    //關(guān)聯(lián)的部門實(shí)體,查詢某個(gè)人的時(shí)候可以把所在部門信息查詢出來
    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 兩種關(guān)聯(lián)查詢方式

//第一中方式:直接進(jìn)行關(guān)聯(lián)查詢把關(guān)聯(lián)實(shí)體的屬性在xml中配置
//然后關(guān)聯(lián)查出來
<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 兩種方式的優(yōu)劣

a.查詢條件相同,所用的時(shí)間:從測(cè)試結(jié)果顯示,關(guān)聯(lián)查詢要比嵌套查詢快一點(diǎn)(結(jié)果不一定準(zhǔn)確,可能關(guān)聯(lián)表多的時(shí)候,結(jié)果會(huì)有所變化)

a.查詢條件相同,所用的時(shí)間:從測(cè)試結(jié)果顯示,關(guān)聯(lián)查詢要比嵌套查詢快一點(diǎn)(結(jié)果不一定準(zhǔn)確,可能關(guān)聯(lián)表多的時(shí)候,結(jié)果會(huì)有所變化)

這里寫圖片描述

這里寫圖片描述

b.適用的情況

2.多個(gè)關(guān)聯(lián)查詢 collection

2.1實(shí)體之間的關(guān)聯(lián)表示

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 兩種關(guān)聯(lián)查詢方式

//第一種方式嵌套查詢
<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>
//第二中方式關(guān)聯(lián)查詢
<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 鑒別器適用的場(chǎng)景

3.2 鑒別器的實(shí)現(xiàn) 

association和collection關(guān)聯(lián)查詢用法

這里只做最簡(jiǎn)單的用法,其它方法請(qǐng)自行查詢;

一對(duì)多 collection

?<collection property="要查詢的實(shí)體集合" javaType="java.util.List"
? ? ? ? ? ? ? ? ? ? ofType="要查詢的實(shí)體所在包路徑"
? ? ? ? ? ? ? ? ? ? select="要查詢的mapper方法"
? ? ? ? ? ? ? ? ? ? column="關(guān)聯(lián)的實(shí)體中的字段=關(guān)聯(lián)的數(shù)據(jù)庫(kù)中的字段"/>

舉例

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

一對(duì)一 & 多對(duì)一

<association property="要查詢的實(shí)體" column="數(shù)據(jù)庫(kù)中的關(guān)聯(lián)字段"
? ? ? ? ? ? ? ? ? ? ?javaType="要查詢的實(shí)體所在包路徑"
? ? ? ? ? ? ? ? ? ? ?select="要查詢的mapper方法"/>

舉例

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論