使用mybatis plus的@Select自界说sql时,怎样实现通用的分页查询?

[复制链接]
发表于 2025-10-22 23:19:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 MyBatis Plus 中使用 @Select 自界说 SQL 实现通用的分页查询,可按以下步调操纵:
1. 设置分页插件

起首,必要在项目中设置 MyBatis Plus 的分页插件。以 Spring Boot 项目为例,在设置类中添加分页插件的 Bean:
  1. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  2. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class MyBatisPlusConfig {
  7.     @Bean
  8.     public MybatisPlusInterceptor mybatisPlusInterceptor() {
  9.         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  10.         interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
  11.         return interceptor;
  12.     }
  13. }
复制代码
2. 界说实体类

假设我们有一个 User 实体类:
  1. import com.baomidou.mybatisplus.annotation.TableName;
  2. @TableName("user")
  3. public class User {
  4.     private Long id;
  5.     private String name;
  6.     private Integer age;
  7.     // 构造函数、Getter 和 Setter 方法
  8.     public User() {}
  9.     public User(Long id, String name, Integer age) {
  10.         this.id = id;
  11.         this.name = name;
  12.         this.age = age;
  13.     }
  14.     public Long getId() {
  15.         return id;
  16.     }
  17.     public void setId(Long id) {
  18.         this.id = id;
  19.     }
  20.     public String getName() {
  21.         return name;
  22.     }
  23.     public void setName(String name) {
  24.         this.name = name;
  25.     }
  26.     public Integer getAge() {
  27.         return age;
  28.     }
  29.     public void setAge(Integer age) {
  30.         this.age = age;
  31.     }
  32.     @Override
  33.     public String toString() {
  34.         return "User{" +
  35.                 "id=" + id +
  36.                 ", name='" + name + '\'' +
  37.                 ", age=" + age +
  38.                 '}';
  39.     }
  40. }
复制代码
3. 界说 Mapper 接口

在 Mapper 接口中使用 @Select 注解编写自界说 SQL,并吸取 Page 对象作为参数:
  1. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import org.apache.ibatis.annotations.Select;
  5. public interface UserMapper extends BaseMapper<User> {
  6.     @Select("SELECT * FROM user WHERE age > #{age}")
  7.     IPage<User> selectUserByAge(Page<User> page, Integer age);
  8. }
复制代码
4. 实现分页查询服务

在服务层调用 Mapper 接口的方法举行分页查询:
  1. import com.baomidou.mybatisplus.core.metadata.IPage;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. @Service
  6. public class UserService {
  7.     @Autowired
  8.     private UserMapper userMapper;
  9.     public IPage<User> getUserByAgePage(Integer pageNum, Integer pageSize, Integer age) {
  10.         Page<User> page = new Page<>(pageNum, pageSize);
  11.         return userMapper.selectUserByAge(page, age);
  12.     }
  13. }
复制代码
5. 调用分页查询方法

在控制器或测试类中调用服务层的方法举行分页查询:
  1. import com.baomidou.mybatisplus.core.metadata.IPage;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class UserController {
  8.     @Autowired
  9.     private UserService userService;
  10.     @GetMapping("/users")
  11.     public IPage<User> getUsers(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam Integer age) {
  12.         return userService.getUserByAgePage(pageNum, pageSize, age);
  13.     }
  14. }
复制代码
表明


  • 分页插件设置:MybatisPlusInterceptor 是 MyBatis Plus 的拦截器,PaginationInnerInterceptor 是分页拦截器,用于实现分页功能。
  • Mapper 接口:@Select 注解中的 SQL 是自界说的查询语句,Page 对象作为参数传入,MyBatis Plus 会主动处理处罚分页逻辑。
  • 服务层:创建 Page 对象,设置当前页码和每页记载数,调用 Mapper 接口的方法举行分页查询。
  • 控制器:吸取前端传入的页码、每页记载数和查询条件,调用服务层的方法举行分页查询并返回结果。
通过以上步调,就可以在 MyBatis Plus 中使用 @Select 自界说 SQL 实现通用的分页查询。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表