您好,登录后才能下订单哦!
MyBatis-Plus 是 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率。其中,分页查询是日常开发中非常常见的需求。MyBatis-Plus 提供了多种分页查询的方法,本文将详细介绍这些方法及其使用场景。
Page
对象进行分页查询Page
是 MyBatis-Plus 提供的一个分页对象,通过它可以方便地进行分页查询。
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
public void testPage() {
// 创建分页对象,当前页为1,每页显示10条
Page<User> page = new Page<>(1, 10);
// 创建查询条件
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(User::getAge, 20);
// 执行分页查询
IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
// 获取分页数据
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
long pages = userPage.getPages();
System.out.println("总记录数:" + total);
System.out.println("总页数:" + pages);
System.out.println("当前页数据:" + userList);
}
Page<User>
是分页对象,构造函数中的两个参数分别是当前页码和每页显示的记录数。QueryWrapper<User>
是查询条件构造器,用于构建查询条件。selectPage
方法用于执行分页查询,返回一个 IPage
对象,其中包含了分页数据、总记录数、总页数等信息。PaginationInterceptor
进行分页PaginationInterceptor
是 MyBatis-Plus 提供的一个分页插件,通过配置该插件可以实现自动分页。
PaginationInterceptor
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
配置好 PaginationInterceptor
后,在查询时只需要传入 Page
对象即可,MyBatis-Plus 会自动进行分页。
public void testPaginationInterceptor() {
// 创建分页对象,当前页为1,每页显示10条
Page<User> page = new Page<>(1, 10);
// 执行分页查询
IPage<User> userPage = userMapper.selectPage(page, null);
// 获取分页数据
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
long pages = userPage.getPages();
System.out.println("总记录数:" + total);
System.out.println("总页数:" + pages);
System.out.println("当前页数据:" + userList);
}
PaginationInterceptor
是 MyBatis-Plus 提供的分页插件,配置后会自动拦截 SQL 语句并添加分页逻辑。Page
对象即可,无需手动编写分页 SQL。PageHelper
进行分页PageHelper
是一个常用的分页插件,MyBatis-Plus 也支持与 PageHelper
集成使用。
PageHelper
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class PageHelperConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
pageHelper.setProperties(properties);
return pageHelper;
}
}
PageHelper
进行分页import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
public void testPageHelper() {
// 开始分页,当前页为1,每页显示10条
PageHelper.startPage(1, 10);
// 执行查询
List<User> userList = userMapper.selectList(null);
// 获取分页信息
PageInfo<User> pageInfo = new PageInfo<>(userList);
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("总页数:" + pageInfo.getPages());
System.out.println("当前页数据:" + pageInfo.getList());
}
PageHelper
是一个独立的分页插件,MyBatis-Plus 也支持与其集成使用。PageHelper.startPage
方法用于开始分页,传入当前页码和每页显示的记录数。PageInfo
是 PageHelper
提供的分页信息对象,包含了分页数据、总记录数、总页数等信息。LambdaQueryWrapper
进行分页查询LambdaQueryWrapper
是 MyBatis-Plus 提供的一个基于 Lambda 表达式的查询条件构造器,可以简化查询条件的编写。
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
public void testLambdaQueryWrapper() {
// 创建分页对象,当前页为1,每页显示10条
Page<User> page = new Page<>(1, 10);
// 创建查询条件
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getAge, 20);
// 执行分页查询
IPage<User> userPage = userMapper.selectPage(page, lambdaQueryWrapper);
// 获取分页数据
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
long pages = userPage.getPages();
System.out.println("总记录数:" + total);
System.out.println("总页数:" + pages);
System.out.println("当前页数据:" + userList);
}
LambdaQueryWrapper
是 MyBatis-Plus 提供的一个基于 Lambda 表达式的查询条件构造器,可以简化查询条件的编写。LambdaQueryWrapper
可以避免硬编码字段名,提高代码的可读性和可维护性。Page
和 Wrapper
进行自定义分页查询在某些场景下,可能需要自定义分页查询逻辑,此时可以结合 Page
和 Wrapper
进行自定义分页查询。
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
public void testCustomPage() {
// 创建分页对象,当前页为1,每页显示10条
Page<User> page = new Page<>(1, 10);
// 创建查询条件
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(User::getAge, 20);
// 执行自定义分页查询
IPage<User> userPage = userMapper.selectCustomPage(page, queryWrapper);
// 获取分页数据
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
long pages = userPage.getPages();
System.out.println("总记录数:" + total);
System.out.println("总页数:" + pages);
System.out.println("当前页数据:" + userList);
}
selectCustomPage
是一个自定义的分页查询方法,需要在 UserMapper
中定义。MyBatis-Plus 提供了多种分页查询的方法,开发者可以根据实际需求选择合适的方式。无论是使用 Page
对象、PaginationInterceptor
插件,还是与 PageHelper
集成,MyBatis-Plus 都能满足大多数分页查询的需求。对于复杂的业务场景,还可以结合 Wrapper
进行自定义分页查询,灵活性非常高。
通过本文的介绍,相信大家对 MyBatis-Plus 的分页查询方法有了更深入的了解,能够在实际开发中灵活运用这些方法,提高开发效率。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。