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
0
投稿
猜你喜欢
- 很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,
- 自定义Starter命名规则注意artifactId的命名规则,Spring官方Starter通常命名为spring-boot-starte
- Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,它有两种参数形式: public static
- UI 妹纸又给了个图叫我做,我一看是这样的:我们首先把这个控件划分成 几个部分:1.底下部分的直线 :2.左右两边的半圆
- 一、获取企业微信群机器人 Webhook 地址业务需要在企业微信推送告警监控或者定时提醒业务,就可以使用企业微信自带的机器人工具Webhoo
- 这两天因为要做一个随机的地图生成系统,所以一直在研究随机迷宫生成算法,好吧,算是有一点小小的成果。随机迷宫生成我自己的理解简而言之分为以下几
- 一、背景假如:黑客黑进了数据库,或者离职人员导出了数据,那么就可能导致这些敏感数据的泄漏。因此我们就需要找到一种方法来解决这个问题。二、解决
- 一、统一全站字符编码通过配置参数charset指明使用何种字符编码,以处理Html Form请求参数的中文问题package me.gacl
- jvm虚拟机栈的作用jvm虚拟机栈栈帧的组成jvm虚拟机栈,也叫java栈,它由一个个的栈帧组成,而栈帖由以下几个部分组成局部变量表-存储方
- SQLite是Android自带的关系型数据库,是一个基于文件的轻量级数据库。Android提供了3种操作数据的方式,SharedPrefe
- 1.首先要导入json相关的jar包引入的jar包:(版本自行定义,可以选用使用人数偏多的版本,这样比较稳定)commons-beanuti
- 1、在启动线程时,为什么要通过调用方法start执行方法run,而不能直接执行方法run?调用方法start执行方法run,才是多线程的工作
- 异步方法很好的解决了这些问题,异步执行某个方法,程序立即开辟一个新线程去运行你的方法,主线程包括界面就不会死掉了。异步如何开始,好理解,现在
- HTTP请求,在日常开发中,还是比较常见的,今天给大家分享HttpUtils如何使用。阅读本文,你将收获:简单总结HTTP请求常用配置;Ja
- 读取本地的xml文件,通过DOM进行解析,DOM解析的特点就是把整个xml文件装载入内存中,形成一颗DOM树形结构,树结构是方便遍历和和操纵
- java 数据类型:在Java中,数据类型分为两大种:基本数据类型(值类型)和包装类型(引用数据类型)。基本数据类型不是对象,不能调用toS
- 前端页面功能模块化拆分当一个系统的功能很多时,不可能所有功能模块的页面都写在一个页面里面,这时就需要将不同功能模块的页面拆分出去,就像模板一
- Bean Searcher 号称 任何复杂的查询都可以 一行代码搞定,但 Mybatis Plus 似乎也有类似的动态查询功能,它们有怎样的
- if判断integer的问题昨天在使用mybatis的if判断integer时遇见一个小问题:<if test="isCho
- 前言假设项目打包后,项目结构为:此时如果需要再windows环境中进行项目的启动或关闭,需要频繁的手敲命令,很不方便。此时可以编写.bat脚