MybatisPlusd实现自定义SQL的多表多条件分页查询

前言

很久没使用过MybatisPlus了,如何使用MP原生的IPage实现多表多条件的分页查询成了一个拦路石,MP的BaseMapper中两种分页查询不太满足当前的需求,经过一番搜罗终于将这块石头踩在脚下。网上关于这块的资料不是很多(或者对于大多数人来说这根本不算问题),现在将实现的过程分享出来,希望帮助到更多像我这样的人





一、业务需求(局部)

前端需要展示的数据及筛选条件如下图

 展示数据中的主要数据从套装的主表中查询,套装类型字段从系统字典表查询,创建人从系统用户表中查询。

筛选条件中的创建人需要关联系统用户表进行模糊搜索




二、实现步骤

此处省略表结构、控制层等敏感及无关代码,仅展示相关的DTO、VO、Service、Mapper、xml文件




1.接收筛选条件的DTO

@Data
@Schema(description = "组件套装列表 DTO对象")
public class ComponentPkgListDTO extends BaseBeanConvert {
    /**
     * 组件套装名称
     */
    @Schema(description = "组件套装名称")
    private String name;
    /**
     * 套装类型
     */
    @Schema(description = "套装类型")
    private Long componentPkgType;
    /**
     * 组件套装状态 0-停用,1-启用
     */
    @Schema(description = "组件套装状态 0-停用,1-启用")
    private Integer componentPkgStatus;
    /**
     * 创建人名称
     */
    @Schema(description = "创建人名称")
    private String createdByName;
    /**
     * 创建时间范围-开始
     */
    @Schema(description = "创建时间范围-开始,pattern:yyyy-MM-dd HH:mm:ss")
    private String startTime;
    /**
     * 创建时间范围-结束
     */
    @Schema(description = "创建时间范围-结束,pattern:yyyy-MM-dd HH:mm:ss")
    private String endTime;

}




2.视图对象VO

@Data
@Schema(description = "组件套装 VO对象")
public class ComponentPkgVO extends BaseBeanConvert {
    /**
     * ID
     */
    @Schema(description = "ID")
    private Long id;
    /**
     * 组件套装编码
     */
    @Schema(description = "组件套装编码")
    private String componentPkgCode;
    /**
     * 组件套装名称
     */
    @Schema(description = "组件套装名称")
    private String name;
    /**
     * 组件套装状态 0-停用,1-启用
     */
    @Schema(description = "组件套装状态 0-停用,1-启用")
    private Integer componentPkgStatus;
    /**
     * 创建人
     */
    @Schema(description = "创建人")
    private String createdByName;
    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    private String createdTime;
    /**
     * 套装类型
     */
    @Schema(description = "套装类型")
    private String componentPkgType;

}

3.service实现

    /**
    * QueryRequest中主要包含要查询的页码和每页展示的数量
    */
    @Override
    public IPage<ComponentPkgVO> getComponentPkgs(QueryRequest request, ComponentPkgListDTO componentPkgPage) {
        log.debug("method enter param:{}", JacksonUtil.toJson(componentPkgPage));
        Page<ComponentPkgVO> page = new Page<>(request.getPage(), request.getSize());
        return componentPkgMapper.getComponentPkgs(page, componentPkgPage);
    }

4.Mapper实现

需要重点强调一下,使用自定义的查询对象一定要加上Mybatis的@Param注解,用于告知MP被修饰的对象为查询参数对象

补充一下:在Mapper中可以通过方法重载来复用xml中定义的查询条件

    /**
     * 组件套装分页查询
     *
     * @param page         分页条件
     * @param componentPkg 查询条件dto
     * @return 满足条件的集合
     */
    IPage<ComponentPkgVO> getComponentPkgs(Page<ComponentPkgVO> page, @Param("pkg") ComponentPkgListDTO componentPkg);

    /**
     * 组件套装不分页查询
     *
     * @param componentPkg 查询条件dto
     * @return 满足条件的集合
     */
    List<ComponentPkgVO> getComponentPkgs(@Param("pkg") ComponentPkgListDTO componentPkg);

5.XML实现

通过注解中配置的参数@Param("pkg")在xml文件中获取相关属性作为判断条件或筛选条件

    <select id="getComponentPkgs" resultType="cn.phsys.biz.api.vo.ComponentPkgVO"
            parameterType="cn.phsys.biz.api.dto.ComponentPkgListDTO">
        SELECT cp.id,cp.component_pkg_code,cp.name,cp.component_pkg_status, u.`username` AS
        createdByName,cp.created_time,dict.`name` componentPkgTypeName
        FROM `basic_component_pkg` cp
        LEFT JOIN sys_user u ON cp.`created_by` = u.`id`
        LEFT JOIN sys_dict dict ON cp.`component_pkg_type` = dict.`id`
        where cp.`is_deleted` = 0
        <if test="pkg.name != null and pkg.name != ''">
            and cp.name LIKE CONCAT('%',#{pkg.name},'%')
        </if>
        <if test="pkg.componentPkgType != null and pkg.componentPkgType > 0">
            and cp.`component_pkg_type` = #{pkg.componentPkgType}
        </if>
        <if test="pkg.componentPkgStatus != null">
            and cp.`component_pkg_status` = #{pkg.componentPkgStatus}
        </if>
        <if test="pkg.createdByName != null and pkg.createdByName != ''">
            and u.`username` LIKE CONCAT('%',#{pkg.createdByName},'%')
        </if>
        <if test="pkg.startTime != null">
            and cp.`created_time` &gt;= #{pkg.startTime}
        </if>
        <if test="pkg.endTime != null">
            and cp.`created_time` &lt;= #{pkg.endTime}
        </if>
        order by cp.`created_time` DESC
    </select>




总结

本文的核心点为Mybatis的@Param注解,其他都是陪衬。作者文采不好、水平不高,如有不准确的表述望各位读者海涵、斧正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值