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>
相关推荐
掉头发的王富贵8 分钟前
ShardingSphere-JDBC入门教程(上篇)
spring boot·后端·mysql
小云数据库服务专线21 分钟前
GaussDB数据库架构师修炼(十六) 如何选择磁盘
数据库·数据库架构·gaussdb
码出财富1 小时前
SQL语法大全指南
数据库·mysql·oracle
异世界贤狼转生码农3 小时前
MongoDB Windows 系统实战手册:从配置到数据处理入门
数据库·mongodb
QuZhengRong3 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
码农阿豪3 小时前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
冷崖8 小时前
MySQL异步连接池的学习(五)
学习·mysql
时序数据说9 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
听雪楼主.12 小时前
Oracle Undo Tablespace 使用率暴涨案例分析
数据库·oracle·架构
我科绝伦(Huanhuan Zhou)12 小时前
KINGBASE集群日常维护管理命令总结
数据库·database