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

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"
    },
  ]
      
}


http://www.kler.cn/a/591773.html

相关文章:

  • WebForms HTML:深入理解与高效应用
  • RS-232与TTL、CMOS的区别
  • 软件工程:数据字典
  • Spring Bean 生命周期深度解析:原理、场景与优化策略
  • Java List 接口的核心 API
  • 【区块链+乡村振兴】国经安农信链服务平台 | FISCO BCOS 应用案例
  • HarmonyOS三层架构实战
  • 算法刷题记录——LeetCode篇(6) [第501~600题](持续更新)
  • 前端安全之DOMPurify基础使用
  • pytorch小记(十三):pytorch中`nn.ModuleList` 详解
  • 【华为OD-E卷 - 单词接龙 100分(python、java、c++、js、c)】
  • linux系统 Ubuntu22.04安装Nvidia驱动,解决4060系列显卡重启黑屏方法
  • 【QA】工厂模式在Qt有哪些应用?
  • 基于传感器数据的城市空气质量预测与污染源分类
  • 用hexo初始化博客执行hexo init时碰到的问题
  • Linux的Shell编程
  • 学习网络层
  • 手搓智能音箱——语音识别及调用大模型回应
  • 如何用AI轻松制作完美PPT,节省时间又提升效率
  • Python基础语法全解析:从入门到实践