【第29章】MyBatis-Plus之分页插件
媒介MyBatis-Plus 的分页插件 PaginationInnerInterceptor 提供了强大的分页功能,支持多种数据库,使得分页查询变得简单高效。
一、支持的数据库
PaginationInnerInterceptor 支持广泛的数据库,包括但不限于:
https://i-blog.csdnimg.cn/direct/87855a2bdbd84f9bb50c6ef6b858e8f8.png
如果你需要支持的数据库不在列表中,可以通过 Pull Request 哀求添加。
覆盖范围很广,已经支持了众多国产数据库。
二、配置方法
在 Spring Boot 项目中,你可以通过 Java 配置来添加分页插件:
@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
return interceptor;
}
}
三、属性介绍
PaginationInnerInterceptor 提供了以下属性来定制分页活动:
属性名类型默认值描述overflowbooleanfalse溢出总页数后是否进行处理maxLimitLong单页分页条数限定dbTypeDbType数据库类型dialectIDialect方言实现类 发起单一数据库类型的均设置 dbType
四、自界说 Mapper 方法中使用分页
你可以通过以下方式在 Mapper 方法中使用分页:
IPage<UserVo> selectPageVo(IPage<?> page, Integer state);
// 或者自定义分页类
MyPage selectPageVo(MyPage page);
// 或者返回 List
List<UserVo> selectPageVo(IPage<UserVo> page, Integer state);
对应的 XML 配置:
<select id="selectPageVo" resultType="xxx.xxx.xxx.UserVo">
SELECT id,name FROM user WHERE state=#{state}
</select>
如果返回类型是 IPage,则入参的 IPage 不能为 null。如果想暂时不分页,可以在初始化 IPage 时 size 参数传入小于 0 的值。 如果返回类型是 List,则入参的 IPage 可以为 null,但需要手动设置入参的 IPage.setRecords(返回的 List)。 如果 XML 需要从 page 里取值,需要使用 page.属性 获取。
五、其他注意事项
[*]天生 countSql 时,如果 left join 的表不参与 where 条件,会将其优化掉。发起在任何带有 left join 的 SQL 中,都给表和字段加上别名。
[*]在使用多个插件时,请将分页插件放到插件执行链的最后面,以制止 COUNT SQL 执行不准确的题目。
六、Page 类
Page 类继承了 IPage 类,实现了简单分页模型。如果你需要实现自己的分页模型,可以继承 Page 类或实现 IPage 类。
属性名类型默认值描述recordsListemptyList查询数据列表totalLong0查询列表总记录数sizeLong10每页表现条数,默认 10currentLong1当前页ordersListemptyList排序字段信息optimizeCountSqlbooleantrue主动优化 COUNT SQLoptimizeJoinOfCountSqlbooleantrue主动优化 COUNT SQL 是否把 join 查询部门移除searchCountbooleantrue是否进行 count 查询maxLimitLong单页分页条数限定countIdStringXML 自界说 count 查询的 statementId 七、实战
1. 配置类
package org.example.springboot3.mybatisplus.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Create by zjg on 2024/7/7
*/
@Configuration
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
return interceptor;
}
}
2. 分页类
package org.example.springboot3.mybatisplus.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.springboot3.bigevent.entity.Result;
import org.example.springboot3.mybatisplus.model.User;
import org.example.springboot3.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Create by zjg on 2024/7/7
*/
@RestController
@RequestMapping("/page/")
public class PageController {
@Autowired
UserService userService;
@GetMapping
Result<List<User>> page(@RequestParam Integer pageNum,@RequestParam Integer pageSize, String name) {
// 假设有一个 QueryWrapper 对象,进行有条件的分页查询
IPage<User> page = new Page<>(pageNum, pageSize);
LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(User.class);
if(StringUtils.hasLength(name)){
queryWrapper.eq(User::getName, name);
}
IPage<User> userPage = userService.page(page, queryWrapper); // 调用 page 方法
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
System.out.println("Total users: " + total);
return Result.success("请求成功",userList);
}
}
3. 测试
https://i-blog.csdnimg.cn/direct/f07a5561e9274b82be3f1356571045ed.png
总结
回到顶部
通过这些配置和使用方法,你可以轻松地在 MyBatis-Plus 中实现分页查询,提高应用的性能和用户体验。
更多插件内容请访问官网
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]