溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2023-04-03 11:15:55 來(lái)源:億速云 閱讀:140 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“SpringBoot MP簡(jiǎn)單的分頁(yè)查詢測(cè)試怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“SpringBoot MP簡(jiǎn)單的分頁(yè)查詢測(cè)試怎么實(shí)現(xiàn)”吧!

導(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;
    }
}

配置完之后寫一個(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è)位置,寫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é)果:

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

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

感謝各位的閱讀,以上就是“SpringBoot MP簡(jiǎn)單的分頁(yè)查詢測(cè)試怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot MP簡(jiǎn)單的分頁(yè)查詢測(cè)試怎么實(shí)現(xiàn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI