Mybatis分页插件PageHelper使用
使用PageHelper插件如何分页:
下载地址:
https://github.com/pagehelper/Mybatis-PageHelper
https://github.com/JSQLParser/JSqlParser
另外一个地址:
Pagehelper 下载地址:
http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/
jsqlparser 下载地址:
http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/
使用步骤:
1、导入相关包 pagehelper-x.x.x.jar 和 jsqlparser-x.x.x.jar。
2、在MyBatis全局配置文件中配置分页插件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <!-- 注意 <plugins> 在xml文件中的位置,必须要符合 http: //mybatis.org/dtd/mybatis-3-config.dtd 中指定的顺序:--> <!-- configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?) --> <configuration> <!-- 为SQL定义部分的parameterType或resultType属性指定自定义类型的别名 --> <typeAliases> <typeAlias alias= "ServiceStation" type= "com.mybatis.models.ServiceStation" /> <typeAlias alias= "InspectorInfo" type= "com.mybatis.models.InspectorInfo" /> <typeAlias alias= "StationInspector" type= "com.mybatis.models.StationInspector" /> </typeAliases> <!-- 配置分页拦截器 --> <plugins> <!-- 配置分页插件 --> <plugin interceptor= "com.github.pagehelper.PageInterceptor" ></plugin> <!-- com.mybatis.util为PageHelper类所在包名 --> <!-- <plugin interceptor= "com.mybatis.util.PagePlugin" > --> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <!-- <property name= "dialect" value= "SQLite" /> --> <!-- <property name= "pageSqlId" value= ".*Page.*" /> --> <!-- </plugin> --> </plugins> <!-- 设置数据库连接参数 --> <!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 --> <environments default = "development" > <environment id= "development" > <transactionManager type= "JDBC" /> <dataSource type= "POOLED" > <property name= "driver" value= "com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name= "url" value= "jdbc:sqlserver://mssql-rw-cyp-coopbusiness.vip.test.suixinhuan.com;DatabaseName=CYP_CoopBusiness" /> <property name= "username" value= "Umanager" /> <property name= "password" value= "ASD123asd!1" /> </dataSource> </environment> </environments> <!-- 加载SQL定义文件 --> <!-- 这里交给sqlSessionFactory 的 mapperLocations属性去得到所有配置信息 --> <mappers> <mapper resource= "com/mybatis/sql/ServiceStation.xml" /> <mapper resource= "com/mybatis/sql/InspectorInfo.xml" /> <mapper resource= "com/mybatis/sql/StationInspector.xml" /> </mappers> </configuration> |
3、使用PageHelper提供的方法进行分页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.mybatis; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.mybatis.dao.InspectorInfoMapper; import com.mybatis.dao.MyBatisUtil; import com.mybatis.dao.ServiceStationMapper; import com.mybatis.dao.StationInspectorMapper; import com.mybatis.models.ServiceStation; import com.mybatis.models.InspectorInfo; import com.mybatis.models.StationInspector; //import com.mybatis.util.Page; /* * 测试类 */ public class HelloMyBatisProgram { public static void main(String[] args) { SqlSession session = MyBatisUtil.getSession(); //由框架生成ServiceStationMapper接口实现对象 ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper. class ); InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper. class ); StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper. class ); //System.out.println(ssDaoMaper.getClass().getName()); System. out .println( "===========分页获取所有服务站列表============" ); //分页设置放在查询之前 Page<Object> page = PageHelper.startPage(1, 5, "StationName" ); List<ServiceStation> listPage = ssDaoMaper.findAllPage( "110100" ); for (ServiceStation item:listPage) { System. out .println(item.getStationName()+ " " +item.getCityCode()+ " " +item.getCityName()); } System. out .println( "当前页码:" +page.getPageNum()); System. out .println( "每页的记录数:" +page.getPageSize()); System. out .println( "总记录数:" +page.getTotal()); System. out .println( "总页码:" +page.getPages()); } } |
4、可以使用更强大的PageInfo封装返回结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.mybatis; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.mybatis.dao.InspectorInfoMapper; import com.mybatis.dao.MyBatisUtil; import com.mybatis.dao.ServiceStationMapper; import com.mybatis.dao.StationInspectorMapper; import com.mybatis.models.ServiceStation; import com.mybatis.models.InspectorInfo; import com.mybatis.models.StationInspector; //import com.mybatis.util.Page; /* * 测试类 */ public class HelloMyBatisProgram { public static void main(String[] args) { SqlSession session = MyBatisUtil.getSession(); //由框架生成ServiceStationMapper接口实现对象 ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper. class ); InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper. class ); StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper. class ); //System.out.println(ssDaoMaper.getClass().getName()); System. out .println( "===========分页获取所有服务站列表============" ); //分页设置放在查询之前 page = PageHelper.startPage(1, 3, "StationName desc" ); List<ServiceStation> list = ssDaoMaper.findAll(); for (ServiceStation item:list) { System. out .println(item.getStationName()+ " " +item.getCityCode()+ " " +item.getCityName()); } PageInfo<ServiceStation> info = new PageInfo<ServiceStation>(list, 3); System. out .println( "当前页码:" +info.getPageNum()); System. out .println( "每页的记录数:" +info.getPageSize()); System. out .println( "总记录数:" +info.getTotal()); System. out .println( "总页码:" +info.getPages()); System. out .println( "是否第一页:" +info.isIsFirstPage()); System. out .println( "连续显示的页码:" ); int [] nums = info.getNavigatepageNums(); for ( int i = 0; i < nums.length; i++) { System. out .println(nums[i]); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现