springboot+mybatis使用PageHelper分页
项目结构和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分页的更多相关文章
- SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页
SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...
- springboot mybatis的pagehelper分页
maven repositary里,分页组件常用的有两个 com.github.pagehelper » pagehelper-spring-boot-starter com.github.page ...
- Mybatis的PageHelper分页插件的PageInfo的属性参数,成员变量的解释,以及页面模板
作者:个人微信公众号:程序猿的月光宝盒 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前页的数量 private int si ...
- SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了 ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- 小白的springboot之路(十五)、mybatis的PageHelper分页插件使用
0.前言 用mybatis,那么分页必不可少,基本都是用PageHelper这个分页插件,好用方便: 1.实现 1.1.添加依赖: <!-- 3.集成 mybatis pagehelper--& ...
- springBoot mybatis mysql pagehelper layui 分页
<!-- 加入 pagehelper 分页插件 jar包--><dependency> <groupId>com.github.pagehelper</gro ...
- SpringBoot整合系列-PageHelper分页插件
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分页插件PageHelper ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
随机推荐
- Teamcity部署.net服务“无法连接到远程服务器”解决方式
在公司Teamcity上执行自动部署.net服务的时候,发现Teamcity在启动default.aspx的时候报错了,提示:使用“0”个参数调用“GetResponse”时发生异常:“无法连接到远程 ...
- log4j2分析总结(一)
现在公司用log4j2 进行日志记录,我也看了相关的资料,现在进行记录学习总结下 整体结构 Appenders里设置日志的输出方式.级别和格式 Loggers里设置全局的级别和绑定appenders里 ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- BZOJ4870 [Shoi2017]组合数问题 【组合数 + 矩乘】
题目链接 BZOJ4870 题解 \[ans = \sum\limits_{i = 0}^{\infty}{nk \choose ik + r} \pmod p\] 发现实际是求 \[ans = \s ...
- 【bzoj2555】 SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 (题目链接) 题意 给出一个初始串,维护两个操作.在原串后面加入一个字符串:询问某个字符串在原 ...
- myeclipse2015修改web项目部署名
在旧版本的myeclipse中修改web项目部署名很方便,直接右键在properties中查找web就能进行修改. 但是myeclipse2015中发现不能直接修改了. 我们可以点击config,或者 ...
- STS导入Gradle项目出现 Could not create task of type 'DependencyManagementReportTask'
解决方法: 在build.gradle文件下添加如下: classpath 'io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE'
- 解决Android SDK Manager更新时出现问题
使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xm ...
- 解题:BJOI 2006 狼抓兔子
题面 可以看出来是最小割,然后你就去求最大流了 这么大的范围就是让你用网络流卡的?咋想的啊=.=??? 建议还是老老实实用 平面图最小割等于其对偶图最短路 这个东西来做吧,虽然这个东西跑的也挺慢的,最 ...
- bzoj 3772 :精神污染 线段树+打标记 or 主席树
3772: 精神污染 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 315 Solved: 87[Submit][Status][Discuss] D ...