您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Mybatis中association和collection怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
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.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.1 鑒別器適用的場景
3.2 鑒別器的實現
這里只做最簡單的用法,其它方法請自行查詢;
<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"/>
關于“Mybatis中association和collection怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。