项目结构和spring搭建mybatis请参考springboot整合mybatis。在这个基础上配置分页。

一:导入PageHelper依赖

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>

二:在启动类SpringmybatisdemoApplication中配置PageHelper bean

@Bean
public PageHelper pageHelper() {
System.out.println("MyBatisConfiguration.pageHelper()");
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}

三:在controller中使用PageHelper ,注入对应的服务UserService (该服务的具体可看本文章开头的springboot整合mybatis).注意:只有紧接着PageHelper.startPage(2,3); 后的那句sql才会进行分页,再下一句sql则不分页。特别注意的是,下面获取到的结果是存放在返回的list中的,但是如果直接输出personList.toString(),输出的是Page对应(写的是一个结果的总体信息格式如下Page{count=true, pageNum=2, pageSize=3, startRow=3, endRow=6, total=6, pages=2, countSignal=false, orderBy='name DESC ', orderByOnly=false, reasonable=true, pageSizeZero=false})(因为toString方法被重写了),需要用循环获取的方式获取其中的内容,才可以看到分页的结果

package com.example.springmybatisdemo.controller;

import com.example.springmybatisdemo.entity.Person;
import com.example.springmybatisdemo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class UserController {
@Autowired
private UserService userService; @RequestMapping("/helloUser/{id}")
@ResponseBody
public String selectUser (@PathVariable int id){
PageHelper.startPage(2,3); //pageNum=2, pageSize=3 ,表示每页的大小为3,查询第二页的结果
PageHelper.orderBy("name DESC "); //进行分页结果的排序,name为字段名,排序规则DESC/ASC
List<Person>personList= userService.selectUser();
System.out.println(personList.toString()); //输出的是Page对象
for(int i=0;i<personList.size();i++){
System.out.println(personList.get(i)); //获取到的分页结果
}
return userService.selectUser().toString(); //这句不分页,又再次执行原始sql语句,正确的是返回list中的结果 }
}

springboot+mybatis使用PageHelper分页的更多相关文章

  1. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  2. springboot mybatis的pagehelper分页

    maven repositary里,分页组件常用的有两个 com.github.pagehelper » pagehelper-spring-boot-starter  com.github.page ...

  3. Mybatis的PageHelper分页插件的PageInfo的属性参数,成员变量的解释,以及页面模板

    作者:个人微信公众号:程序猿的月光宝盒 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int si ...

  4. SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页

    前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了 ...

  5. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  6. 小白的springboot之路(十五)、mybatis的PageHelper分页插件使用

    0.前言 用mybatis,那么分页必不可少,基本都是用PageHelper这个分页插件,好用方便: 1.实现 1.1.添加依赖: <!-- 3.集成 mybatis pagehelper--& ...

  7. springBoot mybatis mysql pagehelper layui 分页

    <!-- 加入 pagehelper 分页插件 jar包--><dependency> <groupId>com.github.pagehelper</gro ...

  8. SpringBoot整合系列-PageHelper分页插件

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分页插件PageHelper ...

  9. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

随机推荐

  1. 6/10 sprint2 看板和燃尽图的更新

  2. Internet History, Technology and Security (Week 6)

    Week 6 Technology: Transport Control Protocol (TCP) Welcome to Week 6 of IHTS. We are in our second ...

  3. 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]

    题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...

  4. Java乐观锁、悲观锁

    乐观锁 乐观锁(Optimistic Lock), 顾名思义,就是很乐观,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号 ...

  5. jQuery文件上传插件uploadify

    官方网站:http://www.uploadify.com/ 参考博客:jQuery Uploadify在ASP.NET MVC3中的使用 参考博客:使用uploadify上传图片时返回“Cannot ...

  6. Qt中 QString 转 char*

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  7. 在HL引擎中制作自定义高清贴花

    你是不是想过要做自定义的子弹孔.喷漆或者一些自定义的痕迹呢? 如果按照HL引擎的基本FA,首先要在decals.wad里加入我们自定义的纹理,然后利用gEngfuncs.pEfxAPI->R_D ...

  8. LOJ #143. 质数判定

    题目描述 判定输入的数是不是质数. 输入格式 若干行,一行一个数 x. 行数不超过 1.5×104​​. 输出格式 对于输入的每一行,如果 x 是质数输出一行 Y,否则输出一行 N. 样例 样例输入 ...

  9. java的4种引用 强软弱虚

    &amp;lt;img src="https://pic4.zhimg.com/d643d9ab5c933ac475cfa23063bed137_b.png" data-r ...

  10. 【BootStrap】Table的基本使用

    一.前言        新年新气象,转眼今年就28了,不知道今年能不能把妹成功呢?哈哈哈!上班第一天,部门Web技术主管给每个同事都发了红包鼓励大家今年加油,我作为新转入部门员工不能给团队掉链子,要加 ...