MybatisPlus分页插件使用

一. 效果展示

MybatisPlus分页插件使用_第1张图片

二. 代码编写

2.1 pom

<dependency>
 	<groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.4.2version>
dependency>

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;
    }
}

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; 
}

2.4 mapper添加分页方法

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

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>

2.6 service

Page<UserVo> findPageVo(ApproveGetDto approveGetDto);

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;
}

2.8 controller

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

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'
       })
     }
   })
}

三. 参数

MybatisPlus分页插件使用_第2张图片

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());//是否有上一页

你可能感兴趣的:(Mybatis,mybatisplus)