mybatis都会用,但要优雅的用就不是那么容易了

今天就简单举例,抛砖引玉,供大家探讨

1.主表

  1. CREATE TABLE `test_one` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `nickname` varchar(255) NOT NULL,
  4. PRIMARY KEY (`id`)
  5. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

对应的java实体类如下(自动生成的代码,省略get set)

  1. @JsonIgnoreProperties(ignoreUnknown = true, value = {"handler"})
  2. public class TestOne implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4.  
  5. private Integer id;
  6.  
  7. private String nickname;
  8.  
  9. @JsonIgnoreProperties(ignoreUnknown = true, value = {"testOne"})
  10. private List<TestTwo> testTwos = new LinkedList<>();

注意:JsonIgnoreProperties请忽略,这是解决对象间循环依赖在json序列化时出错的,不在本次内容中

2.从表

  1. CREATE TABLE `test_two` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `nickname` varchar(255) NOT NULL,
  4. `one_id` int(11) NOT NULL,
  5. PRIMARY KEY (`id`),
  6. KEY `test_two_ibfk_1` (`one_id`),
  7. CONSTRAINT `test_two_ibfk_1` FOREIGN KEY (`one_id`) REFERENCES `test_one` (`id`)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

对应的java实体类如下(自动生成的代码,省略get set)

  1. @JsonIgnoreProperties(ignoreUnknown = true, value = {"handler"})
  2. public class TestTwo implements Serializable {
  3. private Integer id;
  4.  
  5. private String nickname;
  6.  
  7. private Integer oneId;
  8.  
  9. @JsonIgnoreProperties(ignoreUnknown = true, value = {"testTwos"})
  10. private TestOne testOne;

注意:JsonIgnoreProperties请忽略,这是解决对象间循环依赖在json序列化时出错的,不在本次内容中

细心的同学发现,两个表用同名字段,后续会告诉为什么这么举例,而且这种情况项目中是非常常见的

3.TestOneMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="info.zycloud.xcx.merchant.dao.TestOneMapper">
  4. <resultMap id="BaseResultMap" type="info.zycloud.xcx.merchant.model.TestOne">
  5. <constructor>
  6. <idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
  7. <arg column="nickname" javaType="java.lang.String" jdbcType="VARCHAR"/>
  8. </constructor>
  9. </resultMap>
  10.  
  11. <!--一次查询查出collection-->
        这里会一次查询就查询出主对象和关联的list对象, 查询语句是一个join语句
  12. <resultMap id="OnceQueryBaseResultMap" type="info.zycloud.xcx.merchant.model.TestOne" extends="BaseResultMap">
  13. <collection property="testTwos" resultMap="info.zycloud.xcx.merchant.dao.TestTwoMapper.BaseResultMap"
  14. columnPrefix="two_"/> 由于两个表有同名字段,所以需要做区分,这里可以采用前缀,就可以共用之前的ResultMap了
  15. </resultMap>

  16. <select id="onceQuery4Collection" resultMap="OnceQueryBaseResultMap">
  17. SELECT
  18. one.*,  为什么要用*,是为了防止主表字段变了,因为这里是引用的生成的baseresultMap
  19. two.id AS two_id,
  20. two.nickname AS two_nickname,
  21. two.one_id AS two_one_id
  22. FROM
  23. `test_one` one
  24. LEFT JOIN test_two two ON one.id = two.one_id
  25. </select>
  26.  
  27. <!-- 多次查询查出collection-->
  28. <resultMap id="MultipleQueryBaseResultMap" type="info.zycloud.xcx.merchant.model.TestOne" extends="BaseResultMap">
  29. <collection property="testTwos" column="{oneId=id,nickname=nickname}" 多参数时在column中用"{}"将参数包起来, =左侧的为mapper中定义的param, =右侧为主查询的数据库字段名  
  30. select="info.zycloud.xcx.merchant.dao.TestTwoMapper.selectByOneId"/>
  31. </resultMap>
  32.  
  33. <select id="multipleQuery4Collection" parameterType="java.lang.Integer" resultMap="MultipleQueryBaseResultMap">
  34. select
  35. <include refid="Base_Column_List"/>
  36. from test_one
  37. where id = #{id,jdbcType=INTEGER}
  38. </select>
  39.  
  40. </mapper>

对应的接口定义

  1. public interface TestOneMapper {
  2.  
  3. List<TestOne> onceQuery4Collection();
  4.  
  5. TestOne multipleQuery4Collection(Integer id);
  6. }

3.TestTwoMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="info.zycloud.xcx.merchant.dao.TestTwoMapper">
  4. <resultMap id="BaseResultMap" type="info.zycloud.xcx.merchant.model.TestTwo">
  5. <constructor>
  6. <idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
  7. <arg column="nickname" javaType="java.lang.String" jdbcType="VARCHAR"/>
  8. <arg column="one_id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
  9. </constructor>
  10. </resultMap>
  11.  
  12. <select id="selectByOneId" resultMap="BaseResultMap">
  13. select
  14. <include refid="Base_Column_List"/>
  15. from test_two
  16. where one_id=#{oneId} and nickname =#{nickname}
  17. </select>
  18. </mapper>
  1. public interface TestTwoMapper {
  2. List<TestTwo> selectByOneId(@Param("oneId") Integer oneId, @Param("nickname") String nickname);
  3. }

解释了然后我们执行看下效果:

onceQuery4Collection:

  1. multipleQuery4Collection

mybatis中collection association优化使用及多参数传递的更多相关文章

  1. Mybatis中使用association进行关联的几种方式

    这里以一对一单向关联为例.对使用或不使用association的配置进行举例.  实体类: @Data @ToString @NoArgsConstructor public class IdCard ...

  2. mybatis中collection和association的作用以及用法

    deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...

  3. Mybatis中collection和association的使用区别

    1. 关联-association2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ privat ...

  4. Mybatis中使用association及collection进行自关联示例(含XML版与注解版)

    XML版本: 实体类: @Data @ToString @NoArgsConstructor public class Dept { private Integer id; private Strin ...

  5. Mybatis中使用association及collection进行一对多双向关联示例(含XML版与注解版)

    XML版本: 实体类: package com.sunwii.mybatis.bean; import java.util.ArrayList; import java.util.List; impo ...

  6. Mybatis中 collection 和 association 的区别

    public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...

  7. Mybatis中 collection 和 association 的区别?

    public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...

  8. Mybatis中collection与association的区别

    association是多对一的关系 collection是一个一对多的关系

  9. myBatis中 collection 或 association 联合查询 中column 传入多个参数值

    下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap"  type="com.maidan.daas.entit ...

随机推荐

  1. Mybatis 中的<![CDATA[ ]]>浅析

    在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用<![CDATA[ ]]&g ...

  2. ctpn+crnn 训练数据集生成

    1. https://github.com/Belval/TextRecognitionDataGenerator 2. https://textrecognitiondatagenerator.re ...

  3. python 23 继承

    目录 继承--inheritance 1. 面向对象继承: 2. 单继承 2.1 类名执行父类的属性.方法 2.2 子类对象执行父类的属性.方法 2.3 执行顺序 2.4 既要执行子类的方法,又要执行 ...

  4. Python爬虫爬取全书网小说,程序源码+程序详细分析

    Python爬虫爬取全书网小说教程 第一步:打开谷歌浏览器,搜索全书网,然后再点击你想下载的小说,进入图一页面后点击F12选择Network,如果没有内容按F5刷新一下 点击Network之后出现如下 ...

  5. Setup Factory 9 简单打包

    由于项目资源太大,使用VS自带打包工具无法实现需求,所以Setup Factory 9进行打包生成多个文件的方案,下面记录使用方法: 一:这里点击下载:下载,提取码:tt7a 二:下载完安装需要注册码 ...

  6. EF的3种开发模式

    那么明显开发模式是三种. 即:DateBase First(数据库优先).Model First(模型优先)和Code First(代码优先). 当然,如果把Code First模式的两种具体方式独立 ...

  7. map()函数映射

    map()函数(映射) pattern = "abba" str = "dog cat cat dog" res=str.split() print(list( ...

  8. NLP(十四) 情感分析

    情感在自然语言中的表达方式 例句 解释 I am very happy 开心的情感 She is so :( 表达悲伤的图标 import nltk import nltk.sentiment.sen ...

  9. [SNOI2019]字符串

    名称:字符串 来源:2019年陕西省选 题目内容 传送门 洛谷(P5392) 题目描述 给出一个长度为$n$的由小写字母组成的字符串$a$,设其中第$i$个字符为$a_i(1≤i≤n)$. 设删掉第$ ...

  10. 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板

    题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...