本文共 6021 字,大约阅读时间需要 20 分钟。
在Spring Boot项目中新建,添加PageHelper的依赖:
com.github.pagehelper pagehelper-spring-boot-starter 1.2.10
修改application.yml:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test1?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT+8 password: 123 username: root type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true filters: stat, wall, log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true; druid.stat.slowSqlMillis=500mybatis: mapper-locations: classpath*:mapper/**/*.xmlpagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true pageSizeZero: false params: count=countSql
正确使用代码示例:
@Overridepublic PageInfopageQuery() { PageHelper.startPage(1, 5); List list = userMapper.queryAll(); PageInfo userPageInfo = new PageInfo<>(list); return userPageInfo;}
注意事项:
错误示例:
@Overridepublic PageInfopageQuery() { List list = userMapper.queryAll(); PageHelper.startPage(1, 5); PageInfo userPageInfo = new PageInfo<>(list);}
分页失败,需确保分页参数在Mapper查询之前设置。
在Spring Boot项目中添加所需依赖:
com.baomidou mybatis-plus-boot-starter 3.4.0
创建MybatisPlusConfig:
import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@MapperScan("com.zsc.mapper")@Configurationpublic class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; }} 修改application.yml:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test1?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT+8 password: 123 username: root type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true filters: stat, wall, log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true; druid.stat.slowSqlMillis=500mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml
代码示例:
@Overridepublic PagepageQuery() { Page userPage = new Page<>(1, 5); QueryWrapper userQueryWrapper = new QueryWrapper<>(); userQueryWrapper.like("name", "zs"); Page page = userMapper.selectPage(userPage, userQueryWrapper); return page;}
注意事项:
PageInfo或Page对象获取分页数据。创建PageUtil类,适用于快速开发或独立项目:
package com.zsc.utils;import java.util.ArrayList;import java.util.List;public class PageUtil { private List Listlist = PageService.listString();PageUtil pageUtil = new PageUtil(list);pageUtil.setPageNum(1);pageUtil.setPageSize(4);List result = pageUtil.setMyList(list);System.out.println("分页结果:" + result);
转载地址:http://momwk.baihongyu.com/