springboot使用pageHelper分页插件

2024-06-10 23:08

本文主要是介绍springboot使用pageHelper分页插件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

maven引入pageHelper

<!--  https://github.com/pagehelper/Mybatis-PageHelper --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.1.0</version></dependency>

mybatis配置分页信息

@Configuration
@EnableTransactionManagement
public class MybatisConfig implements TransactionManagementConfigurer {@AutowiredDataSource dataSource;@Bean(name = "sqlSessionFactory")public SqlSessionFactory sqlSessionFactoryBean() {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setTypeAliasesPackage("cn.vobile.hss.model");// 支持下划线到驼峰org.apache.ibatis.session.Configuration conf = new org.apache.ibatis.session.Configuration();conf.setMapUnderscoreToCamelCase(true);bean.setConfiguration(conf);//分页插件PageHelper pageHelper = new PageHelper();Properties properties = new Properties();properties.setProperty("offsetAsPageNum", "true");properties.setProperty("rowBoundsWithCount", "true");properties.setProperty("reasonable", "true");properties.setProperty("supportMethodsArguments", "true");properties.setProperty("returnPageInfo", "check");properties.setProperty("params", "pageNum=page;pageSize=rows;orderBy=orderBy");pageHelper.setProperties(properties);//添加插件bean.setPlugins(new Interceptor[]{pageHelper});//添加XML目录ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();try {//读取多个文件下的xml文件List<Resource> resources = new ArrayList<>(8);Resource[] resources1 = resolver.getResources("classpath*:mapper/*.xml");Resource[] resources2 = resolver.getResources("classpath*:sqlmapperext/*.xml");resources.addAll(Arrays.asList(resources1));resources.addAll(Arrays.asList(resources2));bean.setMapperLocations(resources.toArray(new Resource[]{}));return bean.getObject();} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}}

主要是对properties中进行的一些配置化信息。

  • reasonable表示分页合理化,如果pageNum<=0时候会查询第一页的数据。如果pageNum>总页数,会查询最后一页的数据。

  • params中可以定义分页属性名称,默认是pageNum和pageSize。你也可以定义其他的名称。
    例如 properties.setProperty(“params”, “pageNum=page;pageSize=rows;orderBy=orderBy”);

  • pageSizeZero默认false,为true时候,如果pageSize=0时候查询所有数据

使用PageHelper

//自定义分页bo类。主要设置三个属性。与params中设置的对应
public class BaseEntityWithPage {@Transientprivate Integer page = 1;@Transientprivate Integer rows = 10;@Transientprivate String orderBy;@JsonIgnorepublic String getOrderBy() {return orderBy;}public void setOrderBy(String orderBy) {this.orderBy = orderBy;}@JsonIgnorepublic Integer getPage() {return page;}public void setPage(Integer page) {this.page = page;}@JsonIgnorepublic Integer getRows() {return rows;}public void setRows(Integer rows) {this.rows = rows;}
}
//service调用处
public List<Customer> getAllCustomers(Customer page){page.setOrderBy("id");PageHelper.startPage(page);return customerMapper.getAllCustomers();}

直接调用PageHelper的startPage方法。

public static <E> Page<E> startPage(Object params) {}

结果

@Testpublic void getCustomersTest(){Customer page = new Customer();page.setRows(10);page.setPage(2);List<Customer> list = customerService.getAllCustomers(page);System.out.println("list = " + list);}
 //打印的结果SELECT * FROM customer WHERE id <> 'common0000' order by id limit ?,? 
DEBUG [main] - ==> Parameters: 0(Integer), 10(Integer)

可能有些不太详细,具体可以参考插件作者的文档哦。
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

这篇关于springboot使用pageHelper分页插件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1049527

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的