亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot?MP簡(jiǎn)單的分頁(yè)查詢測(cè)試實(shí)現(xiàn)步驟分解

 更新時(shí)間:2023年04月03日 10:56:57   作者:ForestSpringH  
好久沒(méi)水后端的東西了,最近在做vue項(xiàng)目寫(xiě)前端的代碼,所以cloud也停進(jìn)度了,吃完飯突然記得我沒(méi)有在博客里寫(xiě)分頁(yè)的東西,雖然項(xiàng)目中用到了,但是沒(méi)有拎出來(lái),這里就拎出來(lái)看看

導(dǎo)入最新的mp依賴是第一步不然太低的版本什么都做不了,3,1以下的好像連分頁(yè)插件都沒(méi)有加進(jìn)去,所以我們用最新的3.5的,保證啥都有:

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>

這里我們需要認(rèn)識(shí)兩個(gè)插件:mp的核心插件MybatisPlusInterceptor與自動(dòng)分頁(yè)插件PaginationInnerInterceptor。

MybatisPlusInterceptor的源碼(去掉中間的處理代碼):

public class MybatisPlusInterceptor implements Interceptor {
    private List<InnerInterceptor> interceptors = new ArrayList();
    public MybatisPlusInterceptor() {}
    public Object intercept(Invocation invocation) throws Throwable {}
    public Object plugin(Object target) {}
    public void addInnerInterceptor(InnerInterceptor innerInterceptor) {}
    public List<InnerInterceptor> getInterceptors() {}
    public void setProperties(Properties properties) {}
    public void setInterceptors(final List<InnerInterceptor> interceptors) {}
}

我們可以發(fā)現(xiàn)它有一個(gè)私有的屬性列表 List<InnerInterceptor> 而這個(gè)鏈表中的元素類型是InnerInterceptor。

InnerInterceptor源碼:

public interface InnerInterceptor {
    default boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        return true;
    }
    default void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    }
    default boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        return true;
    }
    default void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
    }
    default void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
    }
    default void beforeGetBoundSql(StatementHandler sh) {
    }
    default void setProperties(Properties properties) {
    }
}

不難發(fā)現(xiàn)這個(gè)接口的內(nèi)容大致就是設(shè)置默認(rèn)的屬性,從代碼的意思上就是提供默認(rèn)的數(shù)據(jù)庫(kù)操作執(zhí)行時(shí)期前后執(zhí)行的一些邏輯,誰(shuí)實(shí)現(xiàn)它的方法會(huì)得到新的功能?

再看看PaginationInnerInterceptor插件的源碼:

public class PaginationInnerInterceptor implements InnerInterceptor {
    protected static final List<SelectItem> COUNT_SELECT_ITEM = Collections.singletonList((new SelectExpressionItem((new Column()).withColumnName("COUNT(*)"))).withAlias(new Alias("total")));
    protected static final Map<String, MappedStatement> countMsCache = new ConcurrentHashMap();
    protected final Log logger = LogFactory.getLog(this.getClass());
    protected boolean overflow;
    protected Long maxLimit;
    private DbType dbType;
    private IDialect dialect;
    protected boolean optimizeJoin = true;
    public PaginationInnerInterceptor(DbType dbType) {
        this.dbType = dbType;
    }
    public PaginationInnerInterceptor(IDialect dialect) {
        this.dialect = dialect;
    }
    public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        IPage<?> page = (IPage)ParameterUtils.findPage(parameter).orElse((Object)null);
        if (page != null && page.getSize() >= 0L && page.searchCount()) {
            MappedStatement countMs = this.buildCountMappedStatement(ms, page.countId());
            BoundSql countSql;
            if (countMs != null) {
                countSql = countMs.getBoundSql(parameter);
            } else {
                countMs = this.buildAutoCountMappedStatement(ms);
                String countSqlStr = this.autoCountSql(page, boundSql.getSql());
                MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
                countSql = new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);
                PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());
            }
            CacheKey cacheKey = executor.createCacheKey(countMs, parameter, rowBounds, countSql);
            List<Object> result = executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql);
            long total = 0L;
            if (CollectionUtils.isNotEmpty(result)) {
                Object o = result.get(0);
                if (o != null) {
                    total = Long.parseLong(o.toString());
                }
            }
            page.setTotal(total);
            return this.continuePage(page);
        } else {
            return true;
        }
    }
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {...........省略之后全部的內(nèi)容........}

我們不難發(fā)現(xiàn)它實(shí)現(xiàn)了來(lái)自于InnerInterceptor的方法,這里面的源碼有時(shí)間需要好好處處邏輯。

我們知道了分頁(yè)插件和核心插件的關(guān)系,也就是我們可以將分頁(yè)插件添加入核心插件內(nèi)部的插件鏈表中去,從而實(shí)現(xiàn)多功能插件的使用。

配置mp插件,并將插件交由spring管理(我們用的是springboot進(jìn)行測(cè)試所以不需要使用xml文件):

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MpConfig {
    /*分頁(yè)插件的配置*/
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        /*創(chuàng)建mp攔截器*/
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        /*創(chuàng)建分頁(yè)插件*/
        PaginationInnerInterceptor pagInterceptor = new PaginationInnerInterceptor();
        /*設(shè)置請(qǐng)求的頁(yè)面大于最大頁(yè)容量后的請(qǐng)求操作,true回調(diào)第一頁(yè),false繼續(xù)翻頁(yè),默認(rèn)翻頁(yè)*/
        pagInterceptor.setOverflow(false);
        /*設(shè)置單頁(yè)分頁(yè)的條數(shù)限制*/
        pagInterceptor.setMaxLimit(500L);
        /*設(shè)置數(shù)據(jù)庫(kù)類型*/
        pagInterceptor.setDbType(DbType.MYSQL);
        /*將分頁(yè)攔截器添加到mp攔截器中*/
        interceptor.addInnerInterceptor(pagInterceptor);
        return interceptor;
    }
}

配置完之后寫(xiě)一個(gè)Mapper接口:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hlc.mp.entity.Product;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}

為接口創(chuàng)建一個(gè)服務(wù)類(一定按照mp編碼的風(fēng)格來(lái)):

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlc.mp.entity.Product;
import com.hlc.mp.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "ProductService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements IService<Product> {
    @Autowired
    ProductMapper productMapper;
    /**
     * 根據(jù)傳入的頁(yè)碼進(jìn)行翻頁(yè)
     *
     * @param current 當(dāng)前頁(yè)碼(已經(jīng)約定每頁(yè)數(shù)據(jù)量是1條)
     * @return 分頁(yè)對(duì)象
     */
    public Page<Product> page(Long current) {
        /*current首頁(yè)位置,寫(xiě)1就是第一頁(yè),沒(méi)有0頁(yè)之說(shuō),size每頁(yè)顯示的數(shù)據(jù)量*/
        Page<Product> productPage = new Page<>(current, 1);
        /*條件查詢分頁(yè)*/
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        productMapper.selectPage(productPage, queryWrapper);
        return productPage;
    }
}

到這里我們可以看到分頁(yè)的具體方法就是,先創(chuàng)建一個(gè)分頁(yè)對(duì)象,規(guī)定頁(yè)碼和每一頁(yè)的數(shù)據(jù)量的大小,其次確定查詢操作的范圍,并使用BaseMapper<T>給予我們的查詢分頁(yè)方法selectPage(E page,Wapper<T> queryWapper)進(jìn)行查詢分頁(yè)的操作。

測(cè)試類:

    @Test
    public void testPage(){
        IPage<Product> productIPage = productService.page(2L);
        productIPage.getRecords().forEach(System.out::println);
        System.out.println("當(dāng)前頁(yè)碼"+productIPage.getCurrent());
        System.out.println("每頁(yè)顯示數(shù)量"+productIPage.getSize());
        System.out.println("總頁(yè)數(shù)"+productIPage.getPages());
        System.out.println("數(shù)據(jù)總量"+productIPage.getTotal());
    }

運(yùn)行查看分頁(yè)結(jié)果:

我們可以發(fā)現(xiàn)都正常的按照我們傳入的頁(yè)碼去查詢對(duì)應(yīng)的頁(yè)數(shù)據(jù)了,因?yàn)槲以O(shè)置的每頁(yè)只展示一條數(shù)據(jù),所以ID如果對(duì)應(yīng)頁(yè)碼就說(shuō)明分頁(yè)成功。

到此這篇關(guān)于SpringBoot MP簡(jiǎn)單的分頁(yè)查詢測(cè)試實(shí)現(xiàn)步驟分解的文章就介紹到這了,更多相關(guān)SpringBoot MP分頁(yè)查詢測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Maven優(yōu)雅的添加第三方Jar包的方法

    Maven優(yōu)雅的添加第三方Jar包的方法

    下面小編就為大家?guī)?lái)一篇Maven優(yōu)雅的添加第三方Jar包的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • Spring boot定時(shí)任務(wù)的原理及動(dòng)態(tài)創(chuàng)建詳解

    Spring boot定時(shí)任務(wù)的原理及動(dòng)態(tài)創(chuàng)建詳解

    這篇文章主要給大家介紹了關(guān)于Spring boot定時(shí)任務(wù)的原理及動(dòng)態(tài)創(chuàng)建的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java實(shí)現(xiàn)Excel文件加密解密的示例代碼

    Java實(shí)現(xiàn)Excel文件加密解密的示例代碼

    設(shè)置excel文件保護(hù)時(shí),通??蛇x擇對(duì)整個(gè)工作簿進(jìn)行加密保護(hù)。無(wú)需設(shè)置文檔保護(hù)時(shí),可撤銷密碼保護(hù),即解密文檔。本文將通過(guò)java程序演示以上加密、解密方法的實(shí)現(xiàn),感興趣的可以了解一下
    2022-05-05
  • java實(shí)現(xiàn)電腦端掃描二維碼

    java實(shí)現(xiàn)電腦端掃描二維碼

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)電腦端掃描二維碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式

    Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式

    這篇文章主要介紹了Spring Boot mybatis-config 和 log4j 輸出sql 日志的方式,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • 為什么SpringMVC中請(qǐng)求的body不支持多次讀取

    為什么SpringMVC中請(qǐng)求的body不支持多次讀取

    這篇文章主要介紹了為什么SpringMVC中請(qǐng)求的body不支持多次讀取,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java 異步回調(diào)機(jī)制實(shí)例分析

    Java 異步回調(diào)機(jī)制實(shí)例分析

    這篇文章主要介紹了Java 異步回調(diào)機(jī)制實(shí)例解析的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • java 實(shí)例化類詳解及簡(jiǎn)單實(shí)例

    java 實(shí)例化類詳解及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了java 實(shí)例化類詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 200行java代碼實(shí)現(xiàn)2048小游戲

    200行java代碼實(shí)現(xiàn)2048小游戲

    這篇文章主要為大家詳細(xì)介紹了200行java代碼實(shí)現(xiàn)2048小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • java計(jì)算時(shí)間差的方法

    java計(jì)算時(shí)間差的方法

    這篇文章主要介紹了java計(jì)算時(shí)間差的方法,涉及java針對(duì)時(shí)間的轉(zhuǎn)換與計(jì)算相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07

最新評(píng)論