Mybatis之association和collection用法
作者:Javxuan 时间:2021-10-13 10:09:10
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
标签:Mybatis,association,collection
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java编程中使用XFire框架调用WebService程序接口
2023-11-06 20:16:33
![](https://img.aspxhome.com/file/2023/0/58990_0s.png)
Struts2实现文件上传功能实例解析
2023-11-04 04:56:13
![](https://img.aspxhome.com/file/2023/6/58836_0s.png)
java调用openoffice将office系列文档转换为PDF的示例方法
2023-11-29 11:42:53
简单的java图片处理类(图片水印 图片缩放)
2022-04-02 03:24:37
C# SQLite执行效率的优化教程
2021-07-11 00:11:41
![](https://img.aspxhome.com/file/2023/9/68659_0s.png)
C#构建树形结构数据(全部构建,查找构建)
2022-07-22 12:22:52
![](https://img.aspxhome.com/file/2023/3/81133_0s.png)
c#创建圆形类Circle、矩形类实现代码
2023-08-18 19:59:12
![](https://img.aspxhome.com/file/2023/3/80123_0s.png)
通过Mybatis实现单表内一对多的数据展示示例代码
2021-12-20 12:34:31
![](https://img.aspxhome.com/file/2023/1/64011_0s.png)
Java多线程编程详细解释
2022-12-22 10:19:41
移动开发Spring Boot外置tomcat教程及解决方法
2023-08-25 11:47:33
MyBatis Plus插件机制与执行流程原理分析详解
2021-10-09 20:53:48
![](https://img.aspxhome.com/file/2023/5/77155_0s.jpg)
java 工厂模式的讲解及优缺点的介绍
2022-02-05 10:57:09
C#单例类的实现方法
2023-12-01 02:13:30
Java拦截器Interceptor和过滤器Filte的执行顺序和区别
2022-06-01 20:37:11
![](https://img.aspxhome.com/file/2023/2/61142_0s.jpg)
工作中禁止使用Executors快捷创建线程池原理详解
2021-11-24 20:55:48
![](https://img.aspxhome.com/file/2023/9/75439_0s.png)
Spring Boot2中如何优雅地个性化定制Jackson实现示例
2021-09-27 12:21:15
解决Mybatis-Plus操作分页后数据失效问题
2023-11-25 11:31:55
JAVA调用SAP WEBSERVICE服务实现流程图解
2023-11-07 03:54:57
![](https://img.aspxhome.com/file/2023/2/58992_0s.png)
Java利用POI读取、写入Excel的方法指南
2023-11-23 15:39:23
![](https://img.aspxhome.com/file/2023/9/59899_0s.png)
SpringMVC @RequestBody自动转json Http415错误的解决
2022-09-12 13:12:34
![](https://img.aspxhome.com/file/2023/0/63070_0s.jpg)