Mybatis实现多对多关联组合查询

个人网站:http://xiaocaoshare.com/

1.需求

用户信息表、标签表、用户标签关联信息表

在做用户列表查询的时候,需要查询出该用户对应的用户标签

<resultMap id="BaseUserInfoResponseResultMap" type="com.miniapps.micro.model.response.UserInfoResponse">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="program_user_token" jdbcType="VARCHAR" property="programUserToken" />
  <result column="user_type" jdbcType="VARCHAR" property="userType" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="sex" jdbcType="VARCHAR" property="sex" />
  <result column="address" jdbcType="VARCHAR" property="address" />
  <result column="birthday" jdbcType="DATE" property="birthday" />
  <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  <result column="last_serviced_time" jdbcType="TIMESTAMP" property="lastServicedTime" />
  <result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime" />
  <result column="score" jdbcType="INTEGER" property="scoreTotal" />
  <collection property="tagList" ofType="com.miniapps.micro.model.base.UserTags">
    <id column="t_id" jdbcType="INTEGER" property="id" />
    <result column="tag_name" jdbcType="VARCHAR" property="tagName" />
  </collection>
</resultMap>

<select id="queryUserInfoByToken" resultMap="BaseUserInfoResponseResultMap">
  SELECT  t1.id,t1.program_user_token, t1.user_type,  t1.name,
  t1.sex,t1.address, t1.birthday, t1.create_time, t1.last_serviced_time, t1.last_login_time,
  t1.score,t3.id as t_id,t3.tag_name
  FROM `user_info` t1
  LEFT JOIN user_tag_relationship t2 on t1.id=t2.user_info_id
  LEFT JOIN user_tags t3 on t2.user_tag_id=t3.id
  where  t1.program_user_token = #{programToken}
  ORDER BY t1.create_time DESC
  LIMIT #{start}, #{pageSize}
</select>

考察知识:

1.Mybatis的关联组合查询

2、collection的用法

3.相同字段名的处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值