- Maven中添加相应依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
- 配置文件注册插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
- JAVA程序
public void test() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
PageHelper.startPage(2, 3);
List<Employee> list = mapper.selectAll();
PageInfo pageInfo = new PageInfo(list, 4);
for (Employee employee : list) {
System.out.println(employee.toString());
}
for (int navigatepageNum : pageInfo.getNavigatepageNums()) {
System.out.println(navigatepageNum);
}
}finally {
sqlSession.close();
}
}
输出
Employee(id=4, name=赵六, deptId=2)
Employee(id=5, name=威爷, deptId=3)
Employee(id=12, name=Ben, deptId=2)
1
2
3
|