分页插件PageHelper配置步骤(mybatis)
原理:
mybatis執(zhí)行sql步驟:?
通過(guò)sqlsessionFactory??sqlsession?Exector ?(執(zhí)行器對(duì)象)mappedstatement(sql語(yǔ)句封裝)
在執(zhí)行mappedstatement前在sql語(yǔ)句上加上limit即可實(shí)現(xiàn)分頁(yè)
步驟:
一、引入pageHelper的jar包
二、在mybatis的xml文件中配置分頁(yè)插件
<!-- plugins在配置文件中的位置必須符合要求,否則會(huì)報(bào)錯(cuò),順序如下:properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins><!-- com.github.pagehelper為PageHelper類(lèi)所在包名 --><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/><!-- 該參數(shù)默認(rèn)為false --><!-- 設(shè)置為true時(shí),會(huì)將RowBounds第一個(gè)參數(shù)offset當(dāng)成pageNum頁(yè)碼使用 --><!-- 和startPage中的pageNum效果一樣--><property name="offsetAsPageNum" value="true"/><!-- 該參數(shù)默認(rèn)為false --><!-- 設(shè)置為true時(shí),使用RowBounds分頁(yè)會(huì)進(jìn)行count查詢 --><property name="rowBoundsWithCount" value="true"/><!-- 設(shè)置為true時(shí),如果pageSize=0或者RowBounds.limit = 0就會(huì)查詢出全部的結(jié)果 --><!-- (相當(dāng)于沒(méi)有執(zhí)行分頁(yè)查詢,但是返回結(jié)果仍然是Page類(lèi)型)--><property name="pageSizeZero" value="true"/><!-- 3.3.0版本可用 - 分頁(yè)參數(shù)合理化,默認(rèn)false禁用 --><!-- 啟用合理化時(shí),如果pageNum<1會(huì)查詢第一頁(yè),如果pageNum>pages會(huì)查詢最后一頁(yè) --><!-- 禁用合理化時(shí),如果pageNum<1或pageNum>pages會(huì)返回空數(shù)據(jù) --><property name="reasonable" value="true"/></plugin> </plugins>這里的com.github.pagehelper.PageHelper使用完整的類(lèi)路徑。
其他五個(gè)參數(shù)說(shuō)明:
也可以在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>?三、配置完成后再調(diào)用類(lèi)中配置分頁(yè)屬性:
? ?步驟:?只需將查詢的條件放入PageInfo中,設(shè)置分頁(yè)屬性即可
//查詢所需數(shù)據(jù)條件List<TbItem> list = ibItemMapper.selectByExample(new TbItemExample());//設(shè)置分頁(yè)信息(第幾頁(yè),每頁(yè)數(shù)量)PageHelper.startPage(pageNum, pageSize);//取記錄總條數(shù)PageInfo<TbItem> pageInfo = new PageInfo<>(list); long sum = pageInfo.getTotal();?? ? ? ?
???原理:?執(zhí)行自定義( 例如 :ibItemMapper.selectByExample(new TbItemExample())) 查詢語(yǔ)句時(shí),
? ? ? ? ? ? ? ?會(huì)在mappedstatement執(zhí)行前將分頁(yè)的sql語(yǔ)句追加到查詢語(yǔ)句后。
?
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();?
注意事項(xiàng):
-
PageHelper.startPage方法后的一個(gè)個(gè)查詢方法才會(huì)被分頁(yè)。
- 不支持帶有for update的語(yǔ)句分頁(yè)。
- 不支持關(guān)聯(lián)結(jié)果查詢。
總結(jié)
以上是生活随笔為你收集整理的分页插件PageHelper配置步骤(mybatis)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LIVE555再学习 -- FFmpeg
- 下一篇: RTSP再学习 -- RTSP协议分析(