Mybatis分页方法
使用 Maven
在 pom.xml 中添加如下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
Example
可以这样调用,PageHelper.startPage(1,10)表示从第一页开始,每页10条记录,返回值为Page<>。
@RestController
public class ProductController {
@Autowired
ProductMapper productMapper;
@RequestMapping("getProduct")
public Page<Product> getProduct(){
PageHelper.startPage(1,10);
Page<Product> productList = (Page<Product>) productMapper.selectAll();
return productList;
}
}
这是直接通过mapper获取DO数据可以直接使用,List<>可以被直接强转为Page<>。
如果要转换为DTO或VO,需要通过下面的方式
@RequestMapping("getProduct")
public PageInfo<ProductDTO> getProduct(){
PageHelper.startPage(1,10);
List<ProductDTO> productDTOS = new ArrayList<>();
List<Product> productList = productMapper.selectAll();
PageInfo<Product> pageInfo = new PageInfo<>(productList);
for (Product product : productList) {
ProductDTO productDTO = new ProductDTO();
BeanUtils.copyProperties(product,productDTO);
productDTOS.add(productDTO);
}
PageInfo pageResult = new PageInfo(productDTOS);
return pageResult;
}
返回值为PageInfo<>类型,因为List<DTO>为ArrayList类,不能直接转为Page<>,所以放在PageInfo<>中。
前端取list中的内容即可,其余为参数信息。
PageHelper.startPage 静态方法调用
除了 PageHelper.startPage 方法外,还提供了类似用法的 PageHelper.offsetPage 方法。
在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
Mybatis分页方法的更多相关文章
- Mybatis分页插件PageHelper的配置和使用方法
Mybatis分页插件PageHelper的配置和使用方法 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分 ...
- Spring data Jpa 分页从1开始,查询方法兼容 Mybatis,分页参数兼容Jqgrid
废话少说 有参数可以设置 在org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties 中 /** * Whethe ...
- MyBatis分页插件PageHelper使用方法
1.导入相关依赖坐标 <!-- MyBatis分页插件 --> <dependency> <groupId>com.github.pagehelper</gr ...
- Mybatis分页和Spring的集成
写了一个Mybatis分页控件,在这记录一下使用方式. 在Maven中加入依赖: ? 1 2 3 4 5 6 7 8 9 <dependencies> ... <depe ...
- Mybatis分页插件PageHelper简单使用
一个好的讲解mybatis的博客地址http://www.jianshu.com/nb/5226994 引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句 ...
- Mybatis分页插件PageHelper使用
一. Mybatis分页插件PageHelper使用 1.不使用插件如何分页: 使用mybatis实现: 1)接口: List<Student> selectStudent(Map< ...
- mybatis分页练手
最近碰到个需求,要做个透明的mybatis分页功能,描述如下:目标:搜索列表的Controller action要和原先保持一样,并且返回的json需要有分页信息,如: @ResponseBody @ ...
- SSM 使用 mybatis 分页插件 pagehepler 实现分页
使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...
- MyBatis 分页插件 PageHelper 使用
1. 引入Maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
随机推荐
- javascript中的this绑定问题
this的绑定规则 1 默认绑定: function foo(){ console.log(this.a); } var a = 2 ; foo(); 调用 foo() 的时候其实相当于 window ...
- Android组件化aar躺坑记:ButterKnife 报 元素值必须为常量表达式错误
背景: 项目有需求,将自己写的模块作为一个module给到大项目使用,所以准备把自己的项目打包成aar包. 一.如何将独立项目打包成aar: 1.修改module下的application 为libr ...
- python:实现几种排序算法
冒泡排序 比较相邻两个字符,如果左边大于右边,则交换位置,遍历一遍字符后,则最大的肯定在最右边:继续循环处理剩下的字符(最右边的不用再比较了,已经最大了) 代码实现: def BubbleSort(s ...
- time 库
time 库的三类函数 时间获取: >>> import time >>> time.time() 1570150181.4052463#单位为秒 >> ...
- [转帖]VirtualBox 网络模式
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://xiaoyu.blog.csdn.net/article/detail ...
- [转帖]Docker公司被收购,开源界尴尬不?
Docker公司被收购,开源界尴尬不? https://news.51cto.com/art/201911/606189.htm Docker公司被谁收了? Docker公司被谁收了?Mirantis ...
- 在linux系统下安装两个nginx以及启动、停止、重起
如果没有安装过nginx请看:linux下nginx部署以及配置详解 1.第一个nginx已经安装完成后,现在安装第二个nginx 启动:sudo /usr/sbin/nginx3 重起:sudo / ...
- C# 获取系统字体方法
//需要引用命名空间 using System.Drawing; using System.Drawing.Text; //获取系统字体方法 public dynamic GetFontNames() ...
- Implicit super constructor Array() is undefined for default constructor. Must define an explicit constructor
因为你的父类已经创建了一个带参的构造函数并且父类中没有无参的构造函数,此时编译器不会为你调用默认的构造函数, 所以子类在继承父类的时候需要在自己的构造函数中显式的调用父类的构造函数,这样才能确保子类在 ...
- python预习day1
计算机基础 cpu 大脑 内存 临时记忆 硬盘 永久记忆 输入设备 眼睛 耳朵 输出设备 嘴巴 操作系统 控制计算机硬件工作流程的 应用程序 安装在操作系统之上的软件 python简介 python是 ...