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两种方式. 在使用的过程中一 ...
随机推荐
- 交换机、linux光衰查询
RX收光,TX发光 一.交换机 命令: display interface transceiver brief 结果: ...... HW6851 10GE1/0/15 transceiver dia ...
- 微信小程序入门一: 简易form、本地存储
实例内容 登陆界面 处理登陆表单数据 处理登陆表单数据(异步) 清除本地数据 实例一: 登陆界面 在app.json中添加登陆页面pages/login/login,并设置为入口. 保存后,自动生成相 ...
- Java如何查看死锁?
转载自 https://blog.csdn.net/u014039577/article/details/52351626 Java中当我们的开发涉及到多线程的时候,这个时候就很容易遇到死锁问题,刚开 ...
- [知乎]老狼:UFS VS NVMe
https://zhuanlan.zhihu.com/p/26652622 最近某手机厂商的闪存门在知乎上被人踢爆,在所谓“爵士水军”和“友商水军”的口水大战中,至少eMMC, UFS等火星名词被广泛 ...
- 如何调换antd中Modal对话框确认按钮和取消按钮两个按钮的位置
今天有个工作是把所有的确认按钮放在取消按钮的左边,类似于下图这样的,公司用的时antd组件 但是antd组件的按钮时确认键放在右边的 可以采用下面的方式,将按钮调换过来: 对的,就是在modal里面的 ...
- BZOJ 2143 飞飞侠(分层最短路)
飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街区都装有弹射装置.使用弹射装置是需要支付一定费用的.而且每个弹射装置都有自己的弹 ...
- BZOJ5297 CQOI2018社交网络(矩阵树定理)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- P4910 帕秋莉的手环
题目背景 帕秋莉是蕾米莉亚很早结识的朋友,现在住在红魔馆地下的大图书馆里.不仅擅长许多魔法,还每天都会开发出新的魔法.只是身体比较弱,因为哮喘,会在咏唱符卡时遇到麻烦. 她所用的属性魔法,主要是生命和 ...
- Android Paging库使用详解
Android分页包能够更轻易地在RecyclerView里面缓慢且优雅地加载数据. 许多应用从数据源消耗数据, 数据源里面有大量的数据, 但是一次却只展示一小部分. 分页包帮助应用观测和展示大量数据 ...
- 【Cf #502 F】The Neutral Zone
本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...