SpringBoot整合mybatis-pagehelper实现分页
1. 引入分页插件依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
2. 配置yml
# 分页插件配置 pagehelper: helperDialect: mysql supportMethodsArguments: true
3.分页数据封装到PagedGridResult类
package com.qing.springbootmould.utils; import com.github.pagehelper.PageInfo; import java.util.List; /** * * @Title: PagedGridResult.java * @Package com.imooc.utils * @Description: 用来返回分页Grid的数据格式 * Copyright: Copyright (c) 2019 */ public class PagedGridResult { private int page; // 当前页数 private int total; // 总页数 private long records; // 总记录数 private List<?> rows; // 每行显示的内容 public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public long getRecords() { return records; } public void setRecords(long records) { this.records = records; } public List<?> getRows() { return rows; } public void setRows(List<?> rows) { this.rows = rows; }
//分页数据进行封装到PagedGridResult类,传给前端 public static PagedGridResult setterPagedGrid(List<?> list, Integer page) { PageInfo<?> pageList = new PageInfo<>(list); PagedGridResult grid = new PagedGridResult(); grid.setPage(page); grid.setRows(list); grid.setTotal(pageList.getPages()); grid.setRecords(pageList.getTotal()); return grid; } }
4. 分页的使用
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qing.springbootmould.dao.UserMapper"> <!--查询全部用户--> <select id="findAll" resultType="com.qing.springbootmould.pojo.User"> select * from user </select> </mapper>
dao层
package com.qing.springbootmould.dao; import com.qing.springbootmould.pojo.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { User findUser(Integer id); List<User> findAll(); }
service层,在这进行分页
UserService 接口
public interface UserService { //分页接口 PagedGridResult findAll(Integer page, Integer pageSize); }
UserServiceImpl 实现类
package com.qing.springbootmould.service.impl; import com.github.pagehelper.PageHelper; import com.qing.springbootmould.dao.UserMapper; import com.qing.springbootmould.pojo.User; import com.qing.springbootmould.service.UserService; import com.qing.springbootmould.utils.PagedGridResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public PagedGridResult findAll(Integer page, Integer pageSize) { /** * page: 第几页 * pageSize: 每页显示条数 */ PageHelper.startPage(page, pageSize); // PageHelper.startPage(page, pageSize)方法必须放在业务查询的上方 List<User> userList = userMapper.findAll(); PagedGridResult pagedGridResult = PagedGridResult.setterPagedGrid(userList, page); return pagedGridResult; } }
TestController类
package com.qing.springbootmould.controller; import com.qing.springbootmould.pojo.Result; import com.qing.springbootmould.pojo.User; import com.qing.springbootmould.service.UserService; import com.qing.springbootmould.utils.PagedGridResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import sun.awt.SunHints; import java.util.List; import java.util.Map; @RestController public class TestController { @Autowired private UserService userService; // http://localhost:8080/query3?page=1&pageSize=2 @GetMapping("/query3") public Result findUser3(@RequestParam(name="page") Integer page, @RequestParam(name="pageSize") Integer pageSize){ PagedGridResult gridResult = userService.findAll(page,pageSize); return Result.ok(gridResult); } }
测试结果:
分类:
Springboot的整合
, Mybatis
标签:
mybatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南