SpringBoot Mybatis分页 SpringBoot+Mybatis分页插件PageHelper实现分页效果
Singinwind 人气:0最近刚入职新公司,项目是从零开始搭建的项目。我觉得是时候考验是驴还是千里马的时候。都是泪就不多说了。
附上一篇Mybatis常用的分页案例。这次要做的是最常见的分页效果,也是基础功能。但是很多人都做不好的。这次采用Mybatis分页插件PageHelper。
仅献给伸手党的大爷们。大爷们好!拿代码记得扣666!!小的在这给感谢了!!
一、项目结构
按照controller,service,mapper(也叫dao)来建立项目,utils里面是两个自己写的分页工具类。看前
端需要什么分页参数,你方便添加。并能返回给他
二、插件引入
1.pom.xml。引入PageHelper插件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.simons</groupId> <artifactId>SimonsStudio</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SimonsStudio</name> <description>SimonsStudio</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Mybatis框架--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--pageHelper分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application.xml配置。我的*maper.xml都是放在resource文件夹里面的。这里有个 mapper-locations一定要配你项目的,不懂的看我的项目结构。PageHelper照着配置就行了。
server: port: 8080 spring: datasource: name: test url: jdbc:mysql://127.0.0.1:3306/simonsdb username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml #注意了!这个是mapper.xml的路径。要按照目录文件夹来命名。 type-aliases-package: com.smions.entity #实体类的文件夹 pagehelper: # helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
三、代码
1.mapper。习惯上从dao层写起来。@Mapper@Component两个注解要写上,不然后面springboot启动的时候扫描不到。在Mybatis中。mapper相当于dao层。只有接口。selectPage这个是我们要做的分页,这个名字和Maper.xml里面的id对应。要写对了。
package com.simons.mapper; import com.simons.entity.Student; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @Mapper @Component public interface StudentMapper { /** * 查询全部用户 * @return */ List<Map<String,Object>> selectAll(); /** * 分页查询用户 * @return */ List<Student> selectPage(); }
2.*mapper.xml。namespace对应Java中的mapper类。id = "Base_Column_List",是你要查询的表里面的所有字段。记得username不要写成name.这个我踩过好多坑。运行的时候一直报mapper找不到对应的方法。原因是name会被识别为关键字!!不要取名sql或者java的关键字,不然你会哭的!!
<?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.simons.mapper.StudentMapper" > <sql id="Base_Column_List" > id, username, password </sql> <select id="selectAll" resultType="map"> SELECT <include refid="Base_Column_List" /> FROM student </select> <select id="selectPage" resultType="map"> SELECT <include refid="Base_Column_List" /> FROM student </select> </mapper>
3.service.java。getPageInfo方法是重点。PageHelper的核心代码就只有一句:
PageHelper.startPage(pageNum,pageSize);//开始分页
List<Student> students = studentMapper.selectPage();//查询数据库返回数据
PageInfo<Student> pageInfo = new PageInfo<>(students);//得到数据,里面还带有分页的各个参数
很简单是不是。接下来介绍一下pageInfo自带的分页参数:
pageNum=5, //查询的页数(第5页)
pageSize=3, //每页的大小
size=3,
startRow=13,
endRow=15,
total=22,//总数
pages=8, //总页数:22/3=8
list=Page{count=true, pageNum=5, pageSize=3, startRow=12, endRow=15, total=22, pages=8, reasonable=true, pageSizeZero=false},
prePage=4, //前一页
nextPage=6, //后一页
isFirstPage=false, //是否是第一页
isLastPage=false, //是否是最后一页
hasPreviousPage=true, //是否有上一页
hasNextPage=true, //是否有下一页
navigatePages=8, //导航总页数
navigateFirstPage=1, //导航第一页
navigateLastPage=8, //导航最后一页
navigatepageNums=[1, 2, 3, 4, 5, 6, 7, 8]//页数码
看完后,是不是一个很完美的插件(狗头)。相关的分页参数都是很齐全的。
service代码。这里我先用getInfo拿到PageInfo。再把相关的参数和list数据拿出来。返回给controller即可。
package com.simons.service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.simons.entity.Student; import com.simons.mapper.StudentMapper; import com.simons.util.PageRequest; import com.simons.util.PageResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @Service public class StudentService{ @Autowired private StudentMapper studentMapper; public List<Map<String,Object>> findAll() { // List<Student> students = List<Map<String,Object>> map = studentMapper.selectAll(); return map; } public PageResult findPage(PageRequest pageRequest) { PageInfo<Student> pageInfo = getPageInfo(pageRequest); PageResult pageResult = new PageResult(); pageResult.setPageNum(pageInfo.getPageNum()); pageResult.setPageSize(pageInfo.getPageSize()); pageResult.setTotalPages(pageInfo.getPages()); pageResult.setContent(pageInfo.getList()); return pageResult; } /** * 调用分页插件完成分页 * */ public PageInfo<Student> getPageInfo(PageRequest pageRequest) { int pageNum = pageRequest.getPageNum(); int pageSize = pageRequest.getPageSize(); PageHelper.startPage(pageNum,pageSize);//核心代码:pageHelper查询分页 List<Student> students = studentMapper.selectPage(); PageInfo<Student> pageInfo = new PageInfo<>(students); return pageInfo; } }
4.controller层。findpage方法。查找分页的方案。因为分页往往带有查询参数。这边没写。
package com.simons.controller; import com.simons.service.StudentService; import com.simons.util.PageRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; //@RestController //@RequestMapping("/student") @Controller @RequestMapping(value = {"/student"}) public class StudentController { @Autowired private StudentService studentService; @ResponseBody @RequestMapping(value="/findAll") public List<Map<String,Object>> findAll() { return studentService.findAll(); } @ResponseBody @RequestMapping(value = {"/findPage"}) public Object findPage(@RequestParam(name = "pageNum")String pageNum , @RequestParam(name = "pageSize") String pageSize) { PageRequest pageQuery = new PageRequest(); pageQuery.setPageNum(Integer.parseInt(pageNum)); pageQuery.setPageSize(Integer.parseInt(pageSize)); return studentService.findPage(pageQuery); } // @PostMapping(value="/findPage") // public Object findPage(@RequestBody PageRequest pageQuery) { // return studentService.findPage(pageQuery); // } }
5.utils类。一个是pageRequest请求分页,一个是pageResult返回分页的实体类
package com.simons.util; import java.util.List; /** * 分页返回结果 */ public class PageResult { /** * 当前页码 */ private int pageNum; /** * 每页数量 */ private int pageSize; /** * 记录总数 */ private long totalSize; /** * 页码总数 */ private int totalPages; /** * 数据模型 */ private List<?> content; public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public long getTotalSize() { return totalSize; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public List<?> getContent() { return content; } public void setContent(List<?> content) { this.content = content; } }
package com.simons.util; public class PageRequest { /** * 当前页码 */ private int pageNum; /** * 每页数量 */ private int pageSize; public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
6.spring项目启动 application.java
package com.simons; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @MapperScan("com.simons.mapper") //@ComponentScan(basePackages={"com.simons.*"}) public class SimonsStudioApplication { public static void main(String[] args) { SpringApplication.run(SimonsStudioApplication.class, args); } }
四、测试:
启动spring。输入测试地址:
http://localhost:8080/student/findPage?pageNum=5&pageSize=3
加载全部内容