mybati-plus的普通分页插件和基于xml文件的分页插件

 1.相关依赖

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

        <!--    分页插件    -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.4.2</version>
        </dependency>

2.写mybatis-plus的分页插件:

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
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 MybatisPlusConfig {

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

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}

设置好这个之后,就可以使用分页插件了

3.直接上代码

普通分页:

@PostMapping(value = "/getEmployeeLIst")
    public String insert(@RequestBody C c, HttpServletRequest request) throws Exception {

        // 创建条件构造器
        QueryWrapper<DealerEmployeeModel> dealerEmployeeModelQueryWrapper = new QueryWrapper<>();
        // 创建分页对象
        Page<DealerEmployeeModel> dealerEmployeeModelPage = new Page<>(1,2);
        // 查询数据
        dealerEmployeeMapper.selectPage(dealerEmployeeModelPage,dealerEmployeeModelQueryWrapper);

        // 获取集合
        List<DealerEmployeeModel> records = dealerEmployeeModelPage.getRecords();
        // 获取总数量
        long total = dealerEmployeeModelPage.getTotal();
        // 获取总页数
        long pages = dealerEmployeeModelPage.getPages();

        return successResult(records);
    }

xml文件分页:

@PostMapping(value = "/getEmployeeLIst")
    public String insert(@RequestBody C c, HttpServletRequest request) throws Exception {

        // 创建分页对象
        Page<DealerEmployeeModel> dealerEmployeeModelPage = new Page<>(1,2);
        // 查询数据
        List<DealerEmployeeModel> selectarr = dealerEmployeeMapper.selectarr(dealerEmployeeModelPage);

        // 获取总数量
        long total = dealerEmployeeModelPage.getTotal();
        // 获取总页数
        long pages = dealerEmployeeModelPage.getPages();

        return successResult(selectarr);
    }

在这里我直接定义了一个方法,查询的内容写在xml文件上:

List<DealerEmployeeModel> selectarr(Page<DealerEmployeeModel> dealerEmployeeModelPage);
<select id="selectarr" resultType="com.example.mp_pring.entity.DealerEmployeeModel">
        select * from dealer_employee
    </select>

可以看到,我就只是单纯的进行一个查询,但是如果我们把page分页对象,放到接口方法的第一个参数上,就可以自动进行分页,至于结果我就不执行了,各位可以自己尝试一下,很简单的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值