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",
                }
            }
        ]
    }
}
相关推荐
编程大师哥4 分钟前
vxe-table 透视表分组汇总及排序基础配置
java
8***848218 分钟前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
9***J62820 分钟前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端
M***Z21031 分钟前
SQL 建表语句详解
java·数据库·sql
v***79432 分钟前
Spring Boot 热部署
java·spring boot·后端
执笔论英雄32 分钟前
【RL】python协程
java·网络·人工智能·python·设计模式
galaxyffang43 分钟前
认证、会话管理、授权的区别
java
未名编程1 小时前
Windows 下如何部署 Nacos 并导入配置文件
java·windows
boonya1 小时前
Java中Plugin设计模式的规范应用
java·spring·设计模式·插件模式
杰克尼1 小时前
3. 分巧克力
java·数据结构·算法