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",
                }
            }
        ]
    }
}
相关推荐
赵星星520几秒前
MySQL的默认隔离级别:为什么是可重复读(RR)而非读已提交(RC)?
java
用户2018792831671 分钟前
故事:公司的 "私人储物柜" 系统(ThreadLocalMap)
android·java
ling__i13 分钟前
java day18
java·开发语言
非ban必选14 分钟前
netty-scoket.io路径配置
java·服务器·前端
渣哥1 小时前
我和Java 8 Stream相爱相杀的那些年
java
爱吃烤鸡翅的酸菜鱼1 小时前
【Spring】原理解析:Spring Boot 自动配置
java·spring boot
小白兔3531 小时前
一文讲通Unicode规范、UTF-8与UTF-16编码及在Java中的验证
java
十八旬1 小时前
苍穹外卖项目实战(day7-1)-缓存菜品和缓存套餐功能-记录实战教程、问题的解决方法以及完整代码
java·数据库·spring boot·redis·缓存·spring cache
Java微观世界1 小时前
匿名内部类和 Lambda 表达式为何要求外部变量是 final 或等效 final?原理与解决方案
java·后端
SimonKing2 小时前
全面解决中文乱码问题:从诊断到根治
java·后端·程序员