MyBatis可以使用第三方的插件来对功能进行扩展,分页助手PageHelper是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据:
开发步骤: ① 导入通用PageHelper的坐标
② 在mybatis核心配置文件中配置PageHelper插件
③ 测试分页数据获取
1.导入分页助手依赖 和 语句解析依赖:
<!--分页助手依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<!--语句解析-->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
2. 核心配置类中配置
<?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>
<properties resource="datasourceInfo.properties"></properties>
<typeAliases>
<typeAlias type="com.bihu.Bean.User" alias="User"></typeAlias>
<typeAlias type="java.util.List" alias="List"></typeAlias>
</typeAliases>
<typeHandlers>
<typeHandler handler="com.bihu.handler.DateTypeHandler"></typeHandler>
</typeHandlers>
<!--注意! 插件定义要在mapper 映射标签之前! 【为这里位置好像挺严格的....】-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!--这里指定语言-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bihu/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
3.测试
整个接口 写上映射
查询全部,然后我们在测试哪里分页即可:
里面也写了如何获取分页的各项信息:
package com.bihu.Service;
import com.bihu.Bean.User;
import com.bihu.Dao.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserService {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//下面这个starPage是静态方法。
PageHelper.startPage(2,3);//设置分页 【设置第一页; 每一页三条】
List<User> all = mapper.findAll();//查询全部
for (User item:all
) {
System.out.println(item);
}
//可以new一个PageInFo对象 获取分页的各项参数:
PageInfo<User> pageInfo = new PageInfo<User>(all);
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("每页条数:" + pageInfo.getPageSize());
System.out.println("总条数:" + pageInfo.getTotal());
System.out.println("总页数:" + pageInfo.getPages());
System.out.println("上一页:" + pageInfo.getPrePage());
System.out.println("下一页:" + pageInfo.getNextPage());
System.out.println("是否第一页:" + pageInfo.isIsFirstPage());
System.out.println("是否最后一页" + pageInfo.isIsLastPage());
sqlSession.close();
}
}