• MybatisPlus分页插件使用


    一. 效果展示

    在这里插入图片描述

    二. 代码编写

    2.1 pom
    <dependency>
     	<groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.4.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.2 添加配置类
    @Configuration
    @MapperScan("scan.your.mapper.package")
    public class MybatisPlusConfig {
    
        /**
         * 添加分页插件
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    2.3 前端选择条件实体
    @Data
    public class ApproveGetDto {
        // 导入开始时间
        private String startTime;
        // 导入结束时间
        private String endTime;
        // 审批状态
        private Integer approveStatus;
        // 所属分部
        private List<String> division;
        // 人员范围
        private String personScope;
        // 导入人工号
        private String importPernr;
        // 离职员工工号
        private String quitPernr;
        // 当前页码
        private Integer currentPage;
        // 每页展示条数
        private Integer pageSize; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    2.4 mapper添加分页方法
    /**
     * 根据选择条件进行分页
     * @param page mybatus-plus提供的分页插件,必须位于第一个参数
     * @param approveGetDto 选择条件
     * @return vo对象
     */
    Page<UserVo> selectPageVo(@Param("page") Page<UserVo> page,@Param("approveGetDto") ApproveGetDto approveGetDto);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    2.5 sql映射文件
    <select id="selectPageVo"  resultMap="UserVo">
        select * from t_user
        <where>
            <if test="startTime != null and endTime != null">
                AND importTime between #{startTime} and #{endTime}
            if>
            <if test="division != null and division.size() > 0">
                AND division in
                <foreach item="item" collection="division" open="(" separator="," close=")">
                    #{item}
                foreach>
            if>
            <if test="personScope != null and personScope != ''">
                AND person_scope = #{personScope}
            if>
            <if test=" importPernr != null and importPernr != ''">
                AND originator_pernr = #{importPernr}
            if>
            <if test="quitPernr != null and quitPernr != '' ">
                AND quit_Pernr = #{quitPernr}
            if>
        where>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2.6 service
    Page<UserVo> findPageVo(ApproveGetDto approveGetDto);
    
    • 1
    2.7 serviceImpl
    @Override
    public Page<UserVo> selectPageVo(ApproveGetDto approveGetDto) {
         Page<UserVo> page = new Page<UserVo>(approveGetDto.getCurrentPage,approveGetDto.getPageSize);
         userMapper.selectPageVo(page,approveGetDto);
         return page;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    2.8 controller
    @GetMapping("/page")
    public Result<Page<UserVo>> pageInfo(ApproveGetDto approveGetDto){
        Page<UserVo> userVo = userService.findPageVo(approveGetDto);
        return Result.success(userVo);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.9 前端获取参数
    getList() {
       this.loading = true
       this.queryParams.currentPage = this.currentPage
       this.queryParams.pageSize = this.pageSize
       approveDataList(this.queryParams).then(response => {
         if (response.code == 200) {
           if (response.data.total == 0) {
             this.approveDataList = response.data.rows
             this.$alert('未查询到符合条件的数据,请检查查询条件后重试!', '查询结果', {
               confirmButtonText: '确定',
             })
             this.loading = false
           } else {
             // 获取总记录数
             this.totalCount = response.data.total
             // 获取当前页数据
             this.approveDataList = response.data.records
             this.$message({
               message: '查询成功',
               type: 'success'
             })
             this.loading = false
           }
         } else {
           this.loading = false
           this.approveDataList = []
           this.$message({
             message: '查询失败',
             type: 'erro'
           })
         }
       })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    三. 参数

    在这里插入图片描述

    System.out.println(page.getRecords());//获取分页记录
    System.out.println(page.getPages());//总页数
    System.out.println(page.getTotal());//总记录数
    System.out.println(page.hasNext());//是否有下一页
    System.out.println(page.hasPrevious());//是否有上一页
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    艾美捷QuickTiter 逆转录病毒定量试剂盒测定原理
    208道最常见的Java面试题整理(面试必备)
    Leetcode 1492.n的第k个因子
    数据结构——单链表
    设计模式:策略模式、工厂模式、模板模式混合使用
    golang设计模式——结构模式
    Maintaining leader role through timed lease mechanism
    sqli第24关二次注入
    系统软件开发基础知识
    【节能学院】数据机房中智能小母线与列头柜方案的对比分析
  • 原文地址:https://blog.csdn.net/weixin_46370595/article/details/132781847