1.导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
2.编写yaml文件
spring:
datasource:
url: jdbc:mysql://你的数据库ip/你的数据库名?useSSL=false
username: 你的数据库账号
password: 你的数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
org:
springframework:
web:
servlet:
handler: debug
3. 编写mybatisPlus分页拦截器
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
4.编写单元测试类进行分页查询测试
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
@Test
void mpFengye(){
LambdaQueryWrapper<User>warpper=new LambdaQueryWrapper<>();
warpper.eq(User::getSex,1);
Page myPage=new Page(1,3);
userService.page(myPage,warpper);
System.out.println(myPage.getTotal());
System.out.println(myPage.getRecords());
}
@Test
void mpFengye2(){
LambdaQueryWrapper<User>warpper=new LambdaQueryWrapper<>();
warpper.eq(User::getSex,1);
Page myPage=new Page(1,3);
userMapper.selectPage(myPage,warpper);
System.out.println(myPage.getTotal());
System.out.println(myPage.getRecords());
}