分頁外掛PageHelper
阿新 • • 發佈:2018-11-25
一、PageHelper說明
如果你也在用Mybatis,建議嘗試該分頁外掛,這個一定是最方便使用的分頁外掛。
該外掛目前支援Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫分頁。
二、使用方法
第一步:把PageHelper依賴的jar包新增到工程中。官方提供的程式碼對逆向工程支援的不好,使用參考資料中的pagehelper-fix。
第二步:在Mybatis配置xml中配置攔截器外掛:
<plugins> <!-- com.github.pagehelper為PageHelper類所在包名--> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫--> <property name="dialect" value="mysql"/> </plugin> </plugins>
第三步:測試分頁
@Test public void testPageHelper() throwsException { //初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); //獲得Mapper的代理物件 TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class); //設定分頁資訊 PageHelper.startPage(1, 30); //執行查詢 TbItemExample example = new TbItemExample(); List<TbItem> list = itemMapper.selectByExample(example); //取分頁資訊 PageInfo<TbItem> pageInfo = new PageInfo<>(list); System.out.println(pageInfo.getTotal()); System.out.println(pageInfo.getPages()); System.out.println(pageInfo.getPageNum()); System.out.println(pageInfo.getPageSize()); }