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

Mybatis集合嵌套查询,三级嵌套

三个表:房间 + 玩家 + 玩家信息 

知识点:Mybatis中级联有关联(association)、集合(collection)、鉴别器(discriminator)三种。其中,association对应一对一关系、collection对应一对多关系、discriminator可以根据情况选择采用哪个类作为实例,关联不同的结果集。

参考文章:Mybatis级联:关联、集合和鉴别器的使用 - aaron_shu - 博客园

# room.java

private List<Player> player;

    public List<Player> getPlayer()
    {
        return player;
    }

    public void setPlayer(List<Player> player)
    {
        this.player = player;
    }
<resultMap type="Room" id="RoomResult">
        <id     property="roomId"       column="room_id"      />
        <result property="roomTitle"     column="room_title"    />
        <result property="roomOwnerId"     column="room_owner_id"    />
		<result property="roomTags" column="room_tags" />
		<result property="scoreMode"  column="score_mode" />
		<result property="qrCode" column="qr_code" />
		<result property="appId" column="app_id" />
		<result property="isPlaten" column="is_platen" />
		<result property="userId" column="user_id" />
		<result property="shopId" column="shop_id" />
		<result property="status"       column="status"       />
		<result property="delFlag" 		column="del_flag" />
		<result property="remark"       column="remark"       />
		<result property="createBy"     column="create_by"    />
		<result property="createTime"   column="create_time"  />
		<result property="updateBy"     column="update_by"    />
		<result property="updateTime"   column="update_time"  />
		<collection property="player"    javaType="ArrayList"  ofType="Player" resultMap="playerResult" />
	</resultMap>

	<resultMap id="playerResult" type="Player">
		<result property="roomId"    column="room_id"       />
		<result property="playerScore"     	column="player_score"    />
		<result property="isOnlooker"     	column="is_onlooker"    />
		<result property="userId"     		column="user_id"    />
		<association property="customerUser" javaType="CustomerUser"  resultMap="customerUserResult" />
	</resultMap>

	<resultMap id="customerUserResult" type="CustomerUser">
		<result property="userId"    	column="user_id"       />
		<result property="nickName"     column="nick_name"    />
		<result property="avatar"       column="avatar"       />
	</resultMap>
	
	<sql id="selectRoomVo">
        select d.room_id, d.room_title, d.room_owner_id, d.score_mode, d.room_tags, d.qr_code,
        d.app_id, d.is_platen, d.del_flag, d.user_id, d.shop_id,
        p.player_score, p.is_onlooker, p.user_id,
	    c.nick_name, c.avatar,
	    d.remark, d.status, d.create_by, d.update_by, d.create_time, d.update_time
		from tb_room d
		left join tb_player p on p.room_id = d.room_id
		left join tb_customer_user c on c.user_id = p.user_id
	</sql>

前端得到的json结构

{
    "msg": "操作成功",
    "code": 200,
    "data": {
        "createBy": "",
        "createTime": "2025-03-06 12:15:09",
        "updateBy": "",,
        "delFlag": "1",
        "player": [
            {
                "createBy": null,
                "customerUser": {
                    "createBy": null,
                    "createTime": null, 
                    "avatar": "https://img1.baidu.com/it/u=2587875867,850432697&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800",
                }
            }
        ]
    }
}

相关文章:

  • JAVA与计算机网络基础
  • 生物信息学与计算生物学:各自概念、主要内容、区别与联系、发展展望?
  • 微服务的春天:基于Spring Boot的架构设计与实践
  • 如何把GUI做的像Web一样美观:Python PyQt6特性介绍,如何结合QSS美化
  • C++中`const` 和 `static` 关键字详解
  • 34.二叉树进阶3(C++STL 关联式容器,set/map的介绍与使用)
  • 【mysql系】mysql启动异常Can‘t create test file localhost.lower-test
  • 【大模型基础_毛玉仁】1.4 语言模型的采样方法
  • Excel中COUNTIF用法解析
  • 【笔记】记一次easyExcel中注解ExcelProperty映射字段赋值无效问题
  • dify 工作流 迭代
  • ArcGIS操作:15 计算点的经纬度,并添加到属性表
  • NDT 代价函数
  • 音视频入门基础:RTP专题(15)——FFmpeg源码中,获取RTP的视频信息的实现
  • K8S学习之基础十一:k8s中容器钩子
  • 日新F1、瑞研F600P 干线光纤熔接(熔接损耗最大0.03DB)
  • three学习记录
  • 秋云 ucharts echarts 高性能跨全端图表组件导入
  • P1443 马的遍历(BFS)
  • 企业日常工作中常用的 Linux 操作系统命令整理
  • 做外贸的都有那些网站/武汉网站排名推广
  • 网站的花费/百度首页登录官网
  • 什么网站做简历免费下载/上海今天刚刚发生的新闻
  • 微网站 html/友链交换网站源码
  • wordpress文章没办法显示略缩图/网站seo优化总结
  • 南京制作企业网站/网络营销考试题目及答案2022