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两种方式. 在使用的过程中一 ...
随机推荐
- Log4j读取配置文件并使用
/** 设置配置路径从环境变量读取 * PropertyConfigurator类加载.properties文件的配置 * DOMConfigurator加载.xml文件的配置 ...
- 谁能告诉delphi7 的updatebatch使用属性说明?
谁能告诉delphi7 的updatebatch使用属性说明? ADODataSet1.UpdateBatch(arAll); 就是提交你的数据集到数据库 arCurrentOnly the upda ...
- 用友时空B/S表单外挂(接口)程序操作说明文档
用友时空B/S表单外挂(接口)程序 一.B/S表单接口需求 众所周知,用友时空KSOA支持B/S架构.且移动商务.在线门店,都是完全基于B/S架构的. B/S架构的优越性在于没有本地客户端和本地数据, ...
- jQuery文件上传插件uploadify
官方网站:http://www.uploadify.com/ 参考博客:jQuery Uploadify在ASP.NET MVC3中的使用 参考博客:使用uploadify上传图片时返回“Cannot ...
- myeclipse8.6 注册码
MyEclipse8.6 注册码 别处找的均是8.6版本,可以使用到2014年一:MyEclipse Standard Edition: zhucemLR7ZL-655954-695876566190 ...
- jmeter函数
1.常用JMeter函数 1)__regexFunction 正则表达式函数可以使用正则表达式(用户提供的)来解析前面的服务器响应(或者是某个变量值).函数会返回一个有模板的字符串,其中携带有可变的值 ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供了 ...
- idea的protobuf使用
1.安装插件 2.添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 【BZOJ1034】泡泡堂(贪心)
[BZOJ1034]泡泡堂(贪心) 题面 BZOJ 洛谷 题解 很基础的贪心,然而我竟然没写对...身败名裂. 大概就是类似田忌赛马. 先拿看当前最大值是否能否解决对面最大值,否则检查能否用最小值来兑 ...
- 【Learning】辛普森积分
辛普森积分 这种积分法很暴力:只要求你实现出函数求值\(f(x)\). 使用辛普森积分,我们可以求出函数一段区间\([l,r]\)的近似积分.记\(mid=\frac{l+r}2\),有: \[ \i ...