栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

mybatis升级为mybatis-plus踩到的坑

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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插件包冲突


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

注: 因为之前吃过包冲突的亏,因此在把若依的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.(MapperMethod.java:235)
	at org.apache.ibatis.binding.MapperMethod.(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()
	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 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

总结

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

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号