Mybatis之a(chǎn)ssociation和collection用法
association和collection用法
1.單個關(guān)聯(lián)查詢association
1.1實體之間的關(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)的部門實體,查詢某個人的時候可以把所在部門信息查詢出來 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)查詢方式
//第一中方式:直接進行關(guān)聯(lián)查詢把關(guān)聯(lián)實體的屬性在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.查詢條件相同,所用的時間:從測試結(jié)果顯示,關(guān)聯(lián)查詢要比嵌套查詢快一點(結(jié)果不一定準確,可能關(guān)聯(lián)表多的時候,結(jié)果會有所變化)
a.查詢條件相同,所用的時間:從測試結(jié)果顯示,關(guān)聯(lián)查詢要比嵌套查詢快一點(結(jié)果不一定準確,可能關(guān)聯(lián)表多的時候,結(jié)果會有所變化)
b.適用的情況
2.多個關(guān)聯(lián)查詢 collection
2.1實體之間的關(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 鑒別器適用的場景
3.2 鑒別器的實現(xiàn)
association和collection關(guān)聯(lián)查詢用法
這里只做最簡單的用法,其它方法請自行查詢;
一對多 collection
?<collection property="要查詢的實體集合" javaType="java.util.List" ? ? ? ? ? ? ? ? ? ? ofType="要查詢的實體所在包路徑" ? ? ? ? ? ? ? ? ? ? select="要查詢的mapper方法" ? ? ? ? ? ? ? ? ? ? column="關(guān)聯(lián)的實體中的字段=關(guān)聯(lián)的數(shù)據(jù)庫中的字段"/>
舉例
?<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="數(shù)據(jù)庫中的關(guān)聯(lián)字段" ? ? ? ? ? ? ? ? ? ? ?javaType="要查詢的實體所在包路徑" ? ? ? ? ? ? ? ? ? ? ?select="要查詢的mapper方法"/>
舉例
<association property="stsStudent" column="student_id" ? ? ? ? ? ? ? ? ? ? ?javaType="com.crm.project.domain.StsStudent" ? ? ? ? ? ? ? ? ? ? ?select="com.crm.project.mapper.StsStudentMapper.selectStsStudentById"/>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Mybatis中collection和association的使用區(qū)別詳解
- mybatis利用association或collection傳遞多參數(shù)子查詢
- 在Mybatis中association標簽多層嵌套的問題
- mybatis中一對一關(guān)系association標簽的使用
- MyBatis中association的基本使用方法
- mybatis的association傳遞參數(shù)問題示例
- Mybatis中一對多(collection)和一對一(association)的組合查詢使用
- MyBatis的collection和association的使用解讀
- mybatis中association標簽的使用解讀
- MyBatis使用嵌套查詢collection和association的實現(xiàn)
- Mybatis的association使用子查詢結(jié)果錯誤的問題解決
相關(guān)文章
Java中使用Files類的copy()方法實現(xiàn)復制文件
這篇文章主要介紹了Java中使用Files類的copy()方法實現(xiàn)復制文件,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-05-05Spring boot定時任務的原理及動態(tài)創(chuàng)建詳解
這篇文章主要給大家介紹了關(guān)于Spring boot定時任務的原理及動態(tài)創(chuàng)建的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-03-03SpringBoot下token短信驗證登入登出權(quán)限操作(token存放redis,ali短信接口)
這篇文章主要介紹了SpringBoot下token短信驗證登入登出權(quán)限操作(token存放redis,ali短信接口),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11Spring Boot集成Druid出現(xiàn)異常報錯的原因及解決
Druid 可以很好的監(jiān)控 DB 池連接和 SQL 的執(zhí)行情況,天生就是針對監(jiān)控而生的 DB 連接池。本文講述了Spring Boot集成Druid項目中discard long time none received connection異常的解決方法,出現(xiàn)此問題的同學可以參考下2021-05-05