分页插件PageHelper配置步骤(mybatis)
生活随笔
收集整理的這篇文章主要介紹了
分页插件PageHelper配置步骤(mybatis)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原理:
mybatis執行sql步驟:?
通過sqlsessionFactory??sqlsession?Exector ?(執行器對象)mappedstatement(sql語句封裝)
在執行mappedstatement前在sql語句上加上limit即可實現分頁
步驟:
一、引入pageHelper的jar包
二、在mybatis的xml文件中配置分頁插件
<!-- plugins在配置文件中的位置必須符合要求,否則會報錯,順序如下:properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins><!-- com.github.pagehelper為PageHelper類所在包名 --><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/><!-- 該參數默認為false --><!-- 設置為true時,會將RowBounds第一個參數offset當成pageNum頁碼使用 --><!-- 和startPage中的pageNum效果一樣--><property name="offsetAsPageNum" value="true"/><!-- 該參數默認為false --><!-- 設置為true時,使用RowBounds分頁會進行count查詢 --><property name="rowBoundsWithCount" value="true"/><!-- 設置為true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結果 --><!-- (相當于沒有執行分頁查詢,但是返回結果仍然是Page類型)--><property name="pageSizeZero" value="true"/><!-- 3.3.0版本可用 - 分頁參數合理化,默認false禁用 --><!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁 --><!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空數據 --><property name="reasonable" value="true"/></plugin> </plugins>這里的com.github.pagehelper.PageHelper使用完整的類路徑。
其他五個參數說明:
也可以在spring的xml文件中配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations"><array><value>classpath:mapper/*.xml</value></array></property><property name="typeAliasesPackage" value="com.isea533.ssm.model"/><property name="plugins"><array><bean class="com.github.pagehelper.PageHelper"><property name="properties"><value>dialect=hsqldbreasonable=true</value></property></bean></array></property> </bean>?三、配置完成后再調用類中配置分頁屬性:
? ?步驟:?只需將查詢的條件放入PageInfo中,設置分頁屬性即可
//查詢所需數據條件List<TbItem> list = ibItemMapper.selectByExample(new TbItemExample());//設置分頁信息(第幾頁,每頁數量)PageHelper.startPage(pageNum, pageSize);//取記錄總條數PageInfo<TbItem> pageInfo = new PageInfo<>(list); long sum = pageInfo.getTotal();?? ? ? ?
???原理:?執行自定義( 例如 :ibItemMapper.selectByExample(new TbItemExample())) 查詢語句時,
? ? ? ? ? ? ? ?會在mappedstatement執行前將分頁的sql語句追加到查詢語句后。
?
page的一些屬性: ? ?page.getPageNum();page.getPageSize();page.getStartRow();page.getEndRow(); page.getTotal();page.getPages();page.getFirstPage();page.getLastPage();page.isFirstPage();page.isLastPage();page.isHasPreviousPage();page.isHasNextPage();?
注意事項:
-
PageHelper.startPage方法后的一個個查詢方法才會被分頁。
- 不支持帶有for update的語句分頁。
- 不支持關聯結果查詢。
總結
以上是生活随笔為你收集整理的分页插件PageHelper配置步骤(mybatis)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LIVE555再学习 -- FFmpeg
- 下一篇: RTSP再学习 -- RTSP协议分析(