mybatis升级为mybatis-plus踩到的坑
作者:linyb极客之路
来源:SegmentFault 思否社区
前言
是一个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.xml配置,确实没有发现绑定insert这个sql语句块。那是否加上insert的sql语句块,可以解决问题?加上确实是能解决问题。
但如果用过mybatis-plus的朋友,应该会知道,mybatis-plus中BaseMapper已经帮我们封装好了一部分的单表增删改查,我们无需写配置,就可以实现单表增删改改查。。在xml配置insert是治标不治本。
那要如何排查呢?
方向一:是否是包冲突引起?
利用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.insertat 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提供。而配置是通过
org.apache.ibatis.session.SqlSessionFactory进行设置。然后继续排查,就会发现
com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration这个自动装配类。里面有这么一段代码
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {// TODO 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBeanMybatisSqlSessionFactoryBean 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 此处必为非 NULLGlobalConfig 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 到 MybatisSqlSessionFactoryBeanfactory.setGlobalConfig(globalConfig);return factory.getObject();}
作者在注释上都写了,要用
MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean这样查看若依代码,发现在若依中的mybatis配置类中有配置如下代码片段
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();}
mybatis-plus将不会自动帮我们注入SqlSessionFactory,而使用我们自己定义的SqlSessionFactory。而若依项目配置的SqlSessionFactory不是MybatisSqlSessionFactoryBean
修复
1,方法一
把mybatis的SqlSessionFactoryBean替换成mybatis-plus的MybatisSqlSessionFactoryBean
2,方法二
去掉项目中sqlSessionFactory。这样mybatis-plus就会自动帮我们注入sqlSessionFactory
总结
确实是一个挺好的方法,但有时可能搜索半天都没找到答案,我们就可以通过异常信息栈,以及调试线程栈,就可以得到一些比较有用的信息。出现异常并不可怕,可怕的是出了问题,异常日志信息被吞噬,都不知道从何排查。最后安利一下若依这个脚手架,管理后台开发神器,谁用谁知道
