内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

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

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

成都創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元云夢(mèng)做網(wǎng)站,已為上家服務(wù),為云夢(mèng)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站題目:Mybatis分頁(yè)插件使用方法詳解
網(wǎng)站地址:http://www.rwnh.cn/article24/jiecce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、云服務(wù)器、建站公司、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
米脂县| 邵武市| 衡阳县| 隆子县| 濉溪县| 富锦市| 重庆市| 东乌珠穆沁旗| 汤原县| 班戈县| 泽州县| 株洲县| 惠东县| 姚安县| 若尔盖县| 信阳市| 高碑店市| 大石桥市| 德格县| 右玉县| 清苑县| 商洛市| 芜湖市| 蚌埠市| 鄂尔多斯市| 舟曲县| 枣庄市| 兴仁县| 海阳市| 临澧县| 黄梅县| 文昌市| 波密县| 阿克| 临安市| 邳州市| 昌图县| 修水县| 巨鹿县| 益阳市| 西乌|