springBoot2整合pagehelper的爬坑經歷

  • 2020 年 2 月 24 日
  • 筆記

疫情期間,在家辦公,周末看pageHelper分頁,遂解決問題;

首先,把正確的代碼貼出來,供大家參考:

源碼地址: https://gitee.com/maojindaogg/hake

1、正確的pom配置,重點注意spring-boot2.2.4.RELEASE和pagehelper1.2.12的版本搭配

<parent>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-parent</artifactId>          <version>2.2.4.RELEASE</version>          <relativePath/> <!-- lookup parent from repository -->      </parent>      <groupId>com.heke</groupId>      <artifactId>ssm</artifactId>      <version>0.0.1-SNAPSHOT</version>      <name>ssm</name>      <description>Demo project for Spring Boot</description>        <properties>          <java.version>1.8</java.version>      </properties>        <dependencies>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>          <dependency>              <groupId>org.mybatis.spring.boot</groupId>              <artifactId>mybatis-spring-boot-starter</artifactId>              <version>1.3.2</version>          </dependency>          <dependency>              <groupId>mysql</groupId>              <artifactId>mysql-connector-java</artifactId>              <version>5.1.47</version>              <scope>runtime</scope>          </dependency>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-test</artifactId>              <scope>test</scope>              <exclusions>                  <exclusion>                      <groupId>org.junit.vintage</groupId>                      <artifactId>junit-vintage-engine</artifactId>                  </exclusion>              </exclusions>          </dependency>          <dependency>              <groupId>com.github.pagehelper</groupId>              <artifactId>pagehelper-spring-boot-starter</artifactId>              <version>1.2.12</version>          </dependency>          <dependency>              <groupId>org.projectlombok</groupId>              <artifactId>lombok</artifactId>              <version>1.18.10</version>          </dependency>      </dependencies>        <build>          <plugins>              <plugin>                  <groupId>org.springframework.boot</groupId>                  <artifactId>spring-boot-maven-plugin</artifactId>              </plugin>          </plugins>      </build>

2、application.properties,注意此處可以不配置

##helperDialect: mysql  ##reasonable: true  ##supportMethodsArguments: true  ##parames: count=countSql  server.port=8081    #mysql  spring.datasource.driver-class-name = com.mysql.jdbc.Driver  spring.datasource.url= jdbc:mysql://localhost:3306/hake?serverTimezone=Asia/Shanghai&characterEncoding=utf8  spring.datasource.username = root  spring.datasource.password = longfor    mybatis.mapper-locations = classpath*:mapper/*.xml  mybatis.type-aliases-package=heke.ssm

3、UserMapper

/**   * @Author: Liu Yue   * @Descripition:   * @Date; Create in 2020/2/15 16:00   **/  @Mapper  public interface UserMapper {      List<User> findAll();  }    <mapper namespace="com.heke.ssm.UserMapper">      <select id="findAll" parameterType="java.lang.String" resultType="com.heke.ssm.User" >          select * from user      </select>  </mapper>

4、User

import lombok.Data;    @Data  public class User {      // 主鍵id      private Integer id;      private String name;      private Integer age;  }

5、UserController測試

@Slf4j  @RestController  @RequestMapping("/user")  public class UserController {        @Autowired      private UserMapper userMapper;          @GetMapping("/findAll")      public List<User> findAll(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize) {          PageHelper.startPage(pageNum, pageSize);          List<User> countries = userMapper.findAll();            PageInfo<User> page = PageInfo.of(countries);          System.out.println("每頁展示條數:" + page.getPageSize());          System.out.println("總條數:" + page.getTotal());          System.out.println("當前頁:" + page.getPageNum());          System.out.println("總頁數:" + page.getPages());            return countries;      }  }

6、啟動類SsmApplication啟動類

@SpringBootApplication  @MapperScan("heke.ssm")  public class SsmApplication {        public static void main(String[] args) {          SpringApplication.run(SsmApplication.class, args);      }    }
錯誤的啟動類   @SpringBootApplication(exclude = {           DataSourceAutoConfiguration.class   })   @MapperScan("heke.ssm")   @EnableScheduling   @ComponentScan(value = {"com"})   public class SsmApplication {      public static void main(String[] args) {           SpringApplication.run(SsmApplication.class, args);       }  }   DataSourceAutoConfiguration的類解讀可以參考這位師兄的博客   https://blog.csdn.net/kangsa998/article/details/90231518

排(爬)查(坑)方(經)法(歷): 1、把分頁的正確方法,抽離出一個最簡單的項目; 2、把配置信息往簡單項目添加,得到錯誤的數據,定位錯誤;

最後提供執行sql

CREATE TABLE `user` (    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵',    `name` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '姓名',    `age` int(3) DEFAULT NULL COMMENT '年齡',    PRIMARY KEY (`id`)  ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci