mybatis collection 子查询,嵌套查询,解决分页问题

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xx.springboot.business.system.dao.UserDao">


    <resultMap id="UserDtoMap" type="com.xx.springboot.business.system.dto.UserDto">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="open_id" property="openId" jdbcType="VARCHAR"/>
        <result column="user_code" property="userCode" jdbcType="VARCHAR"/>
        <result column="account" property="account" jdbcType="VARCHAR"/>
        <result column="salt" property="salt" jdbcType="VARCHAR"/>
        <result column="department_id" property="departmentId" jdbcType="VARCHAR"/>
        <result column="department_name" property="departmentName" jdbcType="VARCHAR"/>
        <result column="real_name" property="realName" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="BIGINT"/>
        <result column="sex" property="sex" jdbcType="BIGINT"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="id_card_num" property="idCardNum" jdbcType="VARCHAR"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
        <result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
        <result column="modified_user_id" property="modifiedUserId" jdbcType="BIGINT"/>
        <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
        <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
        <result column="is_deleted" property="isDeleted" jdbcType="TINYINT"/>
        <collection property="roleDOList" columnPrefix="sr_" resultMap="RoleMap"/>
        <collection property="organizationDOList" columnPrefix="so_" resultMap="OrganizationMap"/>
    </resultMap>

    <resultMap id="RoleMap" type="com.xx.springboot.business.system.domain.SysRoleDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="role_code" property="roleCode" jdbcType="VARCHAR"/>
        <result column="role_name" property="roleName" jdbcType="VARCHAR"/>
        <result column="role_desc" property="roleDesc" jdbcType="VARCHAR"/>
        <result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
        <result column="modified_user_id" property="modifiedUserId" jdbcType="BIGINT"/>
        <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
        <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
        <result column="is_deleted" property="isDeleted" jdbcType="TINYINT"/>
    </resultMap>

    <resultMap id="OrganizationMap" type="com.xx.springboot.business.system.domain.SysOrganizationDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="organization_code" property="organizationCode" jdbcType="VARCHAR"/>
        <result column="organization_name" property="organizationName" jdbcType="VARCHAR"/>
        <result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
        <result column="modified_user_id" property="modifiedUserId" jdbcType="BIGINT"/>
        <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
        <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
        <result column="is_deleted" property="isDeleted" jdbcType="TINYINT"/>
    </resultMap>

    <!--子查询map,解决分页问题-->
    <resultMap id="UserDtoMapSelect" type="com.xx.springboot.business.system.dto.UserDto">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="open_id" property="openId" jdbcType="VARCHAR"/>
        <result column="user_code" property="userCode" jdbcType="VARCHAR"/>
        <result column="account" property="account" jdbcType="VARCHAR"/>
        <result column="salt" property="salt" jdbcType="VARCHAR"/>
        <result column="department_id" property="departmentId" jdbcType="VARCHAR"/>
        <result column="department_name" property="departmentName" jdbcType="VARCHAR"/>
        <result column="real_name" property="realName" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="BIGINT"/>
        <result column="sex" property="sex" jdbcType="BIGINT"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="id_card_num" property="idCardNum" jdbcType="VARCHAR"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
        <result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
        <result column="modified_user_id" property="modifiedUserId" jdbcType="BIGINT"/>
        <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
        <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
        <result column="is_deleted" property="isDeleted" jdbcType="TINYINT"/>

        <collection property="roleDOList" column="id" select="getRoleListByUserId"/>
        <collection property="organizationDOList" column="id" select="getOrganizationListByUserId"/>
    </resultMap>

    <select id="getAll" resultType="com.xx.springboot.business.system.domain.SysUserDO">
        select * from sys_user;
    </select>

    <select id="getUserListPage" resultType="com.xx.springboot.business.system.domain.SysUserDO">
        select
        *
        from sys_user
        limit #{start},#{size}
    </select>

    <select id="getUserListPageNum" resultType="java.lang.Long">
        select
        count(*)
        from sys_user
    </select>

    <select id="getUserByAccount" resultType="com.xx.springboot.business.system.domain.SysUserDO">
         select * from sys_user where account = #{account}
    </select>

    <select id="selectEntityAndPage"
            parameterType="com.xx.springboot.business.system.domain.SysUserDO"
            resultType="com.xx.springboot.business.system.dto.UserDto">
        select *  from sys_user
    </select>

    <select id="selectUserRole" resultType="com.xx.springboot.business.system.dto.UserRoleDto">
          select *  from sys_user u,sys_role_user ru,sys_role r where u.id = ru.user_id and r.id = ru.role_id
    </select>

    <sql id="whereSql">
        <where>
            <if test="1==1 ">
                su.is_deleted = 0
            </if>
            <if test="organizationId != null and organizationId != '' ">
                and suo.organization_id = #{organizationId}
            </if>
            <if test="roleId != null and roleId != '' ">
                and sru.role_id = #{roleId}
            </if>

            <if test="account != null and account != '' ">
                and su.account = #{account}
            </if>
            <if test="realName != null and realName != '' ">
                and su.real_name = #{realName}
            </if>
            <if test="phone != null and phone != '' ">
                and su.phone = #{phone}
            </if>
            <if test="email != null and email != '' ">
                and su.email = #{email}
            </if>

            <if test="startTimestamp != null and startTimestamp != 0 ">
                and UNIX_TIMESTAMP(su.gmt_create) &gt;= #{startTimestamp}
            </if>
            <if test="endTimestamp != null and endTimestamp != 0 ">
                and UNIX_TIMESTAMP(su.gmt_create ) &lt;= #{endTimestamp}
            </if>
        </where>
    </sql>

    <select id="getUserDtoList1" resultMap="UserDtoMap">
        SELECT
        su.id as id,
        su.open_id,
        su.user_code,
        su.account,
        su.salt,
        su.department_id,
        su.department_name,
        su.real_name,
        su.age,
        su.sex,
        su.phone,
        su.id_card_num,
        su.email,
        su.address,
        su.create_user_id,
        su.modified_user_id,
        su.gmt_create,
        su.gmt_modified,
        su.is_deleted,

        sr.id as sr_id,
        sr.role_code as sr_role_code,
        sr.role_name as sr_role_name,
        sr.role_desc as sr_role_desc,

        so.id as so_id,
        so.organization_name as so_organization_name,
        so.organization_code as so_organization_code
        FROM
        sys_user su
        left join sys_user_organization suo on suo.user_id = su.id
        left join sys_organization so on so.id = suo.organization_id
        left join sys_role_user sru on sru.user_id = su.id
        left join sys_role sr on sr.id = sru.role_id
        WHERE su.id IN (
        select temp.id from
        (SELECT su.* FROM sys_user su
        LEFT JOIN sys_user_organization suo ON suo.user_id = su.id
        LEFT JOIN sys_role_user sru ON sru.user_id = su.id
        <include refid="whereSql"/>
        GROUP BY su.id
        limit #{start},#{size}) as temp
        )
    </select>

    <!-- 子查询方式 ,效率略低 -->
     <select id="getUserDtoList" resultMap="UserDtoMapSelect">
        SELECT
        su.id as id,
        su.open_id,
        su.user_code,
        su.account,
        su.salt,
        su.department_id,
        su.department_name,
        su.real_name,
        su.age,
        su.sex,
        su.phone,
        su.id_card_num,
        su.email,
        su.address,
        su.create_user_id,
        su.modified_user_id,
        su.gmt_create,
        su.gmt_modified,
        su.is_deleted
        FROM
        sys_user su
        left join sys_user_organization suo on suo.user_id = su.id
        left join sys_organization so on so.id = suo.organization_id
        left join sys_role_user sru on sru.user_id = su.id
        left join sys_role sr on sr.id = sru.role_id
        <include refid="whereSql"/>
        group by su.id
        limit #{start},#{size}
    </select>

    <select id="getUserDtoListNum" resultType="java.lang.Integer">
        select
        count(DISTINCT(su.id))
        FROM
        sys_user su
        left join sys_user_organization suo on suo.user_id = su.id
        left join sys_role_user sru on sru.user_id = su.id
        <include refid="whereSql"/>
    </select>

    <select id="getRoleListByUserId" resultType="com.xx.springboot.business.system.domain.SysRoleDO">
        SELECT
        sr.*
        FROM
            sys_role_user sru
        left join sys_role sr on sr.id = sru.role_id
        WHERE
	        sru.user_id = #{userId}
    </select>

    <select id="getOrganizationListByUserId" resultType="com.xx.springboot.business.system.domain.SysOrganizationDO">
        SELECT
	    so.*
        FROM
            sys_user_organization suo
        LEFT JOIN sys_organization so on so.id = suo.organization_id
        WHERE
	      suo.user_id = #{userId}
    </select>
</mapper>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis allows you to perform collection subqueries, which can be useful when you need to retrieve related data for a collection of records. Collection subqueries are typically used in scenarios where you have a one-to-many or many-to-many relationship between tables. To implement a collection subquery in MyBatis, you can use the `<collection>` element within your result mapping. Here's an example: ```xml <resultMap id="userMap" type="User"> <id property="id" column="user_id" /> <result property="name" column="user_name" /> <collection property="orders" ofType="Order"> <id property="id" column="order_id" /> <result property="product" column="product_name" /> </collection> </resultMap> ``` In this example, we have a `User` object that has a collection of `Order` objects. The `User` object has an `id` and a `name`, while the `Order` object has an `id` and a `product`. The `collection` element is used to define the mapping for the collection property (`orders`) of the `User` object. To perform the collection subquery, you can use the `<select>` element with a nested `<collection>` element in your SQL statement. Here's an example: ```xml <select id="getUserWithOrders" resultMap="userMap"> SELECT u.id AS user_id, u.name AS user_name, o.id AS order_id, o.product AS product_name FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE u.id = #{userId} </select> ``` In this example, we join the `users` and `orders` tables on the `user_id` column and retrieve the relevant columns for both the `User` and `Order` objects. The `<collection>` element is used to map the query results to the collection property (`orders`) of the `User` object. You can then use this mapping and SQL statement in your MyBatis mapper interface to retrieve a `User` object with its associated `Order` objects. I hope this explanation helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值