MybatisPlus分页插件使用

一. 效果展示

二. 代码编写

2.1 pom

xml 复制代码
<dependency>
 	<groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

2.2 添加配置类

java 复制代码
@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;
    }
}

2.3 前端选择条件实体

java 复制代码
@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; 
}

2.4 mapper添加分页方法

java 复制代码
/**
 * 根据选择条件进行分页
 * @param page mybatus-plus提供的分页插件,必须位于第一个参数
 * @param approveGetDto 选择条件
 * @return vo对象
 */
Page<UserVo> selectPageVo(@Param("page") Page<UserVo> page,@Param("approveGetDto") ApproveGetDto approveGetDto);

2.5 sql映射文件

xml 复制代码
<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>

2.6 service

java 复制代码
Page<UserVo> findPageVo(ApproveGetDto approveGetDto);

2.7 serviceImpl

java 复制代码
@Override
public Page<UserVo> selectPageVo(ApproveGetDto approveGetDto) {
     Page<UserVo> page = new Page<UserVo>(approveGetDto.getCurrentPage,approveGetDto.getPageSize);
     userMapper.selectPageVo(page,approveGetDto);
     return page;
}

2.8 controller

java 复制代码
@GetMapping("/page")
public Result<Page<UserVo>> pageInfo(ApproveGetDto approveGetDto){
    Page<UserVo> userVo = userService.findPageVo(approveGetDto);
    return Result.success(userVo);
}

2.9 前端获取参数

js 复制代码
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'
       })
     }
   })
}

三. 参数

java 复制代码
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());//是否有上一页
相关推荐
胡耀超7 天前
1.分页查询(后端)—— Vue3 + SpringCloud 5 + MyBatisPlus + MySQL 项目系列(基于 Zulu 11)
mysql·spring·spring cloud·mybatis·mybatisplus·zulu11
u0100559602 个月前
IPage类与Page类区别和作用讲解
mybatis·mybatisplus
大飞哥~BigFei2 个月前
mybatisPlus的@TableLogic逻辑删除注解导致联合索引失效的坑
mysql·mybatisplus
装不满的克莱因瓶2 个月前
你真的懂Mybatis分页原理吗?
java·开发语言·架构·mybatis·mybatisplus·mybatis-plus·分页
是梦终空2 个月前
JAVA毕业设计153—基于Java+Springboot+小程序的校园维修管理系统小程序(源代码+数据库)
java·spring boot·小程序·毕业设计·layui·mybatisplus·校园维修管理系统
是梦终空2 个月前
JAVA毕业设计152—基于Java+Springboot+vue+小程序的个人健康管理系统小程序(源代码+数据库+15000字论文)
java·spring boot·vue·毕业设计·论文·mybatisplus·个人健康管理小程序
小言不爱说3 个月前
后端实现图片上传本地,可采用url查看图片
java·数据库·mybatisplus
CSDN专家-赖老师(软件之家)3 个月前
房屋出租管理系统小程序需求分析及功能介绍
小程序·人脸识别·springboot·uniapp·需求分析·mybatisplus·地图定位
是梦终空3 个月前
JAVA毕业设计146—基于Java+Springboot+vue+uniapp的景区旅游购票小程序(源代码+数据库+9000字论文)
java·spring boot·小程序·毕业设计·课程设计·mybatisplus·景区旅游门票小程序系统
&梧桐树夏3 个月前
MybatisPlus 使用教程
mybatisplus