基本使用
将分页插件注入到bean中
@Configuration
public class MPConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
使用BaseMapper中提供的分页方法
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.gt(User::getAge, 30);
Page<User> page = new Page<>(1, 2);
IPage<User> users = userMapper.selectPage(page, lambdaQueryWrapper);
System.out.println(users.getTotal());
System.out.println(users.getRecords());
自定义分页
实际开发中有的分页查询是多表联查的,可以通过自定义SQL查询的方式
- 注入分页插件
@Configuration
public class MPConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
- 定义mapper接口
IPage<User> selectPageUser(Page<User> page, @Param(Constants.WRAPPER)Wrapper<User> wrapper);
- 定义XML文件
<select id="selectPageUser" resultType="com.naruto.mamba.entity.User">
select * from user ${ew.customSqlSegment}
</select>
- 使用分页
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.gt(User::getAge, 30);
Page<User> page = new Page<>(1, 2);
IPage<User> users = userMapper.selectPageUser(page, lambdaQueryWrapper);