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

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解