Mybatis分頁(yè)插件使用方法詳解
本文實(shí)例為大家分享了Mybatis分頁(yè)插件使用的具體代碼,供大家參考,具體內(nèi)容如下
1.分頁(yè)插件簡(jiǎn)介
都說(shuō)這是史上最好用的分頁(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)于沒(méi)有執(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ù)來(lái)配置參數(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è)接口來(lái)設(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)換這用來(lái)復(fù)制屬性值(可選)
數(shù)據(jù)模型entity 只對(duì)應(yīng)數(shù)據(jù)庫(kù)表中的字段, 出參與入?yún)?都是數(shù)據(jù)傳輸對(duì)象 dto , 從數(shù)據(jù)庫(kù)中查出來(lái)的是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 搭建與使用入門(mén)(小結(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通過(guò)插件將數(shù)據(jù)庫(kù)表生成Entiry,Mapper.xml,Mapper.class的方式
相關(guān)文章
完美解決java讀取大文件內(nèi)存溢出的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決java讀取大文件內(nèi)存溢出的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
使用Thrift實(shí)現(xiàn)跨語(yǔ)言RPC的調(diào)用
Thrift最大的優(yōu)勢(shì)就是可以實(shí)現(xiàn)跨語(yǔ)言RPC調(diào)用,尤其在一些大廠,微服務(wù)各模塊之間使用不同的語(yǔ)言是很常見(jiàn)的,本文就將使用java作為服務(wù)端,用python作為客戶端,實(shí)現(xiàn)不同語(yǔ)言之間的RPC調(diào)用,需要的可以參考下2023-10-10
JDK版本管理工具jEnv解決不同jdk版本項(xiàng)目
本文主要介紹了JDK版本管理工具jEnv解決不同jdk版本項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java將字符串String轉(zhuǎn)換為整型Int的兩種方式
這篇文章主要介紹了Java如何將字符串String轉(zhuǎn)換為整型Int,在 Java 中要將 String 類型轉(zhuǎn)化為 int 類型時(shí),需要使用 Integer 類中的 parseInt() 方法或者 valueOf() 方法進(jìn)行轉(zhuǎn)換,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2023-04-04
在Spring Data JPA中引入Querydsl的實(shí)現(xiàn)方式
這篇文章主要介紹了在Spring Data JPA中引入Querydsl的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
java Collection 之Set使用說(shuō)明
本篇文章小編為大家介紹,java Collection 之Set使用說(shuō)明。需要的朋友參考下2013-04-04
淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated)
這篇文章主要介紹了淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

