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",
                }
            }
        ]
    }
}
相关推荐
wkj00115 分钟前
QuaggaJS 配置参数详解
java·linux·服务器·javascript·quaggajs
异常君1 小时前
MyBatis 中 SqlSessionFactory 和 SqlSession 的线程安全性深度分析
java·面试·mybatis
crud1 小时前
Spring Boot 使用 spring-boot-starter-validation 实现优雅的参数校验,一文讲透!
java·spring boot
Dcs1 小时前
常见 GC 垃圾收集器对比分析
java
程序员岳焱1 小时前
Java高级反射实战:15个场景化编程技巧与底层原理解析
java·后端·编程语言
程序员小假1 小时前
说一说 Netty 中的心跳机制
java·后端
真实的菜1 小时前
消息队列处理模式:流式与批处理的艺术
java
盖世英雄酱581361 小时前
Java 内存管理技巧(新手必看集合篇)
java
码农小灰1 小时前
Java 8 Stream API 入门到实践详解
java·java案例
步、步、为营2 小时前
.NET 事件模式举例介绍
java·开发语言·.net