1.引入maven
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
2.必须写配置类(分页拦截器)
@Configuration
public class MybatisPlusConfig {
/**新版 这上下两个的配置是相同的 都是创建分页拦截器,注册到Spring容器中*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
/** 旧版 用 PaginationInterceptor **/
// @Bean
// public PaginationInterceptor paginationInterceptor() {
// return new PaginationInterceptor();
// }
}
3.后台代码(测试用)可以根据前台传的页码和大小来查找数据
@RequestMapping("test")
@ResponseBody
public void test(){
Page<User> page = new Page<>(1,2);
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
Page<User> page1 = userService.page(page, lambdaQueryWrapper);
List<User> records = page1.getRecords();
records.forEach(System.out::println);
}
|