按照官网说明
IPage<UserVo> selectPageVo(IPage<?> page, Integer state);
// or (class MyPage extends Ipage<UserVo>{ private Integer state; })
MyPage selectPageVo(MyPage page);
// or
List<UserVo> selectPageVo(IPage<UserVo> page, Integer state);
<select id="selectPageVo" resultType="xxx.xxx.xxx.UserVo">
SELECT id,name FROM user WHERE state=#{state}
</select>
如果返回类型是 IPage 则入参的 IPage 不能为null,因为 返回的IPage == 入参的IPage; 如果想临时不分页,可以在初始化IPage时size参数传 <0 的值;
如果返回类型是 List 则入参的 IPage 可以为 null(为 null 则不分页),但需要你手动 入参的IPage.setRecords(返回的 List);
如果 xml 需要从 page 里取值,需要page.属性
获取
具体使用方法,在mybatisplus能正常使用的情况下,不需要引包,加入mybatisplus分页插件PaginationInnerInterceptor
@Configuration
public class MyBatisPlusConfig {
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
然后使用
public IPage<Product> selectByPage(ProductDTO productDTO) {
IPage<Product> page = new Page<>();
page.setCurrent(productDTO.getNowPage());
page.setSize(productDTO.getPageSize());
QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotEmpty(productDTO.getName())) {
queryWrapper.like("name", productDTO.getName());
}
page = productMapper.selectPage(page, queryWrapper);
return page;
}
结果:
{
"records": [
{
"id": 1,
"name": "苹果11",
"price": 10000,
"number": 23123
},
{
"id": 2,
"name": "苹果2",
"price": 10000,
"number": 111
},
{
"id": 3,
"name": "苹果3",
"price": 30000,
"number": 222
}
],
"total": 5,
"size": 3,
"current": 1,
"orders": [],
"optimizeCountSql": true,
"searchCount": true,
"countId": null,
"maxLimit": null,
"pages": 2
}