5 Mybatis Plus自定义分页查询

自定义SQL分页查询

1、Mapper接口方法自定义

public interface RentSetMealMapper extends BaseMapper<RentSetMeal> {

    /**
     * 自定义连表查询,包含分页
     * @param page
     * @param rentSetMealDto
     * @param marketParkingId
     * @return
     */
    Page<RentSetMealData> getPageAll(
    	IPage<RentSetMealData> page,
    	@Param("rentSetMealDto") RentSetMealDto rentSetMealDto, 
    	@Param("marketParkingId") List<Long> marketParkingId);
    	}    

2、Mapper.xml中进行相应sql语句编写

<?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="cn.com.hopson.hopsonone.park.mapper.platform.RentSetMealMapper">

    <select id="getPageAll" resultType="cn.com.hopson.hopsonone.park.domain.platform.data.RentSetMealData">
        SELECT rent.rent_set_meal_id,rent.rent_set_meal_no,rent.rent_set_meal_name,
        rent.months,rent.selling_price,rent.crowd,rent.type,rent.examine_status,rent.`status`,
        rule.rule_rent_name,rule.parking_name,rule.parking_id,rule.partition_name,rule.fixed_status
        FROM pf_rent_set_meal rent, pf_rule_rent rule
        WHERE rent.rule_rent_id=rule.rule_rent_id
        <if test="rentSetMealDto.rentSetMealName != null and rentSetMealDto.rentSetMealName != ''">
            AND (rent.rent_set_meal_name LIKE #{rentSetMealDto.rentSetMealName} or rent.rent_set_meal_no LIke
            #{rentSetMealDto.rentSetMealName})
        </if>
        <if test="rentSetMealDto.ruleRentName != null and rentSetMealDto.ruleRentName != ''">
            AND (rule.rule_rent_name LIKE #{rentSetMealDto.ruleRentName} or rule.rule_rent_no LIKE
            #{rentSetMealDto.ruleRentName})
        </if>
        <if test="rentSetMealDto.crowd != null and rentSetMealDto.crowd != ''">
            AND rent.crowd LIKE #{rentSetMealDto.crowd}
        </if>
        <if test="rentSetMealDto.status != null">
            AND rent.status = #{rentSetMealDto.status}
        </if>
        <if test="rentSetMealDto.type != null">
            AND rent.type = #{rentSetMealDto.type}
        </if>
        <if test="rentSetMealDto.partitionId != null">
            AND rule.partition_id = #{rentSetMealDto.partitionId}
        </if>
        <if test="rentSetMealDto.type != null">
            <if test="rentSetMealDto.type == 1">
                AND rule.fixed_status = 1
            </if>
            <if test="rentSetMealDto.type == 2">
                AND rule.fixed_status = 0
            </if>
        </if>
        <choose>
            <when test="rentSetMealDto.parkingId != null and rentSetMealDto.parkingId > 0">
                AND rule.parking_id = #{rentSetMealDto.parkingId}
            </when>
            <otherwise>
                <if test="marketParkingId != null and marketParkingId.size > 0">
                    AND rule.parking_id in
                    <foreach collection="marketParkingId" index="index" item="item" open="("
                             separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </otherwise>
        </choose>
        ORDER BY rent.create_time
    </select>
</mapper>                    

3、Service进行接口调用

        Page<RentSetMealData> page = new Page<>();
        if (rentSetMealDto.getPageNum() != null && rentSetMealDto.getPageSize() != null) {
            page.setCurrent(rentSetMealDto.getPageNum()).setSize(rentSetMealDto.getPageSize());
        }
        if (StringUtils.isNotBlank(rentSetMealDto.getRuleRentName())) {
            rentSetMealDto.setRuleRentName("%" + rentSetMealDto.getRuleRentName() + "%");
        }
        if (StringUtils.isNotBlank(rentSetMealDto.getRentSetMealName())) {
            rentSetMealDto.setRentSetMealName("%" + rentSetMealDto.getRentSetMealName() + "%");
        }
        if (StringUtils.isNotBlank(rentSetMealDto.getCrowd())) {
            rentSetMealDto.setCrowd("%" + rentSetMealDto.getCrowd() + "%");
        }
IPage<RentSetMealData> pageAll = rentSetMealMapper.getPageAll(page, rentSetMealDto, marketParkingId);
posted @   SweetBaby。  阅读(5947)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2019-08-31 dubbo实现原理
点击右上角即可分享
微信分享提示