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>
相关推荐
2501_9411113411 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
q***718515 小时前
MySQL--》如何在MySQL中打造高效优化索引
android·mysql·adb
程序员卷卷狗16 小时前
MySQL 高可用方案:主从 + MHA + ProxySQL + PXC 的实战应用与架构思考
数据库·mysql·架构
千千寰宇17 小时前
[数据库/数据结构] LSM-Tree :结构化的日志合并树——NewSQL数据库的基石
数据库
韩立学长18 小时前
基于Springboot的研学旅游服务系统5u416w14(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·旅游
isNotNullX18 小时前
怎么理解ETL增量抽取?
数据库·数据仓库·etl·企业数字化
IT教程资源C18 小时前
(N_141)基于springboot,vue网上拍卖平台
mysql·vue·前后端分离·拍卖系统·springboot拍卖
谅望者18 小时前
数据分析笔记14:Python文件操作
大数据·数据库·笔记·python·数据挖掘·数据分析
l1t18 小时前
调用python函数的不同方法效率对比测试
开发语言·数据库·python·sql·duckdb