项目结构和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中的结果 }
}

最新文章

  1. js动态的把左边列表添加到右边,可上下移动。
  2. Hive 分组问题
  3. Can&#39;t exec &quot;aclocal&quot;: No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
  4. Android Studio生成javadoc出错的解决办法
  5. JdbcTemplate使用总结
  6. System.Net.Sockets.Socket SendAsync System.ObjectDisposedException: Cannot access a disposed object.
  7. Yii表单验证
  8. 转: https 单向双向认证说明_数字证书, 数字签名, SSL(TLS) , SASL
  9. 备战“软考”之软件project
  10. Keil C中全局变量的使用
  11. leetcode实现 “10001”+“1011” 返回二进制相加的结果
  12. Linux 守护进程的启动方法
  13. 开学&amp;东大一周游记
  14. Struts2的核心运行流程,原理图解
  15. Head First设计模式之策略模式
  16. 如何将VMware虚拟机迁移到AWS
  17. 记录一下,PC端vue开发常用框架,已经用过elementUI和iview 接下来尝试另一个Muse-UI 喜欢它的点击效果
  18. Go学习笔记:Win7+LiteIDE+Go+Beego 环境搭建
  19. wxformbuilder在python如何使用
  20. iOS开发如何在一个透明视图上添加不透明的子控件

热门文章

  1. Linux上两种网络连接方式
  2. ubuntu下安装vsftpd及vsftpd配置文件不见的解决办法
  3. mysql 时间格式化参数表笔记
  4. [转帖]HDD磁盘,非4K无以致远
  5. rhel和centos7下更改网卡名称ens33为eth0
  6. 重新认识javascript的settimeout和异步
  7. double 和 im2double 的区别
  8. MySQL 大表备份、改表
  9. Day24--Part2-伪Ajax(iframe)
  10. winform 利用委托实现窗体传值