首先引入相关的jar包:
然后是在Mybatis全局配置文件中配置:
<plugins> plugin interceptor="com.github.pageHelper.PageInterceptor"></plugin> </>
就可以使用了。
EmployeeMapper.java中
public List<Employee> getAllEmp();
EmployeeMapper.xml中
select ID="getAllEmp" resultType="com.gong.mybatis.bean.Employee"> select * from tbl_employee select>
进行测试:首先是不带分页的测试
package com.gong.mybatis.test;import java.io.IOException; java.io.inputStream; java.util.ArrayList; java.util.Arrays; java.util.HashMap; java.util.List; java.util.Map; org.apache.ibatis.io.Resources; org.apache.ibatis.session.sqlSession; org.apache.ibatis.session.sqlSessionFactory; org.apache.ibatis.session.sqlSessionFactoryBuilder; org.junit.Test; com.github.pageHelper.Page; com.github.pageHelper.pageHelper; com.github.pageHelper.PageInfo; com.gong.mybatis.bean.Department; com.gong.mybatis.bean.Employee; com.gong.mybatis.dao.EmployeeMapper; com.gong.mybatis.mapper.EmployeeMapperDynamicsql;public class TestMybatis5 { public sqlSessionFactory getsqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; inputStream is = Resources.getResourceAsstream(resource); return new sqlSessionFactoryBuilder().build(is); } @Test voID test() IOException { sqlSessionFactory sqlSessionFactory = getsqlSessionFactory(); sqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.); List<Employee> emps = mapper.getAllEmp(); for(Employee emp:emps) { System.out.println(emp); } } finally { // Todo: handle finally clause openSession.close(); } } }
输出:
DEBUG 01-23 14:29:17,295 ==> Preparing: select * from tbl_employee (BaseJdbcLogger.java:145) DEBUG 01-23 14:29:17,334 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:29:17,412 == Total: 7 (BaseJdbcLogger.java:145) Employee [ID=1,lastname=dema,1)">genderemail=dema@qq.com,1)">dept=null]Employee [ID=2,1)">=jack,1)">=675544321@qq.com,1)">=3,1)">=小红,1)">=0,1)">=xiaohong@qq.com,1)">=4,1)">=小明,1)">=xiaoming@qq.com,1)">=6,1)">=timo,1)">=timo@qq.com,1)">=7,1)">=manwang,1)">=manwang@qq.com,1)">=8,1)">=null]
我们共有7条数据。
第一种分页方式:
); 第一个参数为当前页码,第二个参数是每页的记录数 Page<Object> page = pageHelper.startPage(1,5); List<Employee> emps =(Employee emp:emps) { System.out.println(emp); } System.out.println("当前页码:"+page.getPageNum()); System.out.println("总记录数:"+page.getTotal()); System.out.println("每页几条记录:"+page.getPageSize()); System.out.println("总页码数:"+page.getPages()); } openSession.close(); } }}
输出:
DEBUG 01-23 14:32:50,024 Cache Hit Ratio [sql_CACHE]: 0.0 (LoggingCache.java:62) DEBUG 01-23 14:32:50,071 ==> Preparing: SELECT count(0) FROM tbl_employee (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,128 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,197 1 (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,199 ==> Preparing: select * from tbl_employee liMIT 5 (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,1)">==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,212 <== Total: 5 (BaseJdbcLogger.java:145) Employee [ID当前页码:1总记录数:7每页几条记录:5总页码数:2
第二种分页方式:
在上一种设置起始页和每页记录的情况下继续增加:
第一个参数为当前页码,第二个参数是每页的记录数 Page<Object> page = pageHelper.startPage(1,2第一个参数为传入的数据,第二个参数为连续显示多少页 PageInfo<Employee> pageInfo = new PageInfo<>(emps,3(Employee emp:emps) { System.out.println(emp); } System.out.println("当前页码:"+pageInfo.getPageNum()); System.out.println("总记录数:"+pageInfo.getTotal()); System.out.println("每页几条记录:"+pageInfo.getPageSize()); System.out.println("总页码数:"+pageInfo.getPages()); System.out.println("是否第一页:"+pageInfo.isIsFirstPage()); System.out.println("是否最后一页:"+pageInfo.isIsLastPage()); System.out.println("连续显示的页码:"int[] navigatepageNums = pageInfo.getNavigatepageNums(); for (int i = 0; i < navigatepageNums.length; i++) { System.out.println(navigatepageNums[i]); } } DEBUG 01-23 14:39:20,125 Cache Hit Ratio [sql_CACHE]: 0.0 (LoggingCache.java:62) DEBUG 01-23 14:39:20,172 ==> Preparing: SELECT count(0) FROM tbl_employee (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,247 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,278 1 (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,281 Preparing: select * from tbl_employee liMIT 2 (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,283 Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,292 <Total: 2 (BaseJdbcLogger.java:145) Employee [ID当前页码:1总记录数:7每页几条记录:2总页码数:4是否第一页:true是否最后一页:false连续显示的页码:123总结
以上是内存溢出为你收集整理的mybatis扩展之使用PageHelper插件进行分页(是真好用)全部内容,希望文章能够帮你解决mybatis扩展之使用PageHelper插件进行分页(是真好用)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)