MyBatisPlus分页及相关错误
遇相关报错则持续更新
基本使用
导入mybatisPlus整合springboot相关依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
配置mybatis分页配置类
@Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
services中基本使用
@Service public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService { @Autowired private EmployeeMapper employeeMapper; @Override public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) { //开启分页 Page<Employee> page = new Page<>(currentPage,size); IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope); System.out.println(employeeByPage.getTotal()); RespPageBean respPageBean = new RespPageBean(employeeByPage.getTotal(),employeeByPage.getRecords()); return respPageBean; } }
分析:
mybatis分页配置类的代码为新版本的代码,旧版本的代码在此不提供,有兴趣请移步mybatisplus官网https://mp.baomidou.com/guide/page.html
,配置中设置数据库为Mysql类型,如您使用的是其他数据库可进行切换配置。
关于分页代码的使用,预先定义一个Page类,传入当前页码和每页尺寸即可,在使用中只需将其放入mapper查询方法中,注意参数位置为第一个,数据返回信息有总条数,每页数据,每页尺寸和当前页码。
相关细节及报错
1.getTotal()方法打印不出数据
在数据输出时,getRecords()可以打印出查询的所有数据,但是每页的总条数始终都是0,遇到此种情况,我的是因为配置类中添加了
@Configuration
注解却忘记添加@Bean
注解,低级错误,各位共勉