Mybatis分頁(yè)插件使用方法詳解
本文實(shí)例為大家分享了Mybatis分頁(yè)插件使用的具體代碼,供大家參考,具體內(nèi)容如下
1.分頁(yè)插件簡(jiǎn)介
都說這是史上最好用的分頁(yè)插件,支持多種數(shù)據(jù)庫(kù)以多種方式分頁(yè)。
2.分頁(yè)插件的使用
2.1導(dǎo)入maven依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>最新版本</version> </dependency>
2.2 添加配置
1.在mybatis的config配置文件中添加攔截器 <plugin>
<!-- plugins在配置文件中的位置必須符合要求,否則會(huì)報(bào)錯(cuò),順序如下: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <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)于沒有執(zhí)行分頁(yè)查詢,但是返回結(jié)果仍然是Page類型)--> <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="false"/> <!-- 3.5.0版本可用 - 為了支持startPage(Object params)方法 --> <!-- 增加了一個(gè)`params`參數(shù)來配置參數(shù)映射,用于從Map或ServletRequest中取值 --> <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默認(rèn)值 --> <!-- 不理解該含義的前提下,不要隨便復(fù)制該配置 --> <property name="params" value="pageNum=start;pageSize=limit;"/> </plugin> </plugins>
2.或者在spring配置中添加
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注意其他配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置參數(shù),一行配置一個(gè) --> <value> params=value1 </value> </property> </bean> </array> </property> </bean>
這兩種方式不能同時(shí)用
3.在代碼中的使用
3.1設(shè)置一個(gè)基礎(chǔ)的請(qǐng)求類
public class BaseRequest implements Serializable { private static final long serialVersionUID = 1193444819529643410L; private Integer pageNum;//頁(yè)數(shù) private Integer pageSize;//每頁(yè)行數(shù) private Boolean count;//是否查詢總條數(shù) public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Boolean getCount() { return count; } public void setCount(Boolean count) { this.count = count; } @Override public String toString() { return "BaseRequest{" + "pageNum=" + pageNum + ", pageSize=" + pageSize + '}'; } }
3.2 設(shè)置一個(gè)基礎(chǔ)的PageService 接口
讓每個(gè)service 去實(shí)現(xiàn)這個(gè)接口來設(shè)置分頁(yè)的初始值
public interface PageService { default void setDefaultPageInfo(BaseRequest baseRequest) { if (null != baseRequest) { baseRequest.setPageNum(null == baseRequest.getPageNum() ? Constants.PAGE_NUM : baseRequest.getPageNum()); baseRequest .setPageSize(null == baseRequest.getPageSize() ? Constants.PAGE_SIZE : baseRequest.getPageSize()); baseRequest.setCount(null == baseRequest.getCount() ? Boolean.TRUE : baseRequest.getCount()); } else { baseRequest = new BaseRequest(); baseRequest.setPageNum(Constants.PAGE_NUM); baseRequest.setPageSize(Constants.PAGE_SIZE); baseRequest.setCount(Boolean.TRUE); } PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize(),baseRequest.getCount()); } }
3.3 如果做了數(shù)據(jù)轉(zhuǎn)換這用來復(fù)制屬性值(可選)
數(shù)據(jù)模型entity 只對(duì)應(yīng)數(shù)據(jù)庫(kù)表中的字段, 出參與入?yún)?都是數(shù)據(jù)傳輸對(duì)象 dto , 從數(shù)據(jù)庫(kù)中查出來的是entity而 接口返回的是dto 所要BeanUtils.copyProperties復(fù)制屬性,和pageutils.copyProperties 復(fù)制分頁(yè)屬性
public class PageUtils { public static void copyProperties(PageInfo<?> source, PageInfo<?> des) { des.setEndRow(source.getEndRow()); des.setFirstPage(source.getFirstPage()); des.setHasNextPage(source.isHasNextPage()); des.setHasPreviousPage(source.isHasPreviousPage()); des.setIsFirstPage(source.isIsFirstPage()); des.setIsLastPage(source.isIsLastPage()); des.setNavigatepageNums(source.getNavigatepageNums()); des.setNavigatePages(source.getNavigatePages()); des.setNextPage(source.getNextPage()); des.setOrderBy(source.getOrderBy()); des.setPageNum(source.getPageNum()); des.setPages(source.getPages()); des.setPageSize(source.getPageSize()); des.setPrePage(source.getPrePage()); des.setSize(source.getSize()); des.setStartRow(source.getStartRow()); des.setTotal(source.getTotal()); } }
4.使用示例
在OrderService實(shí)現(xiàn)類中
import com.github.pagehelper.PageInfo; import com.javxuan.common.util.PageUtils; import com.javxuan.order.entity.Order; import com.javxuan.order.response.OrderDto; import com.javxuan.order.service.PageService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; public class OrderServcieImpl implements IOrderServcie, PageService { @Autowired IOrderMapper orderMapper; @GetMapping("/order") public PageInfo<OrderDto> list(OrderRequest orderRequest){ //設(shè)置默認(rèn)分頁(yè)信息 PageService的方法 setDefaultPageInfo(orderRequest); //查出order列表 List<Order> orderList = orderMapper.selectList(); //將entity的列表分頁(yè) PageInfo<Order> orderPageInfo = new PageInfo<>(orderList); //連續(xù)顯示5頁(yè)與上面的二選一 //PageInfo<Order> orderPageInfo = new PageInfo<>(orderList,5); //定義一個(gè)數(shù)據(jù)傳輸對(duì)象dtoList List<OrderDto> dtoList = new ArrayList<>(); if(null==orderList || orderList.size<=0){ return null; } //給dtoList 加值 for(Order order:orderList){ OrderDto dto = new OrderDto(); //將entity 的屬性值 復(fù)制給dto上 BeanUtils.copyProperties(order, dto); dtoList.add(dto); } //給dto 分頁(yè) PageInfo<OrderDto> dtoPageInfo = new PageInfo<>(dtoList); //連續(xù)顯示5頁(yè) 與上面的二選一 //PageInfo<Order> orderPageInfo = new PageInfo<>(orderList,5); //將entity的分頁(yè)信息復(fù)制給dtoPageInfo上 PageUtils.copyProperties(orderPageInfo, dtoPageInfo); return dtoPageInfo; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- MybatisPlus 多租戶架構(gòu)(Multi-tenancy)實(shí)現(xiàn)詳解
- Mybatis-Plus 多表聯(lián)查分頁(yè)的實(shí)現(xiàn)代碼
- Mybatis-Plus 搭建與使用入門(小結(jié))
- 基于Mybatis plus 自動(dòng)代碼生成器的實(shí)現(xiàn)代碼
- spring boot整合mybatis+mybatis-plus的示例代碼
- Mybatis批量更新三種方式的實(shí)現(xiàn)
- mybatis關(guān)系映射之一對(duì)多和多對(duì)一
- MyBatis插入數(shù)據(jù)返回主鍵的介紹
- Servlet+MyBatis項(xiàng)目轉(zhuǎn)Spring Cloud微服務(wù),多數(shù)據(jù)源配置修改建議
- MyBatis-Plus通過插件將數(shù)據(jù)庫(kù)表生成Entiry,Mapper.xml,Mapper.class的方式
相關(guān)文章
Java ConcurrentModificationException異常解決案例詳解
這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09MyBatis游標(biāo)Cursor在Oracle數(shù)據(jù)庫(kù)上的測(cè)試方式
這篇文章主要介紹了MyBatis游標(biāo)Cursor在Oracle數(shù)據(jù)庫(kù)上的測(cè)試方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java超詳細(xì)分析講解final關(guān)鍵字的用法
關(guān)于final關(guān)鍵字,它也是我們一個(gè)經(jīng)常用的關(guān)鍵字,可以修飾在類上、或者修飾在變量、方法上,以此看來定義它的一些不可變性!像我們經(jīng)常使用的String類中,它便是final來修飾的類,并且它的字符數(shù)組也是被final所修飾的。但是一些final的一些細(xì)節(jié)你真的了解過嗎2022-06-06SpringBoot整合OpenCV的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot整合OpenCV的實(shí)現(xiàn)示例。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12springBoot service層事務(wù)控制的操作
這篇文章主要介紹了springBoot service層事務(wù)控制的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02