前言:

mybatis plus自带分页功能,而且在spring boot项目中使用起来比ssm项目更加方便

一:spring boot项目导入mybatis plus所需配置

1.1导入所需依赖 

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

1.2创建必要mybatis plus文件

package com.dmdd.springbootwedding.interceptor;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@MapperScan(basePackages = "com.dmdd.springbootwedding.mapper")
@Configuration
public class MybatisPlusConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }


}

二:mapper层接口

 输入Ipage 然后就可以直接调用mybatis plus提供给我们的分页方法了

注意:需要查询条件的话可以加额外的参数,不需要的话写法如下,传一个IPage就可以了

 

 三Service层

懂得都懂 不解释了 

四:如何调用分页方法 

1:创建page对象

2:Page类继承了 Ipage接口,因此可以将page类当做参数传进去。

 页面分页功能展示

 

 

 

最后附上前端分页代码 

                <div class="us_PageCut">
                        <span th:if="${current}==1">
                            <a class="prev disabled" th:href="@{toOrder(current=${current}-1)}"><</a>
                        </span>
                    <span th:if="${current}>1">
                            <a class="prev" th:href="@{toOrder(current=${current}-1)}"><</a>
                        </span>

                    <a class="cur" th:each="i:${#numbers.sequence(1,allPage)}" th:href="@{toOrder(current=${i})}"
                       th:text="${i}">1</a>
                    <span th:if="${current}==${allPage}">
                            <a class="next disabled">></a>
                        </span>
                    <span th:if="${current}<${allPage}">
                            <a class="next" th:href="@{toOrder(current=${current}+1)}">></a>
                        </span>
                </div>

版权声明:本文为qq_60614034原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_60614034/article/details/128764577