第一步导包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
第二步,配置配置文件
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
user=root
upwd=root
Mybatis的配置文件
<?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="db.properties"/>
<settings>
<!--配置可以懒加载-->
<setting name="lazyLoadingEnabled" value="true" />
<!--二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--配置别名-->
<typeAliases>
<!--//指定类配置-->
<!--<typeAlias type="com.hw.entity.Student" alias="student"></typeAlias>-->
<!--指定包配置 别名就是类名 第一个小写-->
<package name="com.hw.entity"></package>
</typeAliases>
<!--配置分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- config params as the following -->
<!--<!–分页参数合理化 –>-->
<property name="param1" value="valueerr1"/>
</plugin>
</plugins>
<!--配置环境-->
<environments default="development">
<environment id="development">
<!--jdbc事务-->
<transactionManager type="JDBC"/>
<!--连接池-->
<dataSource type="POOLED">
<!--驱动-->
<property name="driver" value="${driver}"/>
<!--数据库链接-->
<property name="url" value="${url}"/>
<!--用户名-->
<property name="username" value="${user}"/>
<!--密码-->
<property name="password" value="${upwd}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--映射文件-->
<!--<mapper resource="com/hw/entity/Student.xml"/>-->
<!--mapper映射文件-->
<!--<mapper resource="com/hw/mapper/StudentMapper.xml"></mapper>-->
<!--写类名,要有映射文件与之对应,如果没有那么StudentMapper要声明注解-->
<mapper class="com.hw.mapper.StudentMapper"></mapper>
<!--使用包名映射-->
<!--<package name="com.hw.mapper"></package>-->
</mappers>
</configuration>
实体类
package com.hw.entity;
import java.io.Serializable;
/**
* @program: Maven
* @description:
* @author: hw
* @create: 2019-01-01 00:42
**/
public class Student implements Serializable {
private Integer sid;
private String sname;
private Integer tid;
public Student() {
super();
}
public Student(String sname, Integer tid) {
this.sname = sname;
this.tid = tid;
}
public Student(Integer sid, String sname, Integer tid) {
this.sid = sid;
this.sname = sname;
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", tid=" + tid +
'}';
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
}
Mapper接口
package com.hw.mapper;
import com.hw.entity.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
/**
* @program: Maven
* @description:
* @author: hw
* @create: 2019-01-01 02:45
**/
public interface StudentMapper {
@Select("select * from student where sname like '%${sname}%' and tid=#{tid}")
public List<Student> findStudentByMap(Map<String, Object> map);
}
测试类
package com.hw.test;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.entity.Student;
import com.hw.mapper.StudentMapper;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @program: Maven
* @description:
* @author: hw
* @create: 2019-01-03 09:14
**/
public class demo1 {
SqlSession sqlSession;
SqlSessionFactory build;
@Before
public void before() {
try {
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream("Mybatis.xml");
//通过sqlsessionFactoryBuilder创建sqlsessionFactory会话工厂
build = new SqlSessionFactoryBuilder().build(inputStream);
//通过sqlsessionFactory创建sqlsession
sqlSession = build.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void after() {
//关闭sqlsession
sqlSession.commit();
sqlSession.close();
}
@Test
public void test1() throws IOException, InterruptedException {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//查询返回值为 lsit<map>
Map<String, Object> map = new HashMap<>();
map.put("tid", 1);
map.put("sname", "1");
//分页开始数和分页的条数
Page<Object> objects = PageHelper.startPage(2, 2);
List<Student> studentMap = mapper.findStudentByMap(map);
System.err.println(studentMap);
PageInfo pageInfo=new PageInfo(studentMap);
String str = JSON.toJSONString(pageInfo);
System.err.println(str);
System.err.println(pageInfo.getList()+"_____________");
}
}
输出的信息
然后看下官方文档对于这些参数的解释:
下面几个参数都是针对默认 dialect 情况下的参数。使用自定义 dialect 实现时,下面的参数没有任何作用。
-
helperDialect
:分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect
属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:oracle
,mysql
,mariadb
,sqlite
,hsqldb
,postgresql
,db2
,sqlserver
,informix
,h2
,sqlserver2012
,derby
特别注意:使用 SqlServer2012 数据库时,需要手动指定为sqlserver2012
,否则会使用 SqlServer2005 的方式进行分页。
你也可以实现AbstractHelperDialect
,然后配置该属性为实现类的全限定名称即可使用自定义的实现方法。 -
offsetAsPageNum
:默认值为false
,该参数对使用RowBounds
作为分页参数时有效。 当该参数设置为true
时,会将RowBounds
中的offset
参数当成pageNum
使用,可以用页码和页面大小两个参数进行分页。 -
rowBoundsWithCount
:默认值为false
,该参数对使用RowBounds
作为分页参数时有效。 当该参数设置为true
时,使用RowBounds
分页会进行 count 查询。 -
pageSizeZero
:默认值为false
,当该参数设置为true
时,如果pageSize=0
或者RowBounds.limit = 0
就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是Page
类型)。 -
reasonable
:分页合理化参数,默认值为false
。当该参数设置为true
时,pageNum<=0
时会查询第一页,pageNum>pages
(超过总数时),会查询最后一页。默认false
时,直接根据参数进行查询。 -
params
:为了支持startPage(Object params)
方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置pageNum,pageSize,count,pageSizeZero,reasonable
,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
。 -
supportMethodsArguments
:支持通过 Mapper 接口参数来传递分页参数,默认值false
,分页插件会从查询方法的参数值中,自动根据上面params
配置的字段中取值,查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的com.github.pagehelper.test.basic
包下的ArgumentsMapTest
和ArgumentsObjTest
。 -
autoRuntimeDialect
:默认值为false
。设置为true
时,允许在运行时根据多数据源自动识别对应方言的分页 (不支持自动选择sqlserver2012
,只能使用sqlserver
),用法和注意事项参考下面的场景五。 -
closeConn
:默认值为true
。当使用运行时动态数据源或没有设置helperDialect
属性自动获取数据库类型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认true
关闭,设置为false
后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。
重要提示:
当 offsetAsPageNum=false
的时候,由于 PageNum
问题,RowBounds
查询的时候 reasonable
会强制为 false
。使用 PageHelper.startPage
方法不受影响。
官网教程:https://pagehelper.github.io/docs/howtouse/