springboot mybatis项目整合加入PageHelper分页插件

举报
IT 叶新东老师 发表于 2021/12/31 22:03:48 2021/12/31
【摘要】 springboot整合mybaits我就不介绍了,网上大把教程,这里直接讲解整合PageHelper 分页   1、pom.xml加入依赖 <!-- mybatis分页插件pagehelp 数据驱动 --><dependency> <groupId>com.github.pagehe...

springboot整合mybaits我就不介绍了,网上大把教程,这里直接讲解整合PageHelper 分页

 

1、pom.xml加入依赖


  
  1. <!-- mybatis分页插件pagehelp 数据驱动 -->
  2. <dependency>
  3. <groupId>com.github.pagehelper</groupId>
  4. <artifactId>pagehelper</artifactId>
  5. <version>4.1.6</version>
  6. </dependency>

2、在入口类使用Configuration注册分页插件


  
  1. /*
  2. * 注册MyBatis分页插件PageHelper
  3. */
  4. @Configuration
  5. public class MybatisConf {
  6. @Bean
  7. public PageHelper pageHelper() {
  8. System.out.println("MyBatisConfiguration.pageHelper()");
  9. PageHelper pageHelper = new PageHelper();
  10. Properties p = new Properties();
  11. p.setProperty("offsetAsPageNum", "true");
  12. p.setProperty("rowBoundsWithCount", "true");
  13. p.setProperty("reasonable", "true");
  14. pageHelper.setProperties(p);
  15. return pageHelper;
  16. }
  17. }

3、配置文件 在application.yml 加入以下内容,其实不加也可以,默认的配置就够我们用了


  
  1. #pagehelper分页配置
  2. pagehelper:
  3. helperDialect: mysql
  4. reasonable: true
  5. supportMethodsArguments: true
  6. params: count=countSql

4、使用方法,在查询列表之前先new一个分页出来


  
  1. public ResponseCommand list() {
  2. Article searchModel = new Article();
  3. //分页插件-一定要在查询方法之前加上,参数(1,2)表示查询第1页,每页2条记录
  4. Page<Article> objects = PageHelper.startPage(1, 2);
  5. List<Article> list = articleMapper.queryModelList(searchModel);
  6. //查询完成之后就可以使用对象获取总数
  7. System.out.println("总记录数:"+objects.getTotal());
  8. System.out.println("总页数:"+objects.getPageNum());
  9. System.out.println("每页记录数:"+objects.getPageSize());
  10. System.out.println("当前页记录数:"+objects.size());
  11. System.out.println("总页数:"+objects.getPages());
  12. long total = objects.getTotal();
  13. ResponseCommand command = new ResponseCommand();
  14. command.setStatus(true);
  15. command.setResult(list);
  16. return command;
  17. }

是不是很简单呢?

#################以下为全部代码,大神请忽略,新手可以看下###################

pom.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.spring.boot</groupId>
  6. <artifactId>spriongboot-mybatis</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>love-qdf</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.1.1.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-jdbc</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.mybatis.spring.boot</groupId>
  33. <artifactId>mybatis-spring-boot-starter</artifactId>
  34. <version>1.3.2</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>mysql</groupId>
  38. <artifactId>mysql-connector-java</artifactId>
  39. <scope>runtime</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. <!-- mybatis分页插件pagehelp 数据驱动 -->
  47. <dependency>
  48. <groupId>com.github.pagehelper</groupId>
  49. <artifactId>pagehelper</artifactId>
  50. <version>4.1.6</version>
  51. </dependency>
  52. </dependencies>
  53. <build>
  54. <finalName>love-qdf</finalName>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

springboot 入口类


  
  1. package com.spring.boot;
  2. import com.github.pagehelper.PageHelper;
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import java.util.Properties;
  9. @SpringBootApplication
  10. @MapperScan("com.data.dao")
  11. public class SpriongbootMybatisApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(SpriongbootMybatisApplication.class, args);
  14. }
  15. /*
  16. * 注册MyBatis分页插件PageHelper
  17. */
  18. @Configuration
  19. public class MybatisConf {
  20. @Bean
  21. public PageHelper pageHelper() {
  22. System.out.println("MyBatisConfiguration.pageHelper()");
  23. PageHelper pageHelper = new PageHelper();
  24. Properties p = new Properties();
  25. p.setProperty("offsetAsPageNum", "true");
  26. p.setProperty("rowBoundsWithCount", "true");
  27. p.setProperty("reasonable", "true");
  28. pageHelper.setProperties(p);
  29. return pageHelper;
  30. }
  31. }
  32. }

application.yml配置文件.


  
  1. server:
  2. port: 8081
  3. spring:
  4. datasource:
  5. name: SpringBoot-MyBatis
  6. url: jdbc:mysql://192.168.1.101:3306/article_dev?useUnicode=true
  7. username: root
  8. password: 123
  9. # type: com.alibaba.druid.pool.DruidDataSource # 使用druid 数据源
  10. driver-class-name: com.mysql.jdbc.Driver
  11. # dbcp2:
  12. # min-idle: 1
  13. # max-idle: 2
  14. # initial-size: 1
  15. # time-between-eviction-runs-millis: 3000
  16. # min-evictable-idle-time-millis: 300000
  17. # validation-query: SELECT "ZTM" FROM DUAL
  18. # test-while-idle: true
  19. # test-on-borrow: false
  20. # test-on-return: false
  21. mybatis:
  22. mapper-locations: classpath:sqlMapper/*Mapper.xml
  23. type-aliases-package: com.data.model
  24. #pagehelper分页配置
  25. pagehelper:
  26. helperDialect: mysql
  27. reasonable: true
  28. supportMethodsArguments: true
  29. params: count=countSql

 

文章来源: yexindong.blog.csdn.net,作者:java叶新东老师,版权归原作者所有,如需转载,请联系作者。

原文链接:yexindong.blog.csdn.net/article/details/84793738

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。