SpringBoot之整合PageHelper分页插件

SpringBoot之整合PageHelper分页插件

文章目录

  • SpringBoot之整合PageHelper分页插件
  • [1. 引入坐标](#1. 引入坐标)
  • [2. application.yml配置](#2. application.yml配置)
  • [3. 基本使用](#3. 基本使用)
  • [4. 对多个查询执行分页](#4. 对多个查询执行分页)
    • [1. 默认第一个Select语句会执行分页](#1. 默认第一个Select语句会执行分页)
    • [2. 让Pagehelper也能执行多个分页的方法](#2. 让Pagehelper也能执行多个分页的方法)
    • [3. 完整案例](#3. 完整案例)

详细配置请查看官网或MyBatis分页插件之PageHelper详细介绍-CSDN博客

1. 引入坐标

xml 复制代码
<!--pagehelper-->
			<dependency>
				<groupId>com.github.pagehelper</groupId>
				<artifactId>pagehelper-spring-boot-starter</artifactId>
				<version>1.3.0</version>
				<!--排除pagehelper的依赖mybatis和mybatis-spring的jar包以免与mybatis-plus的冲突,导致报NoClassFound org.mybatis.logging.LoggerFactory-->
				<exclusions>
					<exclusion>
						<groupId>org.mybatis</groupId>
						<artifactId>mybatis</artifactId>
					</exclusion>
					<exclusion>
						<groupId>org.mybatis</groupId>
						<artifactId>mybatis-spring</artifactId>
					</exclusion>
				</exclusions>
			</dependency>

2. application.yml配置

yaml 复制代码
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3. 基本使用

java 复制代码
    @Autowired
    private PublicService publicService;	   
    @GetMapping(value = "/getUserList")
    public Result<PageInfo> getUserList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                 @RequestParam(name="pageSize", defaultValue="5") Integer pageSize){
        StringBuffer sql = new StringBuffer();
         sql.append("SELECT\n" +
                " a.id,\n" +
                " a.username,\n" +
                " b.id AS file_id,\n" +
                " b.file_url,\n" +
                " b.file_size,\n" +
                "FROM\n" +
                " sys_user a\n" +
                " LEFT JOIN sys_file b ON a.id = b.parent_id \n" +
                "WHERE\n" +
                " a.del_flag = '0'");
        Result result = new Result<>();
        Map map = new HashMap(5);
        map.put("sql",sql.toString());
        //获取第pageNo页,pageSize条内容,默认查询总数count
        PageHelper.startPage(pageNo, pageSize);
        //紧跟着的第一个select方法会被分页
        List<Map<String, Object>> mapList = publicService.sqlQuery(map);
        result.setResult(mapList);
        result.setSuccess(true);
        PageInfo pageInfo = new PageInfo(mapList);
        return Result.OK(pageInfo);

4. 对多个查询执行分页

Pagehelper中只有紧跟在 PageHelper.startPage 方法后的第一个 Mybatis 的查询(Select)方法会被分页。

1. 默认第一个Select语句会执行分页

案例代码如下:

java 复制代码
 
@Autowired
private PublicService publicService;

public List<SignatureUser> getUserList(){
        //获取第pageNo页,pageSize条内容,默认查询总数count
        PageHelper.startPage(pageNo, pageSize);
        //紧跟着的第一个select方法会被分页
        List<Map<String, Object>> mapList = publicService.sqlQuery(map);
        IPage iPage = IPageUtil.pageData(mapList);
        //下面这个查询不会分页
        List<SignatureUser> signatureUserList = publicService.getSignatureUserList(map);
        System.out.println(signatureUserList.size());
        return signatureUserList;
}

2. 让Pagehelper也能执行多个分页的方法

在查询参数中设置pageNum与pageSize参数使其第二个查询也能分页,如下:

java 复制代码
@Autowired
private PublicService publicService; 

public List<SignatureUser> getUserList(){
        //获取第pageNo页,pageSize条内容,默认查询总数count
        PageHelper.startPage(pageNo, pageSize);
        //紧跟着的第一个select方法会被分页
        List<Map<String, Object>> mapList = publicService.sqlQuery(map);
        IPage iPage = IPageUtil.pageData(mapList);
        System.out.println("第一个查询分页结果",iPage);
        Map map1 = new HashMap(3);
        //加入mybatis分页的参数pageNum与pageSize则其他查询也能分页
        map1.put("pageNum", pageNo);
        map1.put("pageSize", pageSize);
        List<SignatureUser> signatureUserList = publicService.getSignatureUserList(map1);
        System.out.println(signatureUserList.size());
        return signatureUserList;
}

3. 完整案例

java 复制代码
    @Autowired
    private PublicService publicService; 

    @ApiOperation(value = "用户信息列表", notes = "用户信息列表")
    @GetMapping(value = "/getUserList")
    public Result<?> getUserList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                 @RequestParam(name="pageSize", defaultValue="5") Integer pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                " a.id,\n" +
                " a.username,\n" +
                " b.id AS file_id,\n" +
                " b.file_url,\n" +
                " b.file_size,\n" +
                "FROM\n" +
                " sys_user a\n" +
                " LEFT JOIN sys_file b ON a.id = b.parent_id \n" +
                "WHERE\n" +
                " a.del_flag = '0'");
        //一.直接sql方式分页
        Map map = new HashMap(5);
        map.put("sql",sql.toString());
        //获取第pageNo页,pageSize条内容,默认查询总数count
        PageHelper.startPage(pageNo, pageSize);
        //紧跟着的第一个select方法会被分页
        List<Map<String, Object>> mapList = publicService.sqlQuery(map);
        IPage iPage = IPageUtil.pageData(mapList);
        //return Result.OK(iPage);
        
        
        //二.对象集合分页
        Map map1 = new HashMap(3);
        map1.put("pageNum", pageNo);
        map1.put("pageSize", pageSize);
        List<SignatureUser> signatureUserList = publicService.getSignatureUserList(map1);
        System.out.println(signatureUserList.size());
        return Result.OK(IPageUtil.pageData(signatureUserList));
    }
相关推荐
aramae5 分钟前
模拟实现strcmp()(C语言)
c语言·开发语言·后端
+VX:Fegn089537 分钟前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·课程设计
泡海椒1 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
根目录下的猫2 小时前
RK3588适配的轻量级AI模型推荐
人工智能·后端·python·目标检测
景熙55232 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7043 小时前
Spring Security权限控制
java·python·spring
wno7043 小时前
Spring Boot整合Kafka
spring boot·kafka
星栈3 小时前
用 Rust 写 Agent 服务 -- adk-rust 上手记
后端·agent
杨运交3 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring