分頁插件pagerHelper
該插件支持Oracle、mySql临庇、MariaDB笛坦、SQlite、Sql server苔巨、hsqldb等6種數(shù)據(jù)庫版扩。
<pagehelper.version>3.4.2</pagehelpder.version>
使用步驟
1、把pageHelper maven工程導入到workspace中侄泽。run as》install
2礁芦、在service工程中的mybatis》sqlmapConfig.xml中添加配置,添加攔截,攔截pagehelper工程的com.github.pagehelper.PageHelper類柿扣。
<configuration>
<!-- 配置分頁插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 配置數(shù)據(jù)庫的方言 -->
<!-- 設置數(shù)據(jù)庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
測試
1肖方、在mybatis的配置文件中配置分頁插件
2、在執(zhí)行查詢之前配置分頁條件未状,使用PageHelper的靜態(tài)方法
PageHelper.startPage(1, 10);
3俯画、執(zhí)行查詢
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
//創(chuàng)建Example對象
TbItemExample example = new TbItemExample();
//Criteria criteria = example.createCriteria();//查詢條件
List<TbItem> list = itemMapper.selectByExample(example);
4、取分頁信息司草,使用PageInfo對象取
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
System.out.println("總記錄數(shù):" + pageInfo.getTotal());
System.out.println("總記頁數(shù):" + pageInfo.getPages());
System.out.println("返回的記錄數(shù):" + list.size());