生活随笔
收集整理的這篇文章主要介紹了
PageHelper分页插件使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
分頁插件PageHelper:
MyBatis沒有分頁功能,需要手動編寫LIMIT語句,可以使用第三方的插件來對功能進行擴展,分頁助手PageHelper是將分頁的復(fù)雜操作進行封裝,使用簡單的方式即可獲得分頁的相關(guān)數(shù)據(jù)
PageInfo:
PageInfo常用方法:
方法說明
| getTotal() | 獲取總條數(shù) |
| getPages() | 獲取總頁數(shù) |
| getPageNum() | 獲取當(dāng)前頁 |
| getPageSize() | 獲取每頁顯示條數(shù) |
| getPrePage() | 獲取上一頁 |
| getNextPage() | 獲取下一頁 |
| isIsFirstPage() | 獲取是否是第一頁 |
| isIsLastPage() | 獲取是否是最后一頁 |
演示:
導(dǎo)入與PageHelper的jar包在mybatis核心配置文件中配置PageHelper插件
<plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"><property name="helperDialect" value="mysql"/><property name="pageSizeZero" value="true"/></plugin></plugins>
測試分頁數(shù)據(jù)獲取
@Testpublic void selectPaging() throws Exception{InputStream is
= Resources.getResourceAsStream("MyBatisConfig.xml");SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(is
);SqlSession sqlSession
= sqlSessionFactory
.openSession(true);StudentMapper mapper
= sqlSession
.getMapper(StudentMapper.class);PageHelper.startPage(1,3);List<Student> list
= mapper
.selectAll();for (Student student
: list
) {System.out
.println(student
);}PageInfo<Student> info
= new PageInfo<>(list
);System.out
.println("總條數(shù):" + info
.getTotal());System.out
.println("總頁數(shù):" + info
.getPages());System.out
.println("當(dāng)前頁:" + info
.getPageNum());System.out
.println("每頁顯示條數(shù):" + info
.getPageSize());System.out
.println("上一頁:" + info
.getPrePage());System.out
.println("下一頁:" + info
.getNextPage());System.out
.println("是否是第一頁:" + info
.isIsFirstPage());System.out
.println("是否是最后一頁:" + info
.isIsLastPage());sqlSession
.close();is
.close();}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的PageHelper分页插件使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。