分页插件的使用 (1)简介:与 mybatis 的插件 pagehelper 用法类似。通过简单的配置即可使用。
(2)使用Step1:
配置分页插件。
编写一个 配置类,内部使用 @Bean 注解将 PaginationInterceptor 交给 Spring 容器管理。
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
?
/**
* 自定义一个配置类,mapper 扫描也可在此写上
*/
@Configuration
@MapperScan("com.zx.mybatis_plus.mapper")
public class Myconfig {
/**
* 分页插件
*
* @return 分页插件的实例
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//指定数据库类型是 MySQL
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
?
Step2:
编写分页代码。
直接 new 一个 Page 对象,对象需要传递两个参数(当前页,每页显示的条数)。
调用 mybatis-plus 提供的分页查询方法,其会将 分页查询的数据封装到 Page 对象中。
基本的方法如下
@Test
public void testPage() {
// Step1:创建一个 Page 对象
Page page = new Page();
// Page page = new Page(2, 5);
// Step2:调用 mybatis-plus 提供的分页查询方法
usersService.page(page, null);
// Step3:获取分页数据
System.out.println(page.getCurrent());
// 获取当前页
System.out.println(page.getTotal());
// 获取总记录数
System.out.println(page.getSize());
// 获取每页的条数
System.out.println(page.getRecords());
// 获取每页数据的集合
System.out.println(page.getPages());
// 获取总页数
System.out.println(page.hasNext());
// 是否存在下一页
System.out.println(page.hasPrevious());
// 是否存在上一页
}
跳转到页面的使用方法如下
@RequestMapping("list")
public String list(String rolecode,String rolename,Integer current,Model model){
if(current==null){
current=1;
}
Integer limit=2;
Integer total;
//总页数
List list=smbmsRoleService.list();
total=list.size()/limit;
if (list.size()%limit!=0){
total=list.size()/limit+1;
}else {
total=list.size()/limit;
}
//如果跳转的页面大于总页数就直接到最后一页
if (current>total){
current=total;
System.out.println(current);
}
//currenr当前页数
//limit页面 尺寸
//page分页信息
Page page = new Page(current, limit);
QueryWrapper wrapper =new QueryWrapper<>();
if(rolecode!=null&&rolecode!="null"){
wrapper.like("rolecode",rolecode);
}
if(rolename!=null&&rolename!="null"){
wrapper.like("rolename",rolename);
}
smbmsRoleService.page(page,wrapper);
model.addAttribute("rolecode", rolecode);
model.addAttribute("rolename", rolename);
model.addAttribute("current",current);
model.addAttribute("upage",page);
return "rolelist";
}
返回JOSN数据使用如下
// 分页查询 - mybatis-plus的方式
@GetMapping("/page")
public IPage findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam(defaultValuehttps://www.it610.com/article/= "") String username,
@RequestParam(defaultValuehttps://www.it610.com/article/= "") String email,
@RequestParam(defaultValuehttps://www.it610.com/article/= "") String address) {
IPage page = new Page<>(pageNum, pageSize);
QueryWrapper queryWrapper = new QueryWrapper<>();
if (!"".equals(username)) {
queryWrapper.like("username", username);
}
if (!"".equals(email)) {
queryWrapper.like("email", email);
}
if (!"".equals(address)) {
queryWrapper.like("address", address);
}
return sysUserService.page(page, queryWrapper);
}
【java|mybatis-plus分页插件的使用】
推荐阅读
- python|python setup.py install安装setuptools,pip出错,踩坑记录(下载,配置环境变量)
- 编程学习|Mybatis-Plus介绍与使用
- 谷粒商城|谷粒商城-day13-es和商品上架
- java基础学习|mybatis-plus的介绍和使用
- springboot框架|Springboot框架(2)--springboot的整合
- eclipse|Spring中的IOC和AOP是什么意思()
- Python实战教程|30天Python入门(第二天(深入了解Python中的变量和内置函数))
- LVS+KeepAlived高可用部署实战应用
- 游戏|【Unity】11.1 角色控制器 (Character Controller)