Mybatis-plus添加分页插件

第一步

导入mybatis-plus和mysql依赖


   com.baomidou
   mybatis-plus-boot-starter
   3.5.2


   mysql
   mysql-connector-java

 创建配置类

@Configuration
public class MyBatisPlusConfig {


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

}

我在这里封装了翻页的参数,分别是当前页和每页展示的数量, 使用我们的参数类继承该类即可

@Data
public class BaseParam {

    /**
     * 默认查询第一页
     */
    private Long currentPage = 1L;

    /**
     * 默认每页查询10条数据
     */
    private Long pageSize = 10L;

}

参数都是Long类型

mapper层

Page findAll(Page page);

 mapper层对应的xml文件


        
        
        
        
        
    

    
        id,age,name,create_time,update_time
    

    

service层查询分页后的方法

@Override
    public Page findAll(Page page) {
        return userMapper.findAll(page);
    }

直接在controller层调用

@PostMapping ("select")
    public Page select(@RequestBody UserParam userParam){
        Page page = new Page<>(userParam.getCurrentPage(),userParam.getPageSize());
        Page list = userService.findAll(page);
        return list;
    }

list可以直接返回给前端

你可能感兴趣的:(mybatis,java,spring,boot)