mybatis中collection association优化使用及多参数传递
mybatis都会用,但要优雅的用就不是那么容易了
今天就简单举例,抛砖引玉,供大家探讨
1.主表
CREATE TABLE `test_one` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nickname` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
对应的java实体类如下(自动生成的代码,省略get set)
@JsonIgnoreProperties(ignoreUnknown = true, value = {"handler"})
public class TestOne implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String nickname;
@JsonIgnoreProperties(ignoreUnknown = true, value = {"testOne"})
private List<TestTwo> testTwos = new LinkedList<>();
注意:JsonIgnoreProperties请忽略,这是解决对象间循环依赖在json序列化时出错的,不在本次内容中
2.从表
CREATE TABLE `test_two` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nickname` varchar(255) NOT NULL,
`one_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `test_two_ibfk_1` (`one_id`),
CONSTRAINT `test_two_ibfk_1` FOREIGN KEY (`one_id`) REFERENCES `test_one` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
对应的java实体类如下(自动生成的代码,省略get set)
@JsonIgnoreProperties(ignoreUnknown = true, value = {"handler"})
public class TestTwo implements Serializable {
private Integer id;
private String nickname;
private Integer oneId;
@JsonIgnoreProperties(ignoreUnknown = true, value = {"testTwos"})
private TestOne testOne;
注意:JsonIgnoreProperties请忽略,这是解决对象间循环依赖在json序列化时出错的,不在本次内容中
细心的同学发现,两个表用同名字段,后续会告诉为什么这么举例,而且这种情况项目中是非常常见的
3.TestOneMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="info.zycloud.xcx.merchant.dao.TestOneMapper">
<resultMap id="BaseResultMap" type="info.zycloud.xcx.merchant.model.TestOne">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<arg column="nickname" javaType="java.lang.String" jdbcType="VARCHAR"/>
</constructor>
</resultMap> <!--一次查询查出collection-->
这里会一次查询就查询出主对象和关联的list对象, 查询语句是一个join语句
<resultMap id="OnceQueryBaseResultMap" type="info.zycloud.xcx.merchant.model.TestOne" extends="BaseResultMap">
<collection property="testTwos" resultMap="info.zycloud.xcx.merchant.dao.TestTwoMapper.BaseResultMap"
columnPrefix="two_"/> 由于两个表有同名字段,所以需要做区分,这里可以采用前缀,就可以共用之前的ResultMap了
</resultMap>
<select id="onceQuery4Collection" resultMap="OnceQueryBaseResultMap">
SELECT
one.*, 为什么要用*,是为了防止主表字段变了,因为这里是引用的生成的baseresultMap
two.id AS two_id,
two.nickname AS two_nickname,
two.one_id AS two_one_id
FROM
`test_one` one
LEFT JOIN test_two two ON one.id = two.one_id
</select> <!-- 多次查询查出collection-->
<resultMap id="MultipleQueryBaseResultMap" type="info.zycloud.xcx.merchant.model.TestOne" extends="BaseResultMap">
<collection property="testTwos" column="{oneId=id,nickname=nickname}" 多参数时在column中用"{}"将参数包起来, =左侧的为mapper中定义的param, =右侧为主查询的数据库字段名
select="info.zycloud.xcx.merchant.dao.TestTwoMapper.selectByOneId"/>
</resultMap> <select id="multipleQuery4Collection" parameterType="java.lang.Integer" resultMap="MultipleQueryBaseResultMap">
select
<include refid="Base_Column_List"/>
from test_one
where id = #{id,jdbcType=INTEGER}
</select> </mapper>
对应的接口定义
public interface TestOneMapper {
List<TestOne> onceQuery4Collection();
TestOne multipleQuery4Collection(Integer id);
}
3.TestTwoMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="info.zycloud.xcx.merchant.dao.TestTwoMapper">
<resultMap id="BaseResultMap" type="info.zycloud.xcx.merchant.model.TestTwo">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<arg column="nickname" javaType="java.lang.String" jdbcType="VARCHAR"/>
<arg column="one_id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
</constructor>
</resultMap> <select id="selectByOneId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from test_two
where one_id=#{oneId} and nickname =#{nickname}
</select>
</mapper>
public interface TestTwoMapper {
List<TestTwo> selectByOneId(@Param("oneId") Integer oneId, @Param("nickname") String nickname);
}
解释了然后我们执行看下效果:
onceQuery4Collection:

multipleQuery4Collection:

mybatis中collection association优化使用及多参数传递的更多相关文章
- Mybatis中使用association进行关联的几种方式
这里以一对一单向关联为例.对使用或不使用association的配置进行举例. 实体类: @Data @ToString @NoArgsConstructor public class IdCard ...
- mybatis中collection和association的作用以及用法
deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...
- Mybatis中collection和association的使用区别
1. 关联-association2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ privat ...
- Mybatis中使用association及collection进行自关联示例(含XML版与注解版)
XML版本: 实体类: @Data @ToString @NoArgsConstructor public class Dept { private Integer id; private Strin ...
- Mybatis中使用association及collection进行一对多双向关联示例(含XML版与注解版)
XML版本: 实体类: package com.sunwii.mybatis.bean; import java.util.ArrayList; import java.util.List; impo ...
- Mybatis中 collection 和 association 的区别
public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...
- Mybatis中 collection 和 association 的区别?
public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...
- Mybatis中collection与association的区别
association是多对一的关系 collection是一个一对多的关系
- myBatis中 collection 或 association 联合查询 中column 传入多个参数值
下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap" type="com.maidan.daas.entit ...
随机推荐
- 章节十六、4-TestNG高级功能--把测试方法分优先级、分组执行
一. 把测试方法分优先级执行----->(priority=索引) 1.新建一个testng方法 package testclasses; import org.testng.annotatio ...
- MapDB使用入门
背景 MapDB官网:http://www.mapdb.org 官方翻译之后的话:MapDB基于堆外存储.磁盘存储提供了Java的Maps.Sets.Lists.Queues等功能.它混合了Java集 ...
- 正则表达式-Regex详解
1.什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑.给定一个正 ...
- Python连载35-死锁问题以及解决方式
一.死锁问题 例子 import threading import time lock_1 = threading.Lock() lock_2 = threading.Lock() def f ...
- javascript JS CryptoJS DES加解密CBC模式与C#DES加解密相同互通
我们只知道不同的语言解密要相互通用,就需要遵循相同的加密方式,然而在具体做技术预研的时候,就发现会遇到很多问题,网上找的资料也是比较片面,所以我踩了坑,并且把解决方案和相关资料源码提供出来,给需要的朋 ...
- 机器学习性能度量指标:ROC曲线、查准率、查全率、F1
错误率 在常见的具体机器学习算法模型中,一般都使用错误率来优化loss function来保证模型达到最优. \[错误率=\frac{分类错误的样本}{样本总数}\] \[error=\frac{1} ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- Codeforces Round #480 (Div. 2) B. Marlin
题目地址:http://codeforces.com/contest/980/problem/B 官方题解: 题意: 有一个城市有4行n列,n是奇数,有一个村庄在(1,1),村民在(4,n)钓鱼:还有 ...
- lightoj 1125 - Divisible Group Sums (dp)
Given a list of N numbers you will be allowed to choose any M of them. So you can choose in NCM ways ...
- CF 990D Graph And Its Complement 第十八 构造、思维
Graph And Its Complement time limit per test 2 seconds memory limit per test 256 megabytes input sta ...