mybatisplus mysql嵌套查询传入多个参数

背景:一个用户有多个角色,在获取用户列表的时候想查询该用户有哪些角色

有子查询:通过用户ID,查询角色信息,绝对路径为xxx.a

java 复制代码
<!-- 通过用户ID,查询角色信息-->
<select id="ida" type="接收user下的role的实体类">
    SELECT r.*
    FROM role r,
        user_role ur
    WHERE r.role_id = ur.role_id
      and ur.user_id IN (#{userId})
</select>

1传入一个参数

嵌套查询的接收数据映射

java 复制代码
<resultMap id="mapc" type="接收user以及user下的role 的嵌套实体类">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
      <!--        一对多关系并且传一个参数-->
       <collection property="roleList" ofType="接收user下的role的实体类"
                    select="xxx.a"
                     column="user_id">
        </collection>
    </resultMap>

则 嵌套查询语句:用户列表+用户有哪些角色:

java 复制代码
<select id="idb" resultMap="mapc">
    SELECT u.* 
    from user u
    LEFT JOIN user_role ur on u.user_id=ur.user_id
          <if test="userId != null and userId!= ''">
           and ur.user_id = #{userId}
        </if>
</select>

2如果传入多个参数

子查询,绝对路径为xxx.a

java 复制代码
<select id="ida" type="接收user下的role的实体类">
    SELECT r.*
    FROM role r,
       user_role ur
    WHERE r.role_id = ur.role_id
      and ur.user_id IN (#{userId})
    <if test="subId!= null and subId!= ''">
        and r.sub_id = #{subId}
    </if>
</select>

嵌套查询的接收数据映射

java 复制代码
<resultMap id="mapc" type="接收user以及user下的role 的嵌套实体类">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
      <!--        一对多关系并且传多个参数-->
        <collection property="roleList" ofType="接收user下的role的实体类"
                    select="xxx.a"
                    column= "{userId=user_id,subId=sub_id}"   >
        </collection>
    </resultMap>

则 嵌套查询语句:用户列表+用户有哪些角色:

java 复制代码
  <select id="idb" resultMap="mapc">
        SELECT u.*  
        from user u
        LEFT JOIN user_role ur on u.user_id=ur,user_id
            <if test="userId != null and userId!= ''">
               and u.user_id = #{userId}
            </if>
            <if test="subId!= null and subId != ''">
                and ur.sub_id = #{subId}
            </if>
    </select>
相关推荐
Brookty34 分钟前
【MySQL】JDBC编程
java·数据库·后端·学习·mysql·jdbc
_代号00737 分钟前
MySQL梳理一:整体架构概览
后端·mysql
先做个垃圾出来………1 小时前
SQL的底层逻辑解析
数据库·sql
码不停蹄的玄黓1 小时前
深入拆解MySQL InnoDB可重复读(RR)隔离级别:MVCC+临键锁如何「锁」住一致性?
数据库·mysql·可重复读
paopaokaka_luck2 小时前
基于SpringBoot+Vue的酒类仓储管理系统
数据库·vue.js·spring boot·后端·小程序
用户91453633083912 小时前
MySQL查询执行顺序:一张图看懂SQL是如何工作的
mysql
Lx3522 小时前
MySQL物化视图:预计算查询结果的定期刷新
sql·mysql·性能优化
Lx3522 小时前
Mysql死锁日志分析:事务逻辑冲突的排查技巧
sql·mysql·性能优化
薛晓刚3 小时前
哪个领域数据库最难替换?
数据库
RainbowSea3 小时前
14. MySQL 锁的详细说明
java·sql·mysql