分页插件的使用
(1)简介:与 mybatis 的插件 pagehelper 用法类似。通过简单的配置即可使用。
(2)使用Step1:
配置分页插件。
编写一个 配置类,内部使用 @Bean 注解将 PaginationInterceptor 交给 Spring 容器管理。
import ;
import ;
import ;
import ;
import ;
/**
* 自定义一个配置类,mapper 扫描也可在此写上
*/
@Configuration
@MapperScan(".mybatis_plus.mapper")
public class Myconfig {
/**
* 分页插件
*
* @return 分页插件的实例
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//指定数据库类型是 MySQL
(new PaginationInnerInterceptor());
return interceptor;
}
}
Step2:
编写分页代码。
直接 new 一个 Page 对象,对象需要传递两个参数(当前页,每页显示的条数)。
调用 mybatis-plus 提供的分页查询方法,其会将 分页查询的数据封装到 Page 对象中。
基本的方法如下
@Test
public void testPage() {
// Step1:创建一个 Page 对象
Page<Users> page = new Page<Users>();
// Page<Users> page = new Page<Users>(2, 5);
// Step2:调用 mybatis-plus 提供的分页查询方法
(page, null);
// Step3:获取分页数据
(()); // 获取当前页
(()); // 获取总记录数
(()); // 获取每页的条数
(()); // 获取每页数据的集合
(()); // 获取总页数
(()); // 是否存在下一页
(()); // 是否存在上一页
}
跳转到页面的使用方法如下
@RequestMapping("list")
public String list(String rolecode,String rolename,Integer current,Model model){
if(current==null){
current=1;
}
Integer limit=2;
Integer total;//总页数
List<SmbmsRole> list=();
total=()/limit;
if (()%limit!=0){
total=()/limit+1;
}else {
total=()/limit;
}
//如果跳转的页面大于总页数就直接到最后一页
if (current>total){
current=total;
(current);
}
//currenr 当前页数
//limit 页面 尺寸
//page 分页信息
Page<SmbmsRole> page = new Page<SmbmsRole>(current, limit);
QueryWrapper<SmbmsRole> wrapper =new QueryWrapper<>();
if(rolecode!=null&&rolecode!="null"){
("rolecode",rolecode);
}
if(rolename!=null&&rolename!="null"){
("rolename",rolename);
}
(page,wrapper);
("rolecode", rolecode);
("rolename", rolename);
("current",current);
("upage",page);
return "rolelist";
}
返回JOSN数据使用如下
// 分页查询 - mybatis-plus的方式
@GetMapping("/page")
public IPage<SysUser> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam(defaultValue = "") String username,
@RequestParam(defaultValue = "") String email,
@RequestParam(defaultValue = "") String address) {
IPage<SysUser> page = new Page<>(pageNum, pageSize);
QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
if (!"".equals(username)) {
("username", username);
}
if (!"".equals(email)) {
("email", email);
}
if (!"".equals(address)) {
("address", address);
}
return (page, queryWrapper);
}