【Mybatis-Plus】联表分页查询实现

参考文章:

https://blog.csdn.net/weixin_43847283/article/details/125822614

上上周写的SQL案例确实可以重构,所以搬到Demo里面测试看看

 

案例需要的依赖库

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.yulichang</groupId>
            <artifactId>mybatis-plus-join</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
    </dependencies>

  

这是原来的SQL脚本

SELECT 
  WX_USER.WEIXINID, 
  WX_USER.PHONENUM, 
  WX_USER.NICKNAME,
  WX_USER.HEADIMGURL, 
  WX_USER.UNIONID , 
  WX_USER.CREATETIME, 
  WX_USER.WEIXINID, 
  WX_USER.UPDATETIME, 
  MERCHANT.MERCHANT_NO, 
  MERCHANT.MERCHANT_NAME AS merchantName , 
  MERCHANT.PROVINCE AS province, 
  MERCHANT.CITY AS city, 
  MERCHANT.AREA AS areaCode 
FROM 
  `aisw_user` WX_USER 
  LEFT JOIN `aisw_merchant` MERCHANT ON WX_USER.MERCHANT_NO = MERCHANT.MERCHANT_NO 
WHERE 1 = 1
<if test="user.phonenum != null and user.phonenum != ''">
  AND WX_USER.PHONENUM LIKE CONCAT('%', #{user.phonenum},'%')
</if>
<if test="user.unionid != null and user.unionid != ''">
  AND WX_USER.UNIONID LIKE CONCAT('%', #{user.unionid},'%')
</if>
<if test="user.timeline0 != null and user.timeline0 != '' and user.timeline1 != null and user.timeline1 != ''">
  AND WX_USER.CREATETIME BETWEEN #{user.timeline0} AND #{user.timeline1}
</if> 
<if test="user.merchantName != null and user.merchantName != ''">
  AND ( 
    MERCHANT.MERCHANT_NO LIKE CONCAT('%', #{user.merchantName},'%') 
    OR 
    MERCHANT.MERCHANT_NAME LIKE CONCAT('%', #{user.merchantName},'%') 
  ) 
</if>
<if test="user.province != null and user.province != ''">
  AND MERCHANT.PROVINCE = #{user.province}
</if>
<if test="user.city != null and user.city != ''">
  AND MERCHANT.CITY = #{user.city}
</if>
<if test="user.areaCode != null and user.areaCode != ''">
  AND MERCHANT.AREA = #{user.areaCode}
</if>
ORDER BY WX_USER.CREATETIME DESC

  

Dao调用代码:

    /**
     * /aiswUser/queryUserPage
     * @description: 查询aisw用户列表
     * @author: cloud9
     * @date: 2022/06/24 11:31
     * @param dto
     * @return: cn.ymcd.comm.page.PageResult<cn.ymcd.aisw.common.strategy.dto.UserDTO>
     **/
    @PostMapping(value = "/queryUserPage")
    public IPage<UserDTO> queryUserPage(@RequestBody UserDTO dto) {
        IPage iPage = userDAO.queryUserPageWithMerchantInfo(dto.getPage(), dto);
        return iPage;
    }

 

换成MPJMapper实现:

    @PostMapping(value = "/queryUserPage2")
    public IPage<UserDTO> queryUserPage2(@RequestBody UserDTO dto) {
        IPage<UserDTO> page = userDAO.selectJoinPage(
            dto.getPage(),
            UserDTO.class,
            new MPJLambdaWrapper<UserDTO>()
                .selectAll(UserDTO.class)
                .selectAs(AiswMerchantDTO::getMerchantName, UserDTO::getMerchantName)
                .selectAs(AiswMerchantDTO::getProvince, UserDTO::getProvince)
                .selectAs(AiswMerchantDTO::getCity, UserDTO::getCity)
                .selectAs(AiswMerchantDTO::getArea, UserDTO::getAreaCode)
                .leftJoin(AiswMerchantDTO.class, AiswMerchantDTO::getMerchantNo, UserDTO::getMerchantNo)
                .like(!StringUtils.isEmpty(dto.getUnionid()), UserDTO::getUnionid, dto.getUnionid())
                .like(!StringUtils.isEmpty(dto.getPhonenum()), UserDTO::getPhonenum, dto.getPhonenum())
                .and(
                    !StringUtils.isEmpty(dto.getMerchantName()),
                    wq -> wq
                        .like(AiswMerchantDTO::getMerchantName, dto.getMerchantName())
                        .or()
                        .like(AiswMerchantDTO::getMerchantNo, dto.getMerchantName())
                )
                .between(
                    !StringUtils.isEmpty(dto.getTimeline0()) && !StringUtils.isEmpty(dto.getTimeline1()),
                    UserDTO::getTimeline, dto.getTimeline0(), dto.getTimeline1()
                )
                .eq(!StringUtils.isEmpty(dto.getProvince()), AiswMerchantDTO::getProvince, dto.getProvince())
                .eq(!StringUtils.isEmpty(dto.getCity()), AiswMerchantDTO::getCity, dto.getCity())
                .eq(!StringUtils.isEmpty(dto.getAreaCode()), AiswMerchantDTO::getProvince, dto.getAreaCode())
                .orderByDesc(UserDTO::getCreatetime)
        );
        return page;
    }

  

 

两者的SQL比较:

-- MPJ生成SQL
SELECT
	t.ID,
	t.WEIXINID,
	t.PHONENUM,
	t.NICKNAME,
	t.PROVINCE,
	t.PLACECITY,
	t.PLACEAREA,
	t.HEADIMGURL,
	t.UNIONID,
	t.CREATETIME,
	t.UPDATETIME,
	t.TIMELINE,
	t.MERCHANT_NO,
	t1.MERCHANT_NAME AS merchantName,
	t1.PROVINCE AS province,
	t1.CITY AS city,
	t1.AREA AS areaCode 
FROM
	aisw_user t
	LEFT JOIN aisw_merchant t1 ON ( t1.MERCHANT_NO = t.MERCHANT_NO ) 
WHERE
	( t.TIMELINE BETWEEN '2022-07-13 00:00:00' AND '2022-08-23 23:59:59' ) 
ORDER BY
	t.CREATETIME DESC
	
-- 我写的SQL
SELECT
	WX_USER.WEIXINID,
	WX_USER.PHONENUM,
	WX_USER.NICKNAME,
	WX_USER.HEADIMGURL,
	WX_USER.UNIONID,
	WX_USER.CREATETIME,
	WX_USER.WEIXINID,
	WX_USER.UPDATETIME,
	MERCHANT.MERCHANT_NO,
	MERCHANT.MERCHANT_NAME AS merchantName,
	MERCHANT.PROVINCE AS province,
	MERCHANT.CITY AS city,
	MERCHANT.AREA AS areaCode 
FROM
	`aisw_user` WX_USER
	LEFT JOIN `aisw_merchant` MERCHANT ON WX_USER.MERCHANT_NO = MERCHANT.MERCHANT_NO 
WHERE
	1 = 1 
	AND WX_USER.CREATETIME BETWEEN '2022-07-13 00:00:00' 
	AND '2022-08-23 23:59:59' 
ORDER BY
	WX_USER.CREATETIME DESC

  

 

posted @ 2022-07-19 11:35  emdzz  阅读(662)  评论(0编辑  收藏  举报