spring boot 和 mybatis 中使用 pagehelper:

1、 在pom.xml 中加入pagehelper的引用:

    <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.4</version>
</dependency>   <dependency>
   <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.2.11</version>
  </dependency>

2. 在 application.properties 文件中增加:

pagehelper.offsetAsPageNum=true
pagehelper.rowBoundsWithCount=true
pagehelper.pageSizeZero=true
pagehelper.reasonable=false
pagehelper.params=pageNum=pageHelperStart;pageSize=pageHelperRows;
pagehelper.supportMethodsArguments=false

在 application.properties 中增加以下的语句,打开 com.example.boot2.mapper 包下的日志, 该包下的 sql 语句都可以在控制台输出。

logging.level.com.example.boot2.mapper=debug

3. Controller 文件:

  @Autowired
  private UserService userService;

 @RequestMapping(method = RequestMethod.GET, value = "/selectPage/{pageNum}/{pageSize}")
public PageInfo<User> selectPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) { PageInfo<User> page = userService.selectPage(pageNum, pageSize);
System.out.println(page.getPageSize());
return page;
}

4. UserService 接口:

public PageInfo<User> selectPage(int pageNum, int pageSize);

5. UserServiceImpl 实现类:

    @Autowired
private UserMapper userMapper; public PageInfo<User> selectPage(int pageNum, int pageSize) {
// 将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
List<User> list = userMapper.selectAll();
System.out.println(list.size());
PageInfo page = new PageInfo(list);
return page;
}

在 查询语句  userMapper.selectAll(); 前面加上:  PageHelper.startPage(pageNum, pageSize);  则后面的查询语句即被加上了分页的功能。

6. UserMapper 接口: List<User> selectAll();

7. UserMapper.xml  文件:

  <select id="selectAll" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from se_user
</select>

浏览器中访问:http://localhost:8080/user/selectPage/5/3

日志如下:

2019-06-03 17:43:24.713 DEBUG 30808 --- [nio-8080-exec-5] c.e.b.mapper.UserMapper.selectAll_COUNT  : ==>  Preparing: SELECT count(0) FROM se_user
2019-06-03 17:43:24.713 DEBUG 30808 --- [nio-8080-exec-5] c.e.b.mapper.UserMapper.selectAll_COUNT : ==> Parameters:
2019-06-03 17:43:24.783 DEBUG 30808 --- [nio-8080-exec-5] c.e.b.mapper.UserMapper.selectAll_COUNT : <== Total: 1
2019-06-03 17:43:24.783 DEBUG 30808 --- [nio-8080-exec-5] c.e.boot2.mapper.UserMapper.selectAll : ==> Preparing: select sid, user_num, user_real_name, user_nick_name, user_id_code,
user_address, user_head_pic, user_self_head_pic, user_id_code_front_pic, user_id_code_back_pic, user_approve_status, user_create_date, user_longitude, user_latitude, user_frequent_address,
user_service_num, user_speciality, user_balance, user_service_status, user_apply_datetime, user_service_city, user_company, user_age, user_sex, user_refusal_reason, user_collection_num, user_mcount,
user_telephone, user_approved_datetime, user_is_star, user_invite_no, two_dimension_code, used_capacity, max_capacity, friend_background, click_num, is_discount, user_role, is_use, user_stock_num, is_agent,
member_point, member_glory, member_level, frozen_money, agent_create_time, business_code, user_address_detail, is_sign, user_source, is_house_manager, data_source, deposit_money from se_user LIMIT 12,3
2019-06-03 17:43:24.783 DEBUG 30808 --- [nio-8080-exec-5] c.e.boot2.mapper.UserMapper.selectAll : ==> Parameters:
2019-06-03 17:43:24.789 DEBUG 30808 --- [nio-8080-exec-5] c.e.boot2.mapper.UserMapper.selectAll : <== Total: 3
3
3

返回 3条数据:

pagehelper用法的更多相关文章

  1. MyBatis 分页插件 PageHelper 使用

    1.  引入Maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  2. Echo团队Alpha冲刺随笔 - 第二天

    项目冲刺情况 进展 项目各端都已经开始正式动工,完成了框架的搭建及小部分代码的编写 问题 对于框架使用不够熟练 心得 撸起袖子加油干! 今日会议内容 黄少勇 今日进展 实现账号绑定和首页公告信息及使用 ...

  3. MyBatis-Plus 用起来真的很舒服

    一.MyBatis-Plus 1.简介 MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在. 官网文档地址 ...

  4. PageHelper分页正确用法

    依赖和配置就不说了,说用法 Page<Object> page = PageHelper.startPage(pageNum, pageSize); List<SysRoleDTO& ...

  5. Spring集成PageHelper的简单用法

    1.Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应 <!-- 添加Mybatis依赖 --> <dependency> <groupId ...

  6. Mybatis分页插件PageHelper正确的用法(网上有2篇不够科学的文章)

    今天下午在Mybatis项目中.实现分页.由于我是后加入项目中的,Leader用的是PageHelper这个组件.可是我在实际使用的过程中遇到了2个大问题. 1.p=2#comments" ...

  7. MyBatis学习总结_17_Mybatis分页插件PageHelper

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...

  8. Mybatis 数据库物理分页插件 PageHelper

    以前使用ibatis/mybatis,都是自己手写sql语句进行物理分页,虽然稍微有点麻烦,但是都习惯了.最近试用了下mybatis的分页插件 PageHelper,感觉还不错吧.记录下其使用方法. ...

  9. Mybatis基本用法--下

    Mybatis基本用法--下 第七部分 mybatis-spring-boot-starter 官网:http://www.mybatis.org/spring-boot-starter/mybati ...

随机推荐

  1. 数据结构---->数组

    1.什么是数组? 数组是一种线性的数据结构.它同一组连续的内存空间,来存储一组具有相同类型的数据. 简单说明几点: (1).线性表:就是数据排成像一条线一样的结构.每个线性表的数据最多只有前和后两个方 ...

  2. 网络协议相关面试问题-http协议相关面试问题

    HTTP协议简介: 一些基本概念: 协议:指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则. HTTP协议:超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML ...

  3. emplace_back() 和 push_back 的区别

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

  4. 什么是DNS

    DNS,或者说域名系统,通常是学习如何配置网站和服务器的一个非常困难的部分.了解 DNS 的工作原理将有助于诊断网络访问的问题,也有助于理解 DNS 系统背后的工作原理. 这篇文章中,我们会讨论一些基 ...

  5. [Algorithm] Convert a number from decimal to binary

    125, how to conver to binary number? function DecimalToDinary (n) { let temp = n; let list = []; if ...

  6. PIC - For Resources [ background ]

  7. 32. ClustrixDB License管理

    一.许可的概述 ClustrixDB必须拥有有效的许可证才能运行.本授权指定: 集群中允许的最大节点数 ClustrixDB将使用的最大核数 在裸金属系统上,ClustrixDB将尝试启用与已授权的物 ...

  8. JVM(二),Java怎样实现一次编译到处运行(平台无关性)

    二.Java怎样实现一次编译到处运行(平台无关性) 1.java平台无关性原理 Java源码首先被编译成字节码,再由不同平台的JVM进行解析,JAVA语言在不同的平台上运行时不需要进行重新编译,Jav ...

  9. MVN 报错1

    找不到mapper映射文件 只打包了下面这些 所以pom.xml文件中添加 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --> <build> ...

  10. 改变CTS测试中timeout时间

    关键类: JarHostTest.java——>目录:%SOURCE_ROOT%/cts/tools/tradefed-host/src/com/android/cts/tradefed/tes ...