spring boot通过 mybatis-plus实现分页功能

目录

前言:

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

1.1导入所需依赖 

1.2创建必要mybatis plus文件

二:mapper层接口

 三Service层

四:如何调用分页方法 


前言:

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>

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值