1、拦截器 controller ---- MyBatisPlusConfig
package com.wt.config; import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; @MapperScan("com.wt.mapper") @EnableTransactionManagement // @Configuration // 配置类 public class MyBatisPlusConfig { // 1.乐观锁 @Bean public OptimisticLockerInterceptor optimisticLockerInterceptor(){ return new OptimisticLockerInterceptor(); } // 分页插件 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
2、测试类
@Test public void usePage(){ /* * current 当前页 * size 大小 * getRecords 获取分页 * */ Page<User> page = new Page<>(1, 3); Page<User> userPage = userMapper.selectPage(page, null); for (User record : userPage.getRecords()) { System.out.println(record); } // 总共有多少页 System.out.println(userPage.getTotal()); }