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",
                }
            }
        ]
    }
}
相关推荐
身如柳絮随风扬1 小时前
Java中的CAS机制详解
java·开发语言
风筝在晴天搁浅3 小时前
hot100 78.子集
java·算法
故事和你914 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
Configure-Handler4 小时前
buildroot System configuration
java·服务器·数据库
:Concerto5 小时前
JavaSE 注解
java·开发语言·sprint
电商API_180079052475 小时前
第三方淘宝商品详情 API 全维度调用指南:从技术对接到生产落地
java·大数据·前端·数据库·人工智能·网络爬虫
一点程序6 小时前
基于SpringBoot的选课调查系统
java·spring boot·后端·选课调查系统
C雨后彩虹6 小时前
计算疫情扩散时间
java·数据结构·算法·华为·面试
2601_949809596 小时前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
vx_BS813306 小时前
【直接可用源码免费送】计算机毕业设计精选项目03574基于Python的网上商城管理系统设计与实现:Java/PHP/Python/C#小程序、单片机、成品+文档源码支持定制
java·python·课程设计