mybatis collection 子查询,嵌套查询,解决分页问题
生活随笔
收集整理的這篇文章主要介紹了
mybatis collection 子查询,嵌套查询,解决分页问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<?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_userlimit #{start},#{size}</select><select id="getUserListPageNum" resultType="java.lang.Long">selectcount(*)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) >= #{startTimestamp}</if><if test="endTimestamp != null and endTimestamp != 0 ">and UNIX_TIMESTAMP(su.gmt_create ) <= #{endTimestamp}</if></where></sql><select id="getUserDtoList1" resultMap="UserDtoMap">SELECTsu.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_codeFROMsys_user suleft join sys_user_organization suo on suo.user_id = su.idleft join sys_organization so on so.id = suo.organization_idleft join sys_role_user sru on sru.user_id = su.idleft join sys_role sr on sr.id = sru.role_idWHERE su.id IN (select temp.id from(SELECT su.* FROM sys_user suLEFT JOIN sys_user_organization suo ON suo.user_id = su.idLEFT JOIN sys_role_user sru ON sru.user_id = su.id<include refid="whereSql"/>GROUP BY su.idlimit #{start},#{size}) as temp)</select><!-- 子查詢方式 ,效率略低 --><select id="getUserDtoList" resultMap="UserDtoMapSelect">SELECTsu.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_deletedFROMsys_user suleft join sys_user_organization suo on suo.user_id = su.idleft join sys_organization so on so.id = suo.organization_idleft join sys_role_user sru on sru.user_id = su.idleft join sys_role sr on sr.id = sru.role_id<include refid="whereSql"/>group by su.idlimit #{start},#{size}</select><select id="getUserDtoListNum" resultType="java.lang.Integer">selectcount(DISTINCT(su.id))FROMsys_user suleft join sys_user_organization suo on suo.user_id = su.idleft 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">SELECTsr.*FROMsys_role_user sruleft join sys_role sr on sr.id = sru.role_idWHEREsru.user_id = #{userId}</select><select id="getOrganizationListByUserId" resultType="com.xx.springboot.business.system.domain.SysOrganizationDO">SELECTso.*FROMsys_user_organization suoLEFT JOIN sys_organization so on so.id = suo.organization_idWHEREsuo.user_id = #{userId}</select>
</mapper>
總結(jié)
以上是生活随笔為你收集整理的mybatis collection 子查询,嵌套查询,解决分页问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库完整性(实体完整性,参照完整性,用
- 下一篇: hp服务器性能测试,较新架构服务器用CP