mybatis的分页插件pagehelper的使用

1.今天实验了mybatis的分页插件pagehelper的使用,感觉这个东西很方便,

公司项目居然连mybatis-config.xml文件都没写,直接写了个中间xml然后在spring中通过improt引入的

自己写了了mybatis-config.xml文件,中间xml文件已经配置了扫描包,所以

只有pagehelper配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 配置管理器 -->
<configuration>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql" />
</plugin>
</plugins>
</configuration>

在sqlSessionFactory中引入

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" /><!--引入mybatis-config.xml文件 -->
<property name="typeAliasesPackage" value="com.heb.user.model" />
</bean>

然后在service的实现层写入一下代码

PageHelper.startPage(page,count);
List<ResellModel> resellList = numeralLoginDao.showAllResellInfos();

PageInfo pageResult = new PageInfo<>(resellList);

page和count是两个传入的参数

pageResult就是得到的分页结果

返回到页面信息有

"firstPage":1,"prePage":0,"nextPage":2,"lastPage":2,"isFirstPage":true,"isLastPage":false,"hasPreviousPage":false,"hasNextPage":true,"navigatePages":8,"navigatepageNums":[1,2]}}

可通过对应的信息取出你所需要的值

原文地址:https://www.cnblogs.com/1736gerr/p/6934642.html