springboot中使用mybatis的分页插件pageHelper
首先在pom.xml中配置
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
然后就是写代码了,最重要的两个类是:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanqiao.dao.UserDao;
import com.yuanqiao.entity.User;
import com.yuanqiao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao; @Override
public PageInfo<User> getAllUsers(int pageNum,int pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<User> allUsers = userDao.getAllUsers();
PageInfo<User> pageInfo=new PageInfo<>(allUsers);
return pageInfo;
} @Override
public User getUserById(Integer id) {
User user = userDao.getUserById(id);
System.out.println("------------------这里打印了吗?------------------------------");
System.out.println(JSON.toJSONString(user));
return user;
} }
执行测试类
package com.yuanqiao.service; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest { @Autowired
private UserService userService; @Test
public void getAllUsers(){
userService.getAllUsers(1,2);
}
@Test
public void getAllUsers002(){
userService.getAllUsers(2,3);
}
}
数据库数据如下;

测试sql日志结果如下:
2019-08-03 00:21:00.053 INFO 24268 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6d963d70] will not be managed by Spring
==> Preparing: SELECT count(0) FROM user
==> Parameters:
<== Columns: count(0)
<== Row: 14
<== Total: 1
==> Preparing: select * from user LIMIT ?, ?
==> Parameters: 3(Integer), 3(Integer)
<== Columns: id, name, sex, age
<== Row: 4, zhangsan, female, 33
<== Row: 5, zhangsan, male, 18
<== Row: 6, zhangsan, male, 19
<== Total: 3
可以看到pageHelper实质是帮我们重新封装了sql的,并没有全表扫描出来再物理组装,所以是很高级的一个工具了。
springboot中使用mybatis的分页插件pageHelper的更多相关文章
- SpringBoot集成MyBatis的分页插件 PageHelper
首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...
- Mybatis的分页插件PageHelper
Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina. ...
- Mybatis之分页插件pagehelper的简单使用
最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...
- Spring Boot系列教程八: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- springboot中的mybatis是如果使用pagehelper的
springboot中使用其他组件都是基于自动配置的AutoConfiguration配置累的,pagehelper插件也是一样的,通过PageHelperAutoConfiguration的,这个类 ...
- 理解 Mybatis的分页插件 PageHelper
Mybatis + SpringMVC + Maven实现分页查询 (推荐采用的插件是PageHelper) 先看一下之前的这篇博客,这里推荐了 Mybatis 的分页方法. 按照上面的方法设置后,确 ...
- Mybatis的分页插件PageHelper分页失效的原因
引用博客:个人博客地址:https://alexaccele.github.io/ PageHelper是Mybatis的一个很好的分页插件,但要使用它的分页功能需要注意一下几点 1.导入相关包,例如 ...
- Mybatis 的分页插件 PageHelper
我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 jsqlparser-0.9.4.jar 使用方法: 1.根据Mybatis的版本下载对应版 ...
随机推荐
- 洛谷 P2363 马农
题目描述 分别枚举两个矩阵?那样n^6太要命了. 可以枚举两个矩形的交点 将交点看成原点,可以将整个区域分成四个象限,1与3对应,2与4对应 再枚举相对应的象限计算可以获得的利益,用hash判重 可枚 ...
- 笔记-读官方Git教程(2)~安装与配置
小书匠 版本管理 教程内容基本来自git官方教程,认真都了系列的文章,然后对一些重点的记录下来,做了简单的归纳并写上自己的思考. 1.安装 在基于 Debian 的发行版上,使用 apt-get安装 ...
- Comet OJ - Contest #11题解
传送门 \(A\) 咕咕咕 const int N=1e6+5; char s[N],t[N];int n,res; inline bool cmp(const int &x,const in ...
- subcode
在思考.查阅subcode时,我发现Magma,Sage Math软件都提供了具体的命令和例子,对subcode的认识比较具象. 例如:Sage Math中有如下命令: C1 = codes.Hamm ...
- 《挑战30天C++入门极限》C/C++中结构体(struct)知识点强化
C/C++中结构体(struct)知识点强化 在上一个教程中我们已经简单的阐述了什么是结构体了,为了进一部的学习结构体这一重要的知识点,我们今天来学习一下链表结构. 结构体可以看做是一种自定义 ...
- 说明os,sys模块有什么不同
官方解释: os: This module provides a portable way of usingoperating system dependent functionality. 翻译:提 ...
- [WEB安全]PHP伪协议总结
0x01 简介 首先来看一下有哪些文件包含函数: include.require.include_once.require_once.highlight_file show_source .readf ...
- js检测手机上是否有此APP,有的话打开应用,没有的话跳转到appstore
//html代码中 的 a 标签,以微信为例,默认的是调用weixin scheme,去打开本机的微信,如果没有则跳转到相应连接 <a href="weixin://" cl ...
- docker运行puppeteer出现defucnt僵尸进程
其实这是docker的一个bug,就是在运行前加--init即可,注意这个在mac中没有只在linux上有. docker run --init -d ..... 具体内容参见:https://sta ...
- arcgis python 获得arcgis的版本和安装路径
import arcpy # Use the dictionary iteritems to iterate through # the key/value pairs from GetInstall ...