没mybatisPlus坐标的可以导一下
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
selectPage的方法定义
/*** 根据 entity 条件,查询全部记录(并翻页)
** @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
* */
* IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
配置分页插件:
@Configuration
@MapperScan("cn.itcast.mp.mapper") //设置mapper接口的扫描
public class MybatisPlusPage {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
测试:
@Test
public void selectPage(){
Page<User> page = new Page<>(1,2);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.ge("age",20);
IPage<User> iPage = userMapper.selectPage(page, wrapper); //Preparing: SELECT COUNT(1) FROM tb_user WHERE (age >= ?)
List<User> records = iPage.getRecords();
for (User record : records) {
System.out.println(record);
}
System.out.println("当前页"+iPage.getCurrent());
System.out.println("总页数"+iPage.getPages());
System.out.println("总记录数"+iPage.getTotal());
System.out.println("每页显示条数"+iPage.getSize());
}