疫情期间,在家办公,周末看pageHelper分页,遂解决问题;
首先,把正确的代码贴出来,供大家参考:
源码地址: https://gitee.com/maojindaogg/hake
1、正确的pom配置,重点注意spring-boot2.2.4.RELEASE和pagehelper1.2.12的版本搭配
代码语言:javascript复制<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,注意此处可以不配置
代码语言:javascript复制##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
代码语言:javascript复制/**
* @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
代码语言:javascript复制import lombok.Data;
@Data
public class User {
// 主键id
private Integer id;
private String name;
private Integer age;
}
5、UserController测试
代码语言:javascript复制@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启动类
代码语言:javascript复制@SpringBootApplication
@MapperScan("heke.ssm")
public class SsmApplication {
public static void main(String[] args) {
SpringApplication.run(SsmApplication.class, args);
}
}
代码语言:javascript复制错误的启动类
@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
代码语言:javascript复制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