Spring整合MybatisPlus分页插件不起作用问题(此篇文章主要针对原生spring,不针对springboot问题)

Spring整合MybatisPlus分页插件不起作用问题(此篇文章主要针对原生spring,不针对springboot问题)

今天在尝试spring(纯原生spring操作,没有使用springboot)整合MybatisPlus时,用到了分页插件,配置的分页插件不起作用。网上找了一堆方法,千遍一律都是让添加外部的一个Bean(MybatisPlusInterceptor),可是一开始我就注意这个问题了,没用。特此记录一下解决方法。

问题现状

Mybatis的配置类代码

public class MybatisConfig {
    /*开始还遇到继承BaseMapper不起作用的问题,后来百度了很久才找到,
    原来这里不再像mybatis那样加载SqlSessionFactoryBean,而是需要加载MybatisSqlSessionFactoryBean*/
    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:mappers/*.xml");
        factoryBean.setMapperLocations(resources);
        factoryBean.setTypeAliasesPackage("com.haust.pojo");
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        scannerConfigurer.setBasePackage("com.haust.mapper");
        return scannerConfigurer;
    }
    /*网上大部分帖子都是让添加下面这个外部Bean,不过都是针对springboot工程的解决方法,对纯spring不起作用。*/
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

测试代码和运行结果

@Test
public void test1() {
    IPage<User> page = new Page<User>(3,4);
    IPage<User> userIPage = userDao.selectPage(page, null);
    System.out.println("共有数据: " + userIPage.getTotal() + "条");
    System.out.println("共有: " + userIPage.getPages() + "页");
    System.out.println("每页数据: " + userIPage.getSize() + "条");
    System.out.println("当前为第: " + userIPage.getCurrent() + "页");
}

在这里插入图片描述

问题解决

如前文所言,网上找到的帖子千遍一律的让加载一个外部Bean,也就是那个interceptor,然后再在interceptor中加载进去分页插件PaginationInnerInterceptor。可是这些帖子操作都是针对springboot而言的!

解决这个问题还是看到了一个用xml文件配置spring容器的帖子,里面的一段配置让我想到了解决方法。

在这里插入图片描述

可以看到最终配置的插件还需要在加载MybatisSqlSessionFactoryBean时加载进它的plugins配置中。这也说明了其实MybatisPlusInterceptor的作用和数据源DataSource一样,他们要想在MP中起作用,就必须配置到MP的property相应的属性中去。而对于springboot工程来说,这些属性配置已经替你配置好了,你只需要提供一个外部分页插件的Bean就ok了,这也是为什么网上好多帖子让配置一个MybatisPlusInterceptor外部Bean不起作用的问题,因为那些帖子是针对springboot而言的。好了,知道了其中的问题就好解决了。

修改原配置代码

@Bean
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,MybatisPlusInterceptor mybatisPlusInterceptor) throws IOException {
    MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resolver.getResources("classpath:mappers/*.xml");
    factoryBean.setMapperLocations(resources);
    factoryBean.setTypeAliasesPackage("com.haust.pojo");
    /*在加载MybatisSqlSessionFactoryBean时,把相应的插件加载进去*/
    factoryBean.setPlugins(mybatisPlusInterceptor);
    return factoryBean;
}
/*这里不变,还是用来配置一些外部的插件*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
}

再次运行结果

在这里插入图片描述

总结:对spring的加载流程原理不熟。太依赖springboot

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值