Mybatis利用分页插件PageHelper快速实现分页查询
Mybatis算是对数据库操作的利器了。但是在处理分页的时候,Mybatis并没有什么特别的方法,一般需要自己去写limit子句实现,成本较高。好在有国内开发者写了一个PageHelper插件,可以帮助我们快速实现分页查询。
官网地址
首先创建一个Maven项目导入相关依赖:
<!-- 依赖列表--> <dependencies><!--mysql驱动--><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version></dependency><!--单元测试--><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope></dependency><!--pageHelper依赖--><dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.1</version></dependency><!--mybatis依赖--><dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version></dependency><!--log4j日志依赖--><dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version></dependency> </dependencies>
记住导入PageHelper依赖:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.2.1</version></dependency>数据库中创建一张表
设置Mybatis配置文件在resources目录下,创建一个sqlMapConfig.xml文件,用于做一些全局的mybatis基本的配置,例如数据库连接信息,事务,mapper映射文件路径,日志实现,类的别名,也包含了分页插件的基本配置。
<configuration><!--使用外部属性配置文件 resource:指定类路径下的某个属性配置文件--><properties resource='db.properties' /><settings><!--日志的实现是LOG4J--><setting name='logImpl' value='LOG4J' /></settings> <!--声明别名--><typeAliases><package name='com.ren.domain' /></typeAliases> <!--mybatis的插件配置--><plugins><!-- com.github.pagehelper为PageHelper类所在包名 --><plugin interceptor='com.github.pagehelper.PageInterceptor'><!-- 表示使用mysql的分页方法 --><property name='helperDialect' value='mysql' /><!-- 表示当页码长度为0 的时候,就不进行分页查询 --><property name='pageSizeZero' value='true'/></plugin></plugins><!-- 配置连接数据库四要素 --><environments default='dev'><environment id='dev'><transactionManager type='JDBC' /><dataSource type='POOLED'><property name='driver' value='${driverClassName}' /><property name='url' value='${url}' /><property name='username' value='${username}' /><property name='password' value='${password}' /></dataSource></environment></environments> <mappers><mapper resource='com/ren/mapper/StudentMapper.xml' /></mappers></configuration>编写pojo实体类和mapper接口和mapper映射文件
Student实体类
package pojo;public class Student { private Integer id; private String name; private String email; private Integer age; public Student() { } public Student(Integer id, String name, String email, Integer age) {this.id = id;this.name = name;this.email = email;this.age = age; } public Integer getId() {return id; } public void setId(Integer id) {this.id = id; } public String getName() {return name; } public void setName(String name) {this.name = name; } public String getEmail() {return email; } public void setEmail(String email) {this.email = email; } public Integer getAge() {return age; } public void setAge(Integer age) {this.age = age; } @Override public String toString() {return 'Student{' +'id=' + id +', name=’' + name + ’’’ +', email=’' + email + ’’’ +', age=' + age +’}’; }}
mapper接口
package mapper;import pojo.Student;import java.util.List;public interface StudentMapper { //查询所有学生 List<Student> selectAll();}
Mapper映射文件
<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapperPUBLIC '-//mybatis.org//DTD Mapper 3.0//EN''http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='mapper.StudentMapper'> <select resultType='pojo.Student'>select * from student order by id </select></mapper>
在sqlMapConfig文件中指定Mapper文件位置
<!-- mapper映射文件位置--><mappers><package name='mapper'/></mappers>
编写MyBatisUtils
package utils;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;/** * 工具类:创建SqlSession对象 */public class MyBatisUtil { private static SqlSessionFactory factory = null; static {String config='sqlMapConfig.xml';try { InputStream inputStream =Resources.getResourceAsStream(config); factory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) { e.printStackTrace();} } //创建方法,获取SqlSession对象 public static SqlSession getSqlSession(){SqlSession session = null;if( factory != null ){ session =factory.openSession();// openSession(true);}return session; }}创建测试类
import com.github.pagehelper.PageHelper;import mapper.StudentMapper;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import pojo.Student;import utils.MyBatisUtil;import java.util.List;public class pageHelper_Test { @Test public void PageHelperTest(){//获取sqlSessionSqlSession sqlSession = MyBatisUtil.getSqlSession();//获取mapper代理StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//调用PageHelperPageHelper.startPage(2,2);//查询所有学生List<Student> studentList = mapper.selectAll();//遍历结果List集合studentList.forEach(student -> System.out.println(student));//关闭sqlSession对象sqlSession.close(); }}
查看结果
可以看到sql语句,在mapper文件中并没有写limit,pageHelper已经帮我们加上了limit关键字完成了查询.OK了
总结到此这篇关于Mybatis利用分页插件PageHelper快速实现分页查询的文章就介绍到这了,更多相关Mybatis分页插件PageHelper内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: