您好,登錄后才能下訂單哦!
小編給大家分享一下mybatis升級為mybatis-plus需要注意什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
最近使用RuoYi-Vue來做后臺(tái)管理腳手架。RuoYi-Vue 是一個(gè) Java EE 企業(yè)級快速開發(fā)平臺(tái),基于經(jīng)典技術(shù)組合(Spring Boot、Spring Security、MyBatis、Jwt、Vue),內(nèi)置模塊如:部門管理、角色用戶、菜單及按鈕授權(quán)、數(shù)據(jù)權(quán)限、系統(tǒng)參數(shù)、日志管理、代碼生成等。在線定時(shí)任務(wù)配置;支持集群,支持多數(shù)據(jù)源。其官方文檔如下
http://doc.ruoyi.vip/
感興趣的朋友,可以點(diǎn)鏈接查看。這個(gè)平臺(tái)目前的orm框架是mybatis,而項(xiàng)目組的orm框架是mybatis-plus。為了統(tǒng)一技術(shù)棧,項(xiàng)目組就決定把若依的orm框架升級為mybatis-plus。因?yàn)橹熬陀羞^把mybatis升級為mybatis-plus的經(jīng)驗(yàn),就感覺這個(gè)升級是很簡單。但是在改造后,運(yùn)行程序卻報(bào)了形如下異常
Invalid bound statement (not found): com.lybgeek.admin.file.mapper.FileMapper.insert
從異常的字面意思是說,F(xiàn)IleMapper中的insert方法沒有綁定。查看FileMapper.xml配置,確實(shí)沒有發(fā)現(xiàn)綁定insert這個(gè)sql語句塊。那是否加上insert的sql語句塊,就能解決問題?加上確實(shí)是能解決問題。
但如果用過mybatis-plus的朋友,應(yīng)該會(huì)知道,mybatis-plus中BaseMapper已經(jīng)幫我們封裝好了一系列的單表增刪改查,我們無需寫配置,就可以實(shí)現(xiàn)單表增刪改查。所以在xml配置insert是治標(biāo)不治本。
那要如何排查呢?
1、方向一:是否是包沖突引起?
利用maven helper插件包沖突
從圖可以看出不是包沖突引起的。
注: 因?yàn)橹俺赃^包沖突的虧,因此在把若依的orm改成mybatis-plus之前,就已經(jīng)去除跟mybatis相關(guān)的 jar沖突了
方向二:是不是引入不同類包的BaseMapper
我們引入的必須是
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
而不是
import com.baomidou.mybatisplus.mapper.BaseMapper;
不過出現(xiàn)這個(gè)問題,通常也是引入不同版本的mybatis-plus jar才會(huì)出現(xiàn)。如果你是只用3+以上版本,他引入就只有
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
方向三:通用方法(斷點(diǎn)調(diào)試)
其實(shí)代碼排查最怕就是異常棧被吃了,如果有異常信息,排查方向相對比較好找。比如這個(gè)異常,其異常棧信息為
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)
我們從異常棧信息,我們可以知道這個(gè)異常從
org.apache.ibatis.binding.MapperMethod
這個(gè)類拋出,于是我們可以把斷點(diǎn)先設(shè)置到這邊。通過源碼我們可以得知org.apache.ibatis.mapping.MappedStatement
空了,導(dǎo)致報(bào)了如上異常,而MappedStatement又是由
org.apache.ibatis.session.Configuration
提供。而Configuration是通過
org.apache.ibatis.session.SqlSessionFactory
進(jìn)行設(shè)置。然后繼續(xù)排查,就會(huì)發(fā)現(xiàn)
com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
這個(gè)自動(dòng)裝配類。里面有這么一段代碼
@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 對源碼做了一定的修改(因?yàn)樵创a適配了老舊的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 設(shè)置 GlobalConfig 到 MybatisSqlSessionFactoryBean factory.setGlobalConfig(globalConfig); return factory.getObject(); }
作者在注釋上都寫了,要用
MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
于是查看若依代碼,發(fā)現(xiàn)在若依中的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的源碼中,我們可以得知,當(dāng)項(xiàng)目已經(jīng)有配置SqlSessionFactory。mybatis-plus將不會(huì)自動(dòng)幫我們注入SqlSessionFactory,而使用我們自己定義的SqlSessionFactory。而若依項(xiàng)目配置的SqlSessionFactory不是MybatisSqlSessionFactoryBean
1、方法一
把mybatis的SqlSessionFactoryBean替換成mybatis-plus的MybatisSqlSessionFactoryBean
2、方法二
去掉項(xiàng)目中sqlSessionFactory。這樣mybatis-plus就會(huì)自動(dòng)幫我們注入sqlSessionFactory
以上是“mybatis升級為mybatis-plus需要注意什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。