mybatis升级为mybatis-plus踩到的坑

时间:2022-07-24
本文章向大家介绍mybatis升级为mybatis-plus踩到的坑,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

最近使用RuoYi-Vue来做后台管理脚手架。RuoYi-Vue 是一个 Java EE 企业级快速开发平台,基于经典技术组合(Spring Boot、Spring Security、MyBatis、Jwt、Vue),内置模块如:部门管理、角色用户、菜单及按钮授权、数据权限、系统参数、日志管理、代码生成等。在线定时任务配置;支持集群,支持多数据源。其官方文档如下

http://doc.ruoyi.vip/

感兴趣的朋友,可以点链接查看。这个平台目前的orm框架是mybatis,而项目组的orm框架是mybatis-plus。为了统一技术栈,项目组就决定把若依的orm框架升级为mybatis-plus。因为之前就有过把mybatis升级为mybatis-plus的经验,就感觉这个升级是很简单。但是在改造后,运行程序却报了形如下异常

Invalid bound statement (not found): com.lybgeek.admin.file.mapper.FileMapper.insert

排查

从异常的字面意思是说,FIleMapper中的insert方法没有绑定。查看FileMapper.xml配置,确实没有发现绑定insert这个sql语句块。那是否加上insert的sql语句块,就能解决问题?加上确实是能解决问题。

但如果用过mybatis-plus的朋友,应该会知道,mybatis-plus中BaseMapper已经帮我们封装好了一系列的单表增删改查,我们无需写配置,就可以实现单表增删改查。所以在xml配置insert是治标不治本。

那要如何排查呢?

1、方向一:是否是包冲突引起?

利用maven helper插件包冲突

70fc6fe92c5841b056efaf2c1895b9eb.png

从图可以看出不是包冲突引起的。

注: 因为之前吃过包冲突的亏,因此在把若依的orm改成mybatis-plus之前,就已经去除跟mybatis相关的 jar冲突了

方向二:是不是引入不同类包的BaseMapper

我们引入的必须是

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

而不是

import com.baomidou.mybatisplus.mapper.BaseMapper;

不过出现这个问题,通常也是引入不同版本的mybatis-plus jar才会出现。如果你是只用3+以上版本,他引入就只有

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

方向三:通用方法(断点调试)

其实代码排查最怕就是异常栈被吃了,如果有异常信息,排查方向相对比较好找。比如这个异常,其异常栈信息为

Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.lybgeek.admin.file.mapper.FileMapper.insert
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
	at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:107)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:94)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
	at com.sun.proxy.$Proxy129.insert(Unknown Source)
	at com.baomidou.mybatisplus.extension.service.IService.save(IService.java:59)
	at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

我们从异常栈信息,我们可以知道这个异常从

org.apache.ibatis.binding.MapperMethod

这个类抛出,于是我们可以把断点先设置到这边。通过源码我们可以得知org.apache.ibatis.mapping.MappedStatement空了,导致报了如上异常,而MappedStatement又是由

org.apache.ibatis.session.Configuration

提供。而Configuration是通过

org.apache.ibatis.session.SqlSessionFactory

进行设置。然后继续排查,就会发现

com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

这个自动装配类。里面有这么一段代码

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        // TODO 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
        applyConfiguration(factory);
        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            factory.setPlugins(this.interceptors);
        }
        if (this.databaseIdProvider != null) {
            factory.setDatabaseIdProvider(this.databaseIdProvider);
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        if (this.properties.getTypeAliasesSuperType() != null) {
            factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.typeHandlers)) {
            factory.setTypeHandlers(this.typeHandlers);
        }
        Resource[] mapperLocations = this.properties.resolveMapperLocations();
        if (!ObjectUtils.isEmpty(mapperLocations)) {
            factory.setMapperLocations(mapperLocations);
        }

        // TODO 对源码做了一定的修改(因为源码适配了老旧的mybatis版本,但我们不需要适配)
        Class<? extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultScriptingLanguageDriver();
        if (!ObjectUtils.isEmpty(this.languageDrivers)) {
            factory.setScriptingLanguageDrivers(this.languageDrivers);
        }
        Optional.ofNullable(defaultLanguageDriver).ifPresent(factory::setDefaultScriptingLanguageDriver);

        // TODO 自定义枚举包
        if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) {
            factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage());
        }
        // TODO 此处必为非 NULL
        GlobalConfig globalConfig = this.properties.getGlobalConfig();
        // TODO 注入填充器
        this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
        // TODO 注入主键生成器
        this.getBeanThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerator(i));
        // TODO 注入sql注入器
        this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
        // TODO 注入ID生成器
        this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
        // TODO 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
        factory.setGlobalConfig(globalConfig);
        return factory.getObject();
    }

作者在注释上都写了,要用

 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean

于是查看若依代码,发现在若依中的mybatis配置类中有配置如下代码片段

 @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
    {
        String typeAliasesPackage = env.getProperty("mybatis.type-aliases-package");
        String mapperLocations = env.getProperty("mybatis.mapper-locations");
        String configLocation = env.getProperty("mybatis.config-location");
        typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
        VFS.addImplClass(SpringBootVFS.class);

        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
        return sessionFactory.getObject();
    }

从MybatisPlusAutoConfiguration的源码中,我们可以得知,当项目已经有配置SqlSessionFactory。mybatis-plus将不会自动帮我们注入SqlSessionFactory,而使用我们自己定义的SqlSessionFactory。而若依项目配置的SqlSessionFactory不是MybatisSqlSessionFactoryBean

修复

1、方法一

把mybatis的SqlSessionFactoryBean替换成mybatis-plus的MybatisSqlSessionFactoryBean

2、方法二

去掉项目中sqlSessionFactory。这样mybatis-plus就会自动帮我们注入sqlSessionFactory

总结

可能有朋友会觉得遇到异常问题,直接通过搜索引擎找答案不就可以了。这确实是一个挺好的方法,但有时候可能搜索半天都没找到答案,我们就可以通过异常信息栈、以及调试线程栈,就可以得出一些比较有用的信息。出现异常并不可怕,可怕的是出了问题,异常日志信息被吞,都不知道从何排查。最后安利一下若依这个脚手架,管理后台开发神器,谁用谁知道