前言:
目前学习了网络上很多关于MyBatis的分页方式,看到了有很多种,零零散散的文章,于是我就对目前常用的一些分页方式进行学习总结,一条一条进行梳理,思路清晰明了,仅供大家学习和参考!
目录
一、准备工作
二、使用原生Limit关键字进行分页
三、借助MyBabtis提供的RowBounds进行分页查询
四、借助MyBatis提供的第三方PageHelper分页插件
五、借助MyBatis-Plus拦截器进行分页查询
1、创建表结构:
CREATE TABLE `order_info` (
`id` int NOT NULL AUTO_INCREMENT,
`info` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`time` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
2、导入表数据:
INSERT INTO `order_info` VALUES (1, '购买了手机', '2022-08-25 05:34:17');
INSERT INTO `order_info` VALUES (2, '购买了电脑', '2022-08-25 07:30:39');
INSERT INTO `order_info` VALUES (3, '购买了护手霜', '2022-08-17 22:35:07');
INSERT INTO `order_info` VALUES (4, '购买了泡面', '2022-08-23 08:35:36');
INSERT INTO `order_info` VALUES (5, '购买了纸巾', '2022-07-21 15:26:06');
INSERT INTO `order_info` VALUES (6, '购买了自热米饭', '2021-06-20 13:21:06');
INSERT INTO `order_info` VALUES (7, '购买了移动硬盘', '2022-06-11 11:22:03');
INSERT INTO `order_info` VALUES (8, '购买了狗粮', '2022-05-10 11:21:02');
INSERT INTO `order_info` VALUES (9, '购买了猫粮', '2022-04-10 09:11:02');
INSERT INTO `order_info` VALUES (10, '购买了遥控器', '2022-08-22 22:35:07');
INSERT INTO `order_info` VALUES (11, '购买了裤子', '2022-08-15 08:35:36');
INSERT INTO `order_info` VALUES (12, '购买了鞋子', '2022-08-21 08:35:36');
INSERT INTO `order_info` VALUES (13, '购买了水杯', '2022-08-26 19:39:19');
3、导入pom.xml依赖
创建新的工程的步骤我这里就省略了,所以废话不多说,直接进入正题!
注:请不要重复导入MyBatis的依赖,这里为了方便展示写到一起了,实际运用的时候,请不要混合在一起导入,不然可能会导致jar包的冲突,PageHelper和MyBatis-plus的jar包可能会产生冲突的。
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
com.github.pagehelper
pagehelper-spring-boot-starter
1.4.3
com.baomidou
mybatis-plus-boot-starter
3.5.2
4、配置application.yml文件
server:
port: 8080
spring:
datasource:
username: 你的mysql用户名
password: 你的mysql密码
url: jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*.xml
5、创建公用的实体类
package com.ithuang.demo.bean;
import java.util.Date;
public class OrderInfo {
private int id;
private String info;
private Date time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "OrderInfo{" +
"id=" + id +
", info='" + info + '\'' +
", time=" + time +
'}';
}
}
1、创建controller层
package com.ithuang.demo.controller;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.service.OrderInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class OrderInfoController {
@Resource
private OrderInfoService orderInfoService;
@GetMapping("/getOrderInfoList")
public List getOrderInfoList(@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
@RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
return orderInfoService.getOrderInfoList(pageNow,pageSize);
}
}
2、创建service层
package com.ithuang.demo.service;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.mapper.OrderInfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class OrderInfoService {
@Resource
private OrderInfoMapper orderInfoMapper;
public List getOrderInfoList(int pageNow, int pageSize) {
if(pageNow == 1){
pageNow = 0;
}
return orderInfoMapper.getOrderInfoList(pageNow,pageSize);
}
}
3、创建mapper层
package com.ithuang.demo.mapper;
import com.ithuang.demo.bean.OrderInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface OrderInfoMapper {
List getOrderInfoList(@Param("pageNow") int pageNow,@Param("pageSize") int pageSize);
}
4、创建xml文件
5、项目整体结构
6、使用postman进行测试,测试结果如下
RowBounds它是在SQL执行的结果进行截取分页的,所以不适合大量数据的截取和分页,它适合在查询较少的结果集当中使用。
1、创建controller层
package com.ithuang.demo.controller;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.service.OrderInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class OrderInfoController {
@Resource
private OrderInfoService orderInfoService;
@GetMapping("/getOrderInfoList")
public List getOrderInfoList(@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
@RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
return orderInfoService.getOrderInfoList(pageNow,pageSize);
}
}
2、创建Service层
package com.ithuang.demo.service;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.mapper.OrderInfoMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class OrderInfoService {
@Resource
private OrderInfoMapper orderInfoMapper;
public List getOrderInfoList(int pageNow, int pageSize) {
RowBounds rowBounds = new RowBounds(pageNow,pageSize);
return orderInfoMapper.getOrderInfoList(rowBounds);
}
}
3、创建Mapper层
package com.ithuang.demo.mapper;
import com.ithuang.demo.bean.OrderInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.RowBounds;
import java.util.List;
@Mapper
public interface OrderInfoMapper {
List getOrderInfoList(RowBounds rowBounds);
}
4、创建xml文件
5、项目整体结构
6、使用postman进行测试,测试结果如下
在MyBatis中配置了分页拦截器(PageInterceptor),就是在执行相关的Sql之前会做一些拦截的操作,这里通过调用startPage的方法,其实就是在查询getOrderInfoList之前会自动加上limit;这里通过setLocalPage方法,将分页信息保存在当前之后线程当中,查询方法与之处于同一个线程,共享ThreadLocal当中的数据,最后将getOrderInfoList查询好的数据结果放到PageInfo当中即可。
1、创建controller层
package com.ithuang.demo.controller;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.service.OrderInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class OrderInfoController {
@Resource
private OrderInfoService orderInfoService;
@GetMapping("/getOrderInfoList")
public List getOrderInfoList(@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
@RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
return orderInfoService.getOrderInfoList(pageNow,pageSize);
}
}
2、创建Service层
package com.ithuang.demo.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.mapper.OrderInfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class OrderInfoService {
@Resource
private OrderInfoMapper orderInfoMapper;
public List getOrderInfoList(int pageNow, int pageSize) {
PageHelper.startPage(pageNow,pageSize);
List orderInfoList = orderInfoMapper.getOrderInfoList();
PageInfo userPageInfo = new PageInfo<>(orderInfoList);
return userPageInfo.getList();
}
}
3、创建Mapper层
package com.ithuang.demo.mapper;
import com.ithuang.demo.bean.OrderInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface OrderInfoMapper {
List getOrderInfoList();
}
4、编写xml文件
5、项目整体结构
6、使用postman进行测试,测试结果如下
1、编写主配置类
package com.ithuang.demo.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
2、创建controller层
package com.ithuang.demo.controller;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.service.OrderInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class OrderInfoController {
@Resource
private OrderInfoService orderInfoService;
@GetMapping("/getOrderInfoList")
public List getOrderInfoList(@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
@RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
return orderInfoService.getOrderInfoList(pageNow,pageSize);
}
}
3、创建Service层
package com.ithuang.demo.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.mapper.OrderInfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class OrderInfoService {
@Resource
private OrderInfoMapper orderInfoMapper;
public List getOrderInfoList(int pageNow, int pageSize) {
Page page= new Page<>(pageNow,pageSize);
IPage iPage = orderInfoMapper.selectPage(page,null);
return iPage.getRecords();
}
}
4、创建Mapper层
package com.ithuang.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ithuang.demo.bean.OrderInfo;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface OrderInfoMapper extends BaseMapper {
}
5、项目整体结构
6、使用postman进行测试,测试结果如下
以上就是我对目前MyBatis常用的一些分页方式进行的梳理和总结,如果有不对或者遗漏的地方欢迎大家指出!