1、文档及jar下载地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md
2、步骤:
①准备好需要的bean、mapper、mapper映射、mybatis配置、数据库连接
②创建SqlSessionFactory和SqlSession,并且获取SqlSession的mapper对象
③使用PageHelper.startPage("(int)当前页码", “(int)一页显示多少条记录”);
④调用mapper的方法获取数据
⑤创建PageInfo对象,并且把上一步获取的List数据放到该对象中,如果需要定义连续显示的页码数,可以在创建该对象时候,把想要连续显示的页码数写到第二个入参中
⑥调用PageInfo对象的方法来获取想要的页码信息等数据
3、注意
①要加入jsqlparser.jar和pagehelper.jar
②要在mybatis配置文件中配置 PageInterceptor的plugin
③要记得在获取数据库数据之前调用PageHelper.startPage(),来设定查询规则
④在输出之前要创建PageInfo对象,用该对象来输出数据
⑤创建PageInfo对象时,第二个入参可以控制连续显示页码的个数
4、mybatis.xml
<?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="dbConfig.properties"></properties>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<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="InfoMapper.xml" />
</mappers>
</configuration>
5、InfoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.ssmtest.InfoMapper">
<select id="getData" resultType="com.demo.ssmtest.Info">
select * from info
</select>
</mapper>
6、dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=
7、Info.java
package com.demo.ssmtest;
public class Info {
Integer id;
String name;
String password;
String description;
public Info() {
super();
}
public Info(Integer id, String name, String password, String description) {
super();
this.id = id;
this.name = name;
this.password = password;
this.description = description;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", password=" + password + ", description=" + description + "]";
}
}
8、InfoMapper.java
package com.demo.ssmtest;
import java.util.List;
public interface InfoMapper {
public List<Info> getData();
}
9、TestMain.java
package com.demo.ssmtest;
import java.io.InputStream;
import java.util.List;
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 com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
public class TestMain {
public static void main(String[] args) throws Exception {
test();
}
/*
1、文档及jar下载地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md
2、步骤:
①准备好需要的bean、mapper、mapper映射、mybatis配置、数据库连接
②创建SqlSessionFactory和SqlSession,并且获取SqlSession的mapper对象
③使用PageHelper.startPage("(int)当前页码", "(int)一页显示多少条记录");
④调用mapper的方法获取数据
⑤创建PageInfo对象,并且把上一步获取的List数据放到该对象中,如果需要定义连续显示的页码数,可以在创建该对象时候,把想要连续显示的页码数写到第二个入参中
⑥调用PageInfo对象的方法来获取想要的页码信息等数据
3、注意
①要加入jsqlparser.jar和pagehelper.jar
②要在mybatis配置文件中配置 PageInterceptor的plugin
③要记得在获取数据库数据之前调用PageHelper.startPage(),来设定查询规则
④在输出之前要创建PageInfo对象,用该对象来输出数据
⑤创建PageInfo对象时,第二个入参可以控制连续显示页码的个数
*/
public static void test() throws Exception {
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
InfoMapper queryMapper = openSession.getMapper(InfoMapper.class);
PageHelper.startPage(1, 5);
List<Info> list = queryMapper.getData();
//可以在new PageInfo()的第二个入参里面定义页码连续显示多少页
PageInfo page = new PageInfo(list,5);
for(Info info:list){
System.out.println(info.toString());
}
System.out.println("每页有多少条记录:"+page.getPageSize());
System.out.println("当前页码:"+page.getPageNum());
System.out.println("总记录条数:"+page.getTotal());
System.out.println("总页码:"+page.getPages());
System.out.println("判断是否第一页:"+page.isIsFirstPage());
System.out.println("判断是否最后一页:"+page.isIsLastPage());
System.out.println("判断有没有上一页:"+page.isHasPreviousPage());
System.out.println("判断有没有下一页:"+page.isHasNextPage());
System.out.println("连续显示页码:");
int[] nums = page.getNavigatepageNums();
for(int i=0; i<nums.length; i++){
System.out.println(nums[i]);
}
} finally {
openSession.close();
}
}
}
10、项目目录
11、demo
https://download.csdn.net/download/qq_22778717/10719625