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 ...
随机推荐
- Mybatis 中的<![CDATA[ ]]>浅析
在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用<![CDATA[ ]]&g ...
- ctpn+crnn 训练数据集生成
1. https://github.com/Belval/TextRecognitionDataGenerator 2. https://textrecognitiondatagenerator.re ...
- python 23 继承
目录 继承--inheritance 1. 面向对象继承: 2. 单继承 2.1 类名执行父类的属性.方法 2.2 子类对象执行父类的属性.方法 2.3 执行顺序 2.4 既要执行子类的方法,又要执行 ...
- Python爬虫爬取全书网小说,程序源码+程序详细分析
Python爬虫爬取全书网小说教程 第一步:打开谷歌浏览器,搜索全书网,然后再点击你想下载的小说,进入图一页面后点击F12选择Network,如果没有内容按F5刷新一下 点击Network之后出现如下 ...
- Setup Factory 9 简单打包
由于项目资源太大,使用VS自带打包工具无法实现需求,所以Setup Factory 9进行打包生成多个文件的方案,下面记录使用方法: 一:这里点击下载:下载,提取码:tt7a 二:下载完安装需要注册码 ...
- EF的3种开发模式
那么明显开发模式是三种. 即:DateBase First(数据库优先).Model First(模型优先)和Code First(代码优先). 当然,如果把Code First模式的两种具体方式独立 ...
- map()函数映射
map()函数(映射) pattern = "abba" str = "dog cat cat dog" res=str.split() print(list( ...
- NLP(十四) 情感分析
情感在自然语言中的表达方式 例句 解释 I am very happy 开心的情感 She is so :( 表达悲伤的图标 import nltk import nltk.sentiment.sen ...
- [SNOI2019]字符串
名称:字符串 来源:2019年陕西省选 题目内容 传送门 洛谷(P5392) 题目描述 给出一个长度为$n$的由小写字母组成的字符串$a$,设其中第$i$个字符为$a_i(1≤i≤n)$. 设删掉第$ ...
- 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板
题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...