SpringBoot2整合MyBatis-plus中使用PageHelper(为了避免jar依赖冲突)
因为看到很多朋友在SpringBoot2-MyBatis-plus整合项目中引入PageHelper分页工具出现问题,
特地记录一下笔者的使用方式。
其主要问题还是因为pagehelper-spring-boot-starter所依赖的mybatis-spring,mybatis与
mybatis-plus-boot-starter所依赖的mybatis-spring,mybatis发生冲突只要排除掉即,以下是解决方案。
一、在SpringBoot2项目中引入相关依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
二、application.yml配置(properties一样的道理)
pagehelper:
# 指定使用的数据库数据库
helperDialect: mysql
# reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
reasonable: true
三、Service代码示例
public PageInfo getEmployeeByPage(Integer page, Integer size,String keyWord) {
Page<Object> pageObject = PageHelper.startPage(page, size);
List<Employee> emps = employeeMapper.getEmployeeByPage(keyWord);
PageInfo<Employee> info = new PageInfo<>(emps);
return info;
}
四、PageInfo类封装的信息详情
private int pageNum;
private int pageSize;
private int size;
private int startRow;
private int endRow;
private long total;
private int pages;
private List<T> list;
private int prePage;
private int nextPage;
private boolean isFirstPage;
private boolean isLastPage;
private boolean hasPreviousPage;
private boolean hasNextPage;
private int navigatePages;
private int[] navigatepageNums;
private int navigateFirstPage;
private int navigateLastPage;