欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis分頁(yè)插件使用方法詳解

 更新時(shí)間:2018年12月29日 12:01:39   作者:Javxuan  
這篇文章主要為大家詳細(xì)介紹了Mybatis分頁(yè)插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Mybatis分頁(yè)插件使用的具體代碼,供大家參考,具體內(nèi)容如下

1.分頁(yè)插件簡(jiǎn)介

pagehelper源碼

都說這是史上最好用的分頁(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于Java中代碼塊的執(zhí)行順序

    關(guān)于Java中代碼塊的執(zhí)行順序

    這篇文章主要介紹了關(guān)于Java中代碼塊的執(zhí)行順序,構(gòu)造代碼塊是給所有對(duì)象進(jìn)行統(tǒng)一初始化,而構(gòu)造函數(shù)是給對(duì)應(yīng)的對(duì)象初始化,因?yàn)闃?gòu)造函數(shù)是可以多個(gè)的,運(yùn)行哪個(gè)構(gòu)造函數(shù)就會(huì)建立什么樣的對(duì)象,但無(wú)論建立哪個(gè)對(duì)象,都會(huì)先執(zhí)行相同的構(gòu)造代碼塊,需要的朋友可以參考下
    2023-08-08
  • 最新評(píng)論