IPage分页查询—配置类 官网:https://baomidou.com/
package com.jt.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.jt.vo.EasyUITable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//1.标识配置类 配置类一般与@Bean注解联用!!!
@Configuration
public class MybatisPlusConfig {
//需要将对象交给容器管理Map集合 map<paginationInterceptor方法名,实例化之后的对象>
//Spring注入 1.按照类型注入 2.可以按照名字注入
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
IPage分页查询—代码(案例)
/**
* 在进行分页查询时,MP必须添加配置类
* 利用MP机制,实现商品查询
* @param page
* @param rows
* @return
*/
@Override
public EasyUITable findItemByPage(Integer page, Integer rows) {
//查询条件根据更新时间进行排序.
QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("updated");
//当用户将参数传递之后,MP会自己执行分页操作后,将需要的数据进行封装.
//定义分页对象
IPage<Item> iPage = new Page<>(page,rows);
//根据分页对象执行数据库查询,之后获取其其他分页数据.
iPage = itemMapper.selectPage(iPage,queryWrapper);
//获取总记录数
int total = (int)iPage.getTotal();
//获取分页后的结果
List<Item> itemList = iPage.getRecords();
//封装返回值 返回
return new EasyUITable(total,itemList);
}