Mybatis之association和collection用法
作者:Javxuan 发布时间:2021-10-13 10:09:10
标签: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"/>
来源:https://blog.csdn.net/u014297148/article/details/78820895


猜你喜欢
- C#中的表格控件只有一个,那就是datagridview,不像QT中可以用QTableview,QTableWidget。新手拿到datag
- 效果图: //偶数随机 Random evenRanm
- java中初始化MediaRecorder实现代码:private boolean initializeVideo() { &
- 最近总是有人来和我说我以前写的一个小app无法正常获取数据~Android简易版天气预报app 今天就又运行了下来查找问题,发现或许是接口有
- BeanUtils.copyProperties忽略空值使用spring开发的人,对这行代码肯定不陌生,常用于DTO、VO、PO之间的复制。
- 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。代码public cla
- 简单工厂模式解释: 简单工厂模式(Simple Fact
- 本文实例为大家分享了Android自定义开关的具体代码,供大家参考,具体内容如下以 ToggleColorY 为例分析, ToggleIma
- 一个让人赏心悦目的界面对软件来说非常重要,因此图形图像资源也显得非常重要。本讲就要谈一谈Android中处理图形图像的最重要的一个类Draw
- Common.cs: using System; using System.Collections.Generic; using Syste
- using System;using System.Collections.Generic;using System.IO;using Sy
- 大家好,我叫柠檬水,今天马上就要放假,突然想到自己以前的伙伴、同学,好像想到他们空间没怎么发过动态,难道是把我屏蔽了吗,好友又那么多,行吧,
- 什么是https要说https我们得先说SSL(Secure Sockets Layer,安全套接层),这是一种为网络通信提供安全及数据完整
- 本文实例讲述了C#实现功能强大的中国农历日历操作类。分享给大家供大家参考。具体如下:这个C#类定义了中国农历日历,除了可以输入正常的日历外还
- public class Count { public static void main(String[] args) { int i =
- 本文实例为大家分享了Android读取NFC卡的编号具体代码,供大家参考,具体内容如下NFC相关androidManifest文件设置:一、
- 目前经常出现的时间有三个:本地时间(locale time)格林威治时间(Greenwich Mean Time GMT)时间协调时间 (U
- 最近在维护项目,app遇到安装在高版本的Android时,以往直接授权和new File(path)的形式不再支持,日志也是说Permiss
- 概念JavaBean在实际编程中,我们常常需要一些用来包装值对象的类,例如Student、 Employee、Order,这些 类中往往没有
- 1. Dozer 介绍Dozer 是一个 Java Bean 到 Java Bean 的映射器,它递归地将数据从一个对象复制到另一个对象。D