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>
相关推荐
+VX:Fegn08959 分钟前
计算机毕业设计|基于springboot + vue图书管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
cq林志炫12 分钟前
MySQL 英文逗号隔开的数据如何模糊精确查询
mysql
杨云龙UP1 小时前
MySQL 8.0.x InnoDB 写入链路优化:Redo Log 与 Buffer Pool 扩容与缓冲区调优实战记录-20251029
linux·运维·数据库·sql·mysql
黄俊懿2 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——开启全局事务
java·数据库·spring·spring cloud·微服务·架构·架构师
我命由我123452 小时前
python-dotenv - python-dotenv 快速上手
服务器·开发语言·数据库·后端·python·学习·学习方法
繁星蓝雨2 小时前
Qt优雅的组织项目结构三(使用CMakeLists进行模块化配置)——————附带详细示例代码
开发语言·数据库·qt
Jerry.张蒙3 小时前
SAP业财一体化实现的“隐形桥梁”-价值串
大数据·数据库·人工智能·学习·区块链·aigc·运维开发
无名修道院3 小时前
DVWA 靶场搭建:Windows11(phpstudy 搭建)(步骤 + 截图 + 常见问题)
数据库·网络安全·渗透测试·靶场·php·dvwa·phpstudy
UCoding5 小时前
新能源技术面试 -- 给出一套mysql备份容灾方案
mysql·面试·主从
CodeAmaz5 小时前
MySQL 事务隔离级别详解
数据库·mysql·事务隔离级别