SpringBoot2整合MyBatis-plus中使用PageHelper

SpringBoot2整合MyBatis-plus中使用PageHelper(为了避免jar依赖冲突)

	因为看到很多朋友在SpringBoot2-MyBatis-plus整合项目中引入PageHelper分页工具出现问题,
特地记录一下笔者的使用方式。
	其主要问题还是因为pagehelper-spring-boot-starter所依赖的mybatis-spring,mybatis与
mybatis-plus-boot-starter所依赖的mybatis-spring,mybatis发生冲突只要排除掉即,以下是解决方案。

一、在SpringBoot2项目中引入相关依赖

<!--数据库连接工具(这里使用mysql为例)-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    <version>5.1.38</version>
</dependency>

<!--mybatis-plus-boot-starter 启动器依赖
	需要注意的是在引入mybatis-plus-boot-starter后不需要再次单独引入mybatis与mybatis-spring
	因为mybatis-plus-boot-starter的pom已经依赖了mybatis与mybatis-spring
-->
<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>
    <!--使用spring boot2整合 pagehelper-spring-boot-starter必须排除一下依赖 
		因为pagehelper-spring-boot-starter也已经在pom依赖了mybatis与mybatis-spring
		所以会与mybatis-plus-boot-starter中的mybatis与mybatis-spring发生冲突
	-->
    <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) {
    //在mapper方法前通过指定page页码与size每页条数
    Page<Object> pageObject = PageHelper.startPage(page, size);
    //这里就无须指定页码与条数并且该方法的对应sql也无须写limit
    //sql示例 select * from employee where name = #{keyword}(注意无须写limit分页)
    List<Employee> emps = employeeMapper.getEmployeeByPage(keyWord);
    //PageInfo中包装了分页信息
    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;
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值