在使用pagehelper 的时候,由于pagehelper有不同版本(有的是基于sping 的 ,有的是基于spingboot 的)
    如果直接在maven依赖中直接引用springboot pagehelper,使用springBoot 版本的PH 是不需要在注入pagehelper Bean 的,可以直接在程序中使用

    但是如果只是在maven中引入PH 的依赖,那么就需要对pagehelper 进行Bean注入,下面是实现Bean注入的代码:

    1. @Configuration
    2. public class MybatisConf {
    3. @Bean
    4. public PageHelper pageHelper() {
    5. PageHelper pageHelper = new PageHelper();
    6. Properties p = new Properties();
    7. p.setProperty("offsetAsPageNum", "true");
    8. p.setProperty("rowBoundsWithCount", "true");
    9. p.setProperty("reasonable", "true");
    10. pageHelper.setProperties(p);
    11. return pageHelper;

    当然,如果项目本身就是springboot项目的话,最好还是使用spring boot版本的pagehelper.

    在如下配置中,yml中的文件能够被springboot自动进行加载

    1. ##
    2. server:
    3. port: 8080
    4. ##
    5. spring:
    6. #数据源配置
    7. datasource:
    8. username: root
    9. password:
    10. url: jdbc:mysql://localhost:3306/nongchatea?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    11. driver-class-name: com.mysql.cj.jdbc.Driver
    12. type: com.alibaba.druid.pool.DruidDataSource
    13. #druid 配置
    14. druid:
    15. initialSize: 5
    16. minIdle: 5
    17. maxActive: 20
    18. maxWait: 60000
    19. timeBetweenEvictionRunsMillis: 60000
    20. minEvictableIdleTimeMillis: 300000
    21. validationQuery: SELECT 1 FROM DUAL
    22. testWhileIdle: true
    23. testOnBorrow: false
    24. testOnReturn: false
    25. poolPreparedStatements: true
    26. #自动设置json返回格式
    27. jackson:
    28. #日期格式化
    29. date-format: yyyy-MM-dd
    30. time-zone: GMT+8
    31. #设置空如何序列化
    32. default-property-inclusion: non_null
    33. serialization:
    34. #格式化输出
    35. indent_output: true
    36. #忽略无法转换的对象
    37. fail_on_empty_beans: false
    38. deserialization:
    39. #允许对象忽略json中不存在的属性
    40. fail_on_unknown_properties: false
    41. parser:
    42. #允许出现特殊字符和转义符
    43. allow_unquoted_control_chars: true
    44. #允许出现单引号
    45. allow_single_quotes: true
    46. #文件上传限制
    47. servlet:
    48. multipart:
    49. max-request-size: 100MB
    50. max-file-size: 50MB
    51. ##mybatis设置
    52. mybatis:
    53. mapper-locations: classpath:mapping/*Mapper.xml
    54. type-aliases-package: com.example.springbootmybatis.pojo
    55. configuration:
    56. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    57. map-underscore-to-camel-case: true
    58. ##pagehelper设置
    59. pagehelper:
    60. helperDialect: mysql
    61. reasonable: true
    62. supportMethodsArguments: true
    63. params: count=countSql
    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.5.3</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.example</groupId>
    12. <artifactId>SpringBootMybatis</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>SpringBootMybatis</name>
    15. <description>Demo project for Spring Boot</description>
    16. <properties>
    17. <java.version>11</java.version>
    18. </properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.boot</groupId>
    22. <artifactId>spring-boot-starter-data-jdbc</artifactId>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-starter-web</artifactId>
    27. </dependency>
    28. <dependency>
    29. <groupId>org.mybatis.spring.boot</groupId>
    30. <artifactId>mybatis-spring-boot-starter</artifactId>
    31. <version>2.2.0</version>
    32. </dependency>
    33. <dependency>
    34. <groupId>mysql</groupId>
    35. <artifactId>mysql-connector-java</artifactId>
    36. <scope>runtime</scope>
    37. </dependency>
    38. <!-- 注意使用springboot适配的druid-->
    39. <dependency>
    40. <groupId>com.alibaba</groupId>
    41. <artifactId>druid-spring-boot-starter</artifactId>
    42. <version>1.2.4</version>
    43. </dependency>
    44. <dependency>
    45. <groupId>org.projectlombok</groupId>
    46. <artifactId>lombok</artifactId>
    47. <optional>true</optional>
    48. </dependency>
    49. <dependency>
    50. <groupId>org.springframework.boot</groupId>
    51. <artifactId>spring-boot-starter-test</artifactId>
    52. <scope>test</scope>
    53. </dependency>
    54. <dependency>
    55. <groupId>com.fasterxml.jackson.core</groupId>
    56. <artifactId>jackson-core</artifactId>
    57. </dependency>
    58. <dependency>
    59. <groupId>log4j</groupId>
    60. <artifactId>log4j</artifactId>
    61. <version>1.2.17</version>
    62. </dependency>
    63. <dependency>
    64. <groupId>org.jetbrains</groupId>
    65. <artifactId>annotations</artifactId>
    66. <version>RELEASE</version>
    67. <scope>compile</scope>
    68. </dependency>
    69. <!-- 注意使用springboot适配的pagehelper,否则需要自己注入bean-->
    70. <dependency>
    71. <groupId>com.github.pagehelper</groupId>
    72. <artifactId>pagehelper-spring-boot-starter</artifactId>
    73. <version>1.3.0</version>
    74. </dependency>
    75. <dependency>
    76. <groupId>org.springframework.boot</groupId>
    77. <artifactId>spring-boot-configuration-processor</artifactId>
    78. </dependency>
    79. </dependencies>
    80. <build>
    81. <plugins>
    82. <plugin>
    83. <groupId>org.springframework.boot</groupId>
    84. <artifactId>spring-boot-maven-plugin</artifactId>
    85. <configuration>
    86. <excludes>
    87. <exclude>
    88. <groupId>org.projectlombok</groupId>
    89. <artifactId>lombok</artifactId>
    90. </exclude>
    91. </excludes>
    92. </configuration>
    93. </plugin>
    94. </plugins>
    95. </build>
    96. </project>