当前位置:网站首页>分页插件--PageHelper
分页插件--PageHelper
2022-07-26 22:36:00 【最不会程序的程序猿】
mybatis的分页查询可以通过PageHelper插件实现
在数据库中我们使用分页查询的sql语句为:
select * from 表名 [where 条件] limit (page-1)*pageSize, pageSize;
page: 当前页码
pageSize: 每页显示的条数。
但是当我们使用PageHelper插件后页码什么的都不用我们计算 我们可以通过直接使用它里面的函数得到我们想要的。如:总条数 总页数 当前页面呈现的数据
使用步骤:
第一步:在pom.xml中引入依赖
<dependency>
<groupId>repMaven.com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
第二步:在conf.xml中设置PageHelper拦截器
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
注意:
第三步:在测试类(单元测试)中测试:
@Test
public void testFindAll() throws Exception{
Reader rd = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(rd);
SqlSession session = factory.openSession();
UserDao userDao = session.getMapper(UserDao.class);
//pageNum:当前页 pageSize:每页显示几条数据
PageHelper.startPage(1,2);
List<User> user = userDao.findAll();
PageInfo<User> pageInfo = new PageInfo<User>(user);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页码对应的数据:"+pageInfo.getList());
session.close();
}

数据库为:
/*
Navicat Premium Data Transfer
Source Server : Michinaish
Source Server Type : MySQL
Source Server Version : 80011
Source Host : localhost:3306
Source Schema : mybatis
Target Server Type : MySQL
Target Server Version : 80011
File Encoding : 65001
Date: 06/06/2022 19:16:07
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tbl_user02
-- ----------------------------
DROP TABLE IF EXISTS `tbl_user02`;
CREATE TABLE `tbl_user02` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`pwd` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tbl_user02
-- ----------------------------
INSERT INTO `tbl_user02` VALUES (6, '张三', '123', '[email protected]');
INSERT INTO `tbl_user02` VALUES (7, '李四', '123', '[email protected]');
INSERT INTO `tbl_user02` VALUES (9, 'ac', '123', '[email protected]');
INSERT INTO `tbl_user02` VALUES (10, 'bc', '123', '[email protected]');
INSERT INTO `tbl_user02` VALUES (11, 'cc', '123', '[email protected]');
INSERT INTO `tbl_user02` VALUES (12, 'dc', '123', '[email protected]');
SET FOREIGN_KEY_CHECKS = 1;
边栏推荐
- Pre research of data quality management tools Griffin vs deequ vs great expectations vs quality
- The most classic Nature paper on Alzheimer's disease is suspected of fraud
- NFT展示指南:如何展示你的NFT藏品
- C.Net timestamp and time conversion support time zone
- What are the use cases in the Internet of things industry in 2022?
- JUnit、JMockit、Mockito、PowerMockito
- 公有云安全性和合规性方面的考虑事项
- 买不到的数目
- Pytorch learning record (II): tensor
- [postgresql]postgresqlg use generate_ Series() function completes statistics
猜你喜欢

第二部分—C语言提高篇_11. 预处理

Product principles of non-financial decentralized application
![[shaders realize distorted outline effect _shader effect Chapter 2]](/img/b3/ab28039cce2521ff1d59f0de6bf852.png)
[shaders realize distorted outline effect _shader effect Chapter 2]

Real time voice quality monitoring

np. transpose & np.expand_ dims
![[shader realizes swaying effect _shader effect Chapter 4]](/img/ab/bdbc4a0f297541b532af81a49e2633.png)
[shader realizes swaying effect _shader effect Chapter 4]

Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)

org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line

上千Tile的倾斜模型浏览提速,告别一块一块往外蹦的尴尬

Question 141 of Li Kou: circular linked list
随机推荐
第6节:cmake语法介绍
Vector execution engine framework gluten announced the official open source and appeared at spark technology summit
What is the reason for oom during redis shake synchronization in shake database?
[2016] [paper notes] differential frequency tunable THz technology——
Which securities company has the lowest commission? Is online account opening safe
What are the use cases in the Internet of things industry in 2022?
会议OA项目排座功能以及送审功能
Part II - C language improvement_ 13. Recursive function
买不到的数目
Learn various details and thoughts of chatroom implementation in Muduo
Three effective strategies for the transformation of data supply chain to be coordinated and successful
Esmfold: a new breakthrough in protein structure prediction after alphafold2
使用AW9523B芯片驱动16路LED时,LED出现误点亮的问题
Public cloud security and compliance considerations
Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
Basic select statement
Meeting OA my meeting
Product principles of non-financial decentralized application
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
Custom type