MyBatis-Plus分页查询教程

MyBatis-Plus分页查询教程

MyBatis-Plus是MyBatis的一个增强工具,用于简化开发。其中一个很强大的特点就是分页查询。在本教程中,我们将探索如何使用MyBatis-Plus进行分页查询。

1. 前置条件

确保你的项目已经集成了MyBatis-Plus。首先,你需要将MyBatis-Plus的依赖加入到你的pom.xml文件中:

<!-- MyBatis-Plus 核心库 -->
<dependency>
    <groupId>com.baomidou.mybatisplus</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>版本</version>
</dependency>

2. 配置分页插件

首先,你需要在MyBatis配置中添加分页插件。

在Spring Boot的配置类中添加以下代码:

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();
    }
}

这样,当你的应用启动时,MyBatis-Plus的分页插件将自动配置并启动。

3. 实现分页查询

假设我们有一个名为User的实体类和一个对应的UserMapper

3.1 在Service中创建一个分页方法

UserService接口中定义一个分页方法:

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;

public interface UserService {
    Page<User> getUsersByPage(int currentPage, int pageSize);
}

现在,我们将实现该方法。在UserServiceImpl中:

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {
    
    @Override
    public Page<User> getUsersByPage(int currentPage, int pageSize) {
        Page<User> page = new Page<>(currentPage, pageSize);
        return baseMapper.selectPage(page, null);
    }
}

3.2 在Controller中使用

在你的UserController中,你可以调用上面创建的分页方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("/users")
    public Page<User> getUsersByPage(
        @RequestParam(defaultValue = "1") int currentPage,
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        return userService.getUsersByPage(currentPage, pageSize);
    }
}

访问/users,并使用currentPagepageSize参数,你可以得到分页的用户数据。

4. 结束语

使用MyBatis-Plus进行分页查询是一个简单而强大的功能,特别适合初学者。只需几个简单的步骤,你就可以为你的应用添加分页功能。希望本教程对你有所帮助,并鼓励你更深入地探索MyBatis-Plus的其他功能!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 是一个 MyBatis 的增强工具包,提供了很多方便的功能和特性,其中包括分页查询插件。使用 MyBatis-Plus分页查询插件可以简化分页查询的操作。 在 MyBatis-Plus 中,分页查询可以通过 `Page` 对象和 `PageHelper` 工具类来实现。下面是使用 MyBatis-Plus 进行分页查询的示例代码: 1. 首先,添加 MyBatis-Plus 和分页插件的依赖到你的项目中。你可以在项目的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 2. 创建一个 `Page` 对象,并设置分页参数: ```java // 创建一个 Page 对象 Page<User> page = new Page<>(current, size); // 设置分页参数 page.setPages(current); // 当前页码 page.setSize(size); // 每页显示的记录数 ``` 3. 在 MyBatis 的 Mapper 接口中使用 `@Param` 注解传递 `Page` 对象,并在 SQL 中使用 MyBatis-Plus 提供的分页查询方法: ```java @Mapper public interface UserMapper extends BaseMapper<User> { List<User> selectUserPage(@Param("page") Page<User> page, @Param("name") String name); } ``` 4. 在 Service 层中调用分页查询方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUserPage(long current, long size, String name) { Page<User> page = new Page<>(current, size); return userMapper.selectUserPage(page, name); } } ``` 这样,你就可以使用 MyBatis-Plus分页查询插件进行分页查询了。记得在你的 SQL 语句中使用 MyBatis-Plus 提供的分页查询方法,而不是传统的 LIMIT 语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值