Mybatis分頁插件使用方法詳解
本文實例為大家分享了Mybatis分頁插件使用的具體代碼,供大家參考,具體內(nèi)容如下
1.分頁插件簡介
都說這是史上最好用的分頁插件,支持多種數(shù)據(jù)庫以多種方式分頁。
2.分頁插件的使用
2.1導入maven依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>最新版本</version> </dependency>
2.2 添加配置
1.在mybatis的config配置文件中添加攔截器 <plugin>
<!-- plugins在配置文件中的位置必須符合要求,否則會報錯,順序如下: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> <!-- 該參數(shù)默認為false --> <!-- 設置為true時,會將RowBounds第一個參數(shù)offset當成pageNum頁碼使用 --> <!-- 和startPage中的pageNum效果一樣--> <property name="offsetAsPageNum" value="true"/> <!-- 該參數(shù)默認為false --> <!-- 設置為true時,使用RowBounds分頁會進行count查詢 --> <property name="rowBoundsWithCount" value="true"/> <!-- 設置為true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結(jié)果 --> <!-- (相當于沒有執(zhí)行分頁查詢,但是返回結(jié)果仍然是Page類型)--> <property name="pageSizeZero" value="true"/> <!-- 3.3.0版本可用 - 分頁參數(shù)合理化,默認false禁用 --> <!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁 --> <!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空數(shù)據(jù) --> <property name="reasonable" value="false"/> <!-- 3.5.0版本可用 - 為了支持startPage(Object params)方法 --> <!-- 增加了一個`params`參數(shù)來配置參數(shù)映射,用于從Map或ServletRequest中取值 --> <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默認值 --> <!-- 不理解該含義的前提下,不要隨便復制該配置 --> <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ù),一行配置一個 --> <value> params=value1 </value> </property> </bean> </array> </property> </bean>
這兩種方式不能同時用
3.在代碼中的使用
3.1設置一個基礎的請求類
public class BaseRequest implements Serializable {
private static final long serialVersionUID = 1193444819529643410L;
private Integer pageNum;//頁數(shù)
private Integer pageSize;//每頁行數(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 設置一個基礎的PageService 接口
讓每個service 去實現(xiàn)這個接口來設置分頁的初始值
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)換這用來復制屬性值(可選)
數(shù)據(jù)模型entity 只對應數(shù)據(jù)庫表中的字段, 出參與入?yún)?都是數(shù)據(jù)傳輸對象 dto , 從數(shù)據(jù)庫中查出來的是entity而 接口返回的是dto 所要BeanUtils.copyProperties復制屬性,和pageutils.copyProperties 復制分頁屬性
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實現(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){
//設置默認分頁信息 PageService的方法
setDefaultPageInfo(orderRequest);
//查出order列表
List<Order> orderList = orderMapper.selectList();
//將entity的列表分頁
PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
//連續(xù)顯示5頁與上面的二選一
//PageInfo<Order> orderPageInfo = new PageInfo<>(orderList,5);
//定義一個數(shù)據(jù)傳輸對象dtoList
List<OrderDto> dtoList = new ArrayList<>();
if(null==orderList || orderList.size<=0){
return null;
}
//給dtoList 加值
for(Order order:orderList){
OrderDto dto = new OrderDto();
//將entity 的屬性值 復制給dto上
BeanUtils.copyProperties(order, dto);
dtoList.add(dto);
}
//給dto 分頁
PageInfo<OrderDto> dtoPageInfo = new PageInfo<>(dtoList);
//連續(xù)顯示5頁 與上面的二選一
//PageInfo<Order> orderPageInfo = new PageInfo<>(orderList,5);
//將entity的分頁信息復制給dtoPageInfo上
PageUtils.copyProperties(orderPageInfo, dtoPageInfo);
return dtoPageInfo;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- MybatisPlus 多租戶架構(gòu)(Multi-tenancy)實現(xiàn)詳解
- Mybatis-Plus 多表聯(lián)查分頁的實現(xiàn)代碼
- Mybatis-Plus 搭建與使用入門(小結(jié))
- 基于Mybatis plus 自動代碼生成器的實現(xiàn)代碼
- spring boot整合mybatis+mybatis-plus的示例代碼
- Mybatis批量更新三種方式的實現(xiàn)
- mybatis關系映射之一對多和多對一
- MyBatis插入數(shù)據(jù)返回主鍵的介紹
- Servlet+MyBatis項目轉(zhuǎn)Spring Cloud微服務,多數(shù)據(jù)源配置修改建議
- MyBatis-Plus通過插件將數(shù)據(jù)庫表生成Entiry,Mapper.xml,Mapper.class的方式
相關文章
Java ConcurrentModificationException異常解決案例詳解
這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
MyBatis游標Cursor在Oracle數(shù)據(jù)庫上的測試方式
這篇文章主要介紹了MyBatis游標Cursor在Oracle數(shù)據(jù)庫上的測試方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

