首先在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的更多相关文章

  1. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  2. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  3. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

  4. Spring Boot系列教程八: Mybatis使用分页插件PageHelper

    一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...

  5. Spring Boot系列教程十一: Mybatis使用分页插件PageHelper

    一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...

  6. springboot中的mybatis是如果使用pagehelper的

    springboot中使用其他组件都是基于自动配置的AutoConfiguration配置累的,pagehelper插件也是一样的,通过PageHelperAutoConfiguration的,这个类 ...

  7. 理解 Mybatis的分页插件 PageHelper

    Mybatis + SpringMVC + Maven实现分页查询 (推荐采用的插件是PageHelper) 先看一下之前的这篇博客,这里推荐了 Mybatis 的分页方法. 按照上面的方法设置后,确 ...

  8. Mybatis的分页插件PageHelper分页失效的原因

    引用博客:个人博客地址:https://alexaccele.github.io/ PageHelper是Mybatis的一个很好的分页插件,但要使用它的分页功能需要注意一下几点 1.导入相关包,例如 ...

  9. Mybatis 的分页插件 PageHelper

    我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 jsqlparser-0.9.4.jar   使用方法: 1.根据Mybatis的版本下载对应版 ...

随机推荐

  1. Java中复合赋值运算符自动进行强制类型转换

    public class Operation { public static void main(String[] args) { int num1 = 10; num1 = num1 / 2.2; ...

  2. 项目兼容ie8技术要点

    好久没有写博客了,因为最近公司项目要调ie8兼容,一直在忙这事,终于竣工了,跟大家分享下这老掉牙的浏览器是如何搞定的...本人新手一枚,欢迎大家指教 项目是使用的jeecg框架,后台使用的java,前 ...

  3. YAML_04 用user模块添加用户,并修改密码

    ansible]# vim user.yml --- - hosts: cache   remote_user: root   vars:     username: lisi   tasks:   ...

  4. pt

    https://www.hdarea.co/torrents.php http://hdhome.org/torrents.php https://ourbits.club/torrents.php ...

  5. PHP-FPM 的工作整理

    1.php-fpm的配置文件 根据命令找到路径修改配置文件 ps -ef|grep php-fpm vim /home/php/etc/php-fpm.conf ;;;;;;;;;;;;;;;;;; ...

  6. 深入基础(四)Buffer,转码

    Buffer     前面提及到一些关于buffer类的问题,当时不是很明确 那么就次机会顺便深入探讨一下这个东西到底干嘛的出现在什么时候,如何使用.昨天跟朋友聊天他说我每一篇博文内容太长太长了 虽然 ...

  7. lixuxmint系统定制与配置(3)-字体

    小书匠Linux 有些系统自带的字体实在太难看了,看起来不清晰,不明确,有一个好的字体,可以带来好心情,并提高工作与效率. 1.常用中文字体 文泉驿微黑,微软雅黑,思源黑体 2.字体安装 2.1检查已 ...

  8. 记录一个webstorm的设置或者说小技巧

    在 html 的元素中,如果输入属性,默认会填充 引号,在 react 书写中非常不方便. 其中的JSX很多时候是不需要 quotation 的,只是需要一个 括号 {} 即可. 自己找了下webst ...

  9. 《挑战30天C++入门极限》C++中类的多态与虚函数的使用

        C++中类的多态与虚函数的使用 类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持 ...

  10. Pytest权威教程08-使用tmp目录和文件

    目录 使用tmp目录和文件 tmp_path Fixture方法 tmp_path_factory Fixture方法 tmpdir Fixture方法 tmpdir_factory Fixture方 ...