mybatis-plus新版本的分页查询操作

@EnableAutoConfiguration
//开不开都一样默认开启
@MapperScan(value = "com.ccc.mapper")
public class MainConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

先添加mybatis-plus的分页插件

@Test
    public void testlist(){
        Page<user> userPage = new Page<>(1, 3);
        usermapper.selectPage(userPage,null);
        userPage.getRecords().forEach(System.out::println);
    }

分页查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值