mybatis-plus 分页查询

方法1:

添加分页插件:

@Configuration
public class MybatisPlusPageConfig {
    /**
     * 分页插件配置
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor() ); // 乐观锁插件
        // DbType:数据库类型(根据类型获取应使用的分页方言)
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor( DbType.MYSQL)); // 分页插件
        return interceptor;
    }
}
//分页查询
    @GetMapping("/queryUserPage")
    List<User> queryUserPage(@RequestParam Long current,@RequestParam Long size){
        return userService.queryUserPage(current,size);
    }
 //分页查询,需要Mybatis-plus分页插件
    List<User> queryUserPage(Long current, Long size);
//分页查询,需要Mybatis-plus分页插件
    @Override
    public List<User> queryUserPage(Long current, Long size){
        Page<User> page = new Page<>(current,size);//(页码,条数)
        userMapper.selectPage(page,null);//第二个参数为条件构造器,如果为null,则查询所有
        List<User> list = page.getRecords();
        return list;
    }
//获取分页数据
        List<User> list = page.getRecords();
        list.forEach(System.out::println);
        System.out.println("当前页:"+page.getCurrent());
        System.out.println("每页显示的条数:"+page.getSize());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("总页数:"+page.getPages());
        System.out.println("是否有上一页:"+page.hasPrevious());
        System.out.println("是否有下一页:"+page.hasNext());

方法2

PageHelper插件分页查询
添加依赖:

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.5</version>
        </dependency>

XxxServiceImpl.java:Service CRUD 接口的实现

  @Override
    public PageInfo<SchoolStudent> getPageStudentFour(Integer current, Integer size) {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(current, size);
        List<SchoolStudent> list = this.list();
        //用PageInfo对结果进行包装
        PageInfo page = new PageInfo(list);
        return page;
    }```


参考:https://www.yii666.com/blog/566139.html

你可能感兴趣的:(A_mybatis-plus,mybatis-plus)