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>
相关推荐
TDengine (老段)4 分钟前
TDengine 特殊函数 MODE() 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
没学上了5 分钟前
数据库的连接_qt
数据库
好名字更能让你们记住我8 分钟前
MYSQL数据库初阶 之 MYSQL用户管理
linux·数据库·sql·mysql·adb·数据库开发·数据库架构
网硕互联的小客服9 分钟前
Mysql服务无法启动,显示错误1067如何处理?
数据库·mysql
哥哥还在IT中10 分钟前
mysql分库分表数据量核查问题
数据库·mysql·adb
paid槮10 分钟前
MySql基础:数据类型
android·mysql·adb
黎相思20 分钟前
MySQL在Centos 7环境下安装
数据库·mysql·centos
vxtkjzxt88827 分钟前
手机群控平台的核心功能
数据库·数据仓库
TG_yilongcloud32 分钟前
阿里云国际代理:怎么保障数据库在凭据变更过程中的安全与稳定?
数据库·安全·阿里云·云计算
恣艺1 小时前
Redis有序集合(ZSet):排行榜功能的最优解,原理与实战
数据库·chrome·redis