当前位置: 首页 > news >正文

mybatis集合映射association与collection

官方文档:MyBatis的一对多关联关系

一、用途

一对一:association

一对多:collection

二、association

比较容易理解,可参考官方文档

三、collection

<?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="com.lyy.WaterMapper">
    <resultMap id="BaseResultMap" type="com.lyy.WaterVO">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="name" column="name"/>
        <collection property="solidList"
                    javaType="list"
                    ofType="com.lyy.SolidVO"
                    select="selectSolidList"
                    column="id">
        </collection>
        <collection property="gasList"
                    javaType="list"
             ofType="com.lyy.GasVO"
                    select="selectGasList"
                    column="id">
        </collection>
    </resultMap>

    <resultMap id="solidList" type="com.lyy.SolidVO">
        <collection property="solidList" javaType="list"
                    ofType="com.lyy.SolidVO"
                    select="selectSolidList" column="id">
        </collection>
    </resultMap>

    <resultMap id="gasList" type="com.lyy.GasVO">
        <collection property="gasList" javaType="list"
                    ofType="com.lyy.GasVO"
                    select="selectGasList" column="id">
        </collection>
    </resultMap>

    <select id="selectSolidList" resultMap="solidList">
        SELECT id,name,num
        FROM water w
        left join solid s on w.id=s.id
        WHERE w.id = #{id}
    </select>

    <select id="selectGasList" resultMap="gasList">
        SELECT id,name,size
        FROM water w
        left join gas g on w.id=g.id
        WHERE w.id = #{id}
    </select>
    //最终的使用
    <select id="getInfo" resultMap="BaseResultMap">
        SELECT *
        FROM water w
        left join c on c.id =w.id
        where w.id=#{id}
    </select>
</mapper>
实体类
public class SolidVO{
    Long id;
    String name;
    Long  num;
}

public class GasVO{
    Long id;
    String name;
    Long  size;
}

public class WaterVO{
    Long id;
    String name;
    List<SolidVO> solidList;
    List<GasVO> gasList;
}

注意:

1、column="id" 将当前查询结果中的ID作为参数,传递给子查询selectSolidList

2、几个对应关系

结果:

{
  "id": 30,
  "name": "盐酸",
  "solidList":[
    {
        "id": 1,
        "name": "xx",
        "num":"xx"
    },
     {
        "id": 5,
        "name": "xx",
        "num":"xx"
    },
  ],
 "gasList":[
    {
        "id": 4,
        "name": "xx",
        "size":"xx"
    },
     {
        "id": 5,
        "name": "xx",
        "size":"xx"
    },
  ]
      
}

相关文章:

  • 常用的遍历方法用途和运用
  • QT学习笔记1
  • 【在数轴上找最优位置,使移动距离最短】
  • 【区块链 + 商贸零售】商小萌小程序 | FISCO BCOS 应用案例
  • uniapp路由跳转导致页面堆积问题
  • 51单片机和STM32 入门分析
  • RSA后台解密报错:javax.crypto.BadPaddingException: Message is larger than modulus
  • 4.1--入门知识扫盲,ISO知识体系介绍(看一遍,协议啥的全部记住)
  • Android Zygote的进程机制
  • nginx配置txt文件点击链接后下载
  • 【ES6新特性】默认参数常见用法
  • (C语言)斐波那契数列(递归求解)
  • uniapp-x vue 特性
  • 通过 API 将Deepseek响应流式内容输出到前端
  • 论文精度:Transformers without Normalization
  • 提示词模板
  • KNN算法性能优化技巧与实战案例
  • vuex持久化存储,手动保存到localStorage,退出登录时清空vuex及localStorage
  • 【数据库】掌握MySQL事务与锁机制-数据一致性的关键
  • Vue:单文件组件
  • 阳光保险拟设立私募证券投资基金,总规模200亿元
  • 雷军内部演讲回应质疑:在不服输、打不倒方面,没人比我们更有耐心
  • 中央军委决定调整组建3所军队院校
  • 现场丨在胡适施蛰存等手札与文献间,再读百年光华
  • 全国人大常委会今年将初次审议检察公益诉讼法
  • 一个多月来上海交大接连“牵手”三区,在这些方面进行区校合作