mybatis plus 日志打印_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.insertat 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@ConditionalOnMissingBeanpublic 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配置類中有配置如下代碼片段
@Beanpublic 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
總結
可能有朋友會覺得遇到異常問題,直接通過搜索引擎找答案不就可以了。這確實是一個挺好的方法,但有時候可能搜索半天都沒找到答案,我們就可以通過異常信息棧、以及調試線程棧,就可以得出一些比較有用的信息。出現異常并不可怕,可怕的是出了問題,異常日志信息被吞,都不知道從何排查。最后安利一下若依這個腳手架,管理后臺開發神器,誰用誰知道
總結
以上是生活随笔為你收集整理的mybatis plus 日志打印_mybatis升级为mybatis-plus踩到的坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: websocket心跳链接代码_Hype
- 下一篇: pytorch 训练过程acc_pyto