解决tk mapper 通用mapper的bug问题


Posted in Java/Android onJune 16, 2021

如下所示:

<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.isea533.mybatis.mapper"/>
            <property name="properties">
                <value>
                    mappers=tk.mybatis.mapper.common.Mapper
                </value>
            </property>
        </bean>

basePackage的属性值 不能包含通用mapper所在的包

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-03-14 12:39:21.741 ERROR [main] [hio-sys] o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tk.mybatis.mapper.autoconfigure.MapperAutoConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseMapper' defined in file []: Invocation of init method failed; nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:543)
    at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at com.jzy.hio.HioSysApplication.main(HioSysApplication.java:35)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseMapper' defined in file [C:\Users\coffee\OneDrive\workspace\v2r1-hio-base\target\classes\com\jzy\hio\base\mapper\BaseMapper.class]: Invocation of init method failed; nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType_jrOrig(DefaultListableBeanFactory.java:519)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType_jrOrig(DefaultListableBeanFactory.java:508)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java)
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1188)
    at tk.mybatis.mapper.autoconfigure.MapperAutoConfiguration.addPageInterceptor(MapperAutoConfiguration.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
    ... 18 common frames omitted
Caused by: tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at tk.mybatis.mapper.mapperhelper.MapperHelper.setSqlSource(MapperHelper.java:248)
    at tk.mybatis.mapper.mapperhelper.MapperHelper.processConfiguration(MapperHelper.java:311)
    at tk.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:54)
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 39 common frames omitted
Caused by: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at tk.mybatis.mapper.mapperhelper.MapperTemplate.setSqlSource(MapperTemplate.java:258)
    at tk.mybatis.mapper.mapperhelper.MapperHelper.setSqlSource(MapperHelper.java:245)
    ... 44 common frames omitted
Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at tk.mybatis.mapper.mapperhelper.MapperTemplate.getEntityClass(MapperTemplate.java:290)
    at tk.mybatis.mapper.provider.base.BaseSelectProvider.selectOne(BaseSelectProvider.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at tk.mybatis.mapper.mapperhelper.MapperTemplate.setSqlSource(MapperTemplate.java:246)
    ... 45 common frames omitted

(通用mapper)项目升级通用Mapper引发的一连串问题以及问题解决

项目环境

通用Mapper版本

<dependency>
  <groupId>tk.mybatis</groupId>
  <artifactId>mapper-spring-boot-starter</artifactId>
  <version>1.1.5</version>
 </dependency>

Spring boot版本

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
  <relativePath />
 </parent>

公司统一封装了三个核心包(kemean-aid、kemean-third、kemean-web),平时技术开发都是把三核心包下载源码到本地启动运行,这个星期把三个核心包打包成jar,让项目依赖jar启动,但启动的时候报了一个maven错误(问题一)。

问题一

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/springframework/boot/devtools/restart/classloader/RestartClassLoader) previously initiated loading for a different type with name "tk/mybatis/mapper/common/Mapper"

maven报了一个重复引用通用Mapper Jar错误,但反复查看maven引用,并没有发现有重复引用通用Mapper jar包,而且项目引用源码启动是正常运行的,这个问题着实报得有点诡异!因为经验问题,重复就maven报的这个问题寻找重复引用的jar,然而处理了很久也并没有解决。后来查看通用mapper的版本maven仓库(https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter) ,发现公司现在使用的版本已经更新好多个版本了,第一直觉认为是公司用的1.1.5这个版本有问题,于是乎升级了通用mapper版本到2版本(当前最新2.0.4),就这样掉大坑了,开始一系列的问题解决的路程。

<dependency>
     <groupId>tk.mybatis</groupId>
     <artifactId>mapper-spring-boot-starter</artifactId>
     <version>2.0.4</version>
 </dependency>

以下的问题都是在2版本的通用mapper中遇到的

问题二

tk.mybatis.mapper.MapperException: 无法获取实体类com.kemean.bean.KemeanAdminUser对应的表名!

github也有此问题的讨论:https://github.com/abel533/MyBatis-Spring-Boot/issues/18

问题解决

修改启动类@MapperScan注解

2版本的通用Mapper提供了@MapperScan注解,之前我们使用的是org.mybatis.spring.annotation.MapperScan,现需把@MapperScan改为tk.mybatis.spring.annotation.MapperScan

添加spring-devtools.properties文件

在项目src/main/resources目录新建一个文件夹META-INF(项目右键-new-Source Folder),添加热部署配置文件spring-devtools.properties,在文件里面添加restart.include.companycommonlibs=tk/mybatis.*

问题三

tk.mybatis.mapper.MapperException: tk.mybatis.mapper.provider.EmptyProvider中缺少selectOne方法!

github也有此问题的讨论:

https://github.com/abel533/MyBatis-Spring-Boot/issues/92

https://github.com/abel533/MyBatis-Spring-Boot/issues/53

问题解决:把项目热部署去掉

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
</dependency>

在处理完问题二、三,通用Mapper就升级成功了,项目也能够正常启动访问,但是,咱们在处理问题三的时候,把spring boot的热部署给去掉了,这就意味着咱们日后每改动一点,都需要重新启动部署服务,这是士可忍,叔不可忍;叔可忍,婶也不能忍的操作啊。

尝试过好多方式,都不能“升级通用mapper2版本”与“保留Spring boot热部署”两个兼得,后来看到这哥们的一句话,给了我一个提醒,于是乎在刚才添加的spring-devtools.properties文件再补充一行restart.include.companycommonlibs=kemean.*

spring-devtools.properties内容(kemean.*是公司jar的前缀)

restart.include.companycommonlibs=tk/mybatis.*
restart.include.companycommonlibs=kemean.*

https://github.com/abel533/MyBatis-Spring-Boot/issues/53

解决tk mapper 通用mapper的bug问题

再启动服务,服务正常使用,热部署也能够保留

问题一的最终解决

结合问题三的解决思路,那么咱们在不升级通用mapper的前提下,让项目依赖打包出来的jar运行,则仅需在

项目src/main/resources目录新建一个文件夹META-INF(项目右键-new-Source Folder),添加热部署配置文件spring-devtools.properties,在文件里面添加

restart.include.companycommonlibs=kemean.*

问题一就在不升级通用mapper下完美解决了~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Java/Android 相关文章推荐
新手入门Jvm-- JVM对象创建与内存分配机制
Jun 18 Java/Android
Java常用工具类汇总 附示例代码
Jun 26 Java/Android
Java实现二维数组和稀疏数组之间的转换
Jun 27 Java/Android
深入浅出讲解Java8函数式编程
Jan 18 Java/Android
Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解
Mar 21 Java/Android
Java中Dijkstra(迪杰斯特拉)算法
May 20 Java/Android
SpringBoot全局异常处理方案分享
May 25 Java/Android
Spring Boot项目如何优雅实现Excel导入与导出功能
Jun 10 Java/Android
Spring Cloud OAuth2实现自定义token返回格式
Jun 25 Java/Android
springboot 全局异常处理和统一响应对象的处理方式
Jun 28 Java/Android
Java多线程并发FutureTask使用详解
Jun 28 Java/Android
Spring boot实现上传文件到本地服务器
Aug 14 Java/Android
一篇带你入门Java垃圾回收器
Java实现简易的分词器功能
Java用自带的Image IO给图片添加水印
java Nio使用NioSocket客户端与服务端交互实现方式
JVM入门之类加载与字节码技术(类加载与类的加载器)
Spring Cache和EhCache实现缓存管理方式
浅析NIO系列之TCP
You might like
【动漫杂谈】关于《请在T台上微笑》
2020/03/03 日漫
利用php+mcDropdown实现文件路径可在下拉框选择
2013/08/07 PHP
PHP生成随机密码方法汇总
2015/08/27 PHP
深入理解PHP中的count函数
2016/05/31 PHP
简单PHP会话(session)说明介绍
2016/08/21 PHP
php写一个函数,实现扫描并打印出自定目录下(含子目录)所有jpg文件名
2017/05/26 PHP
PHP开发之归档格式phar文件概念与用法详解【创建,使用,解包还原提取】
2017/11/17 PHP
显示、隐藏密码
2006/07/01 Javascript
javascript parseInt() 函数的进制转换注意细节
2013/01/08 Javascript
jQuery制作仿腾讯web qq用户体验桌面
2013/08/20 Javascript
js判断是否为ie的方法小结
2014/01/13 Javascript
toggle()隐藏问题的解决方法
2014/02/17 Javascript
jQuery简单实现仿京东分类导航层效果
2016/06/07 Javascript
详解如何构建Angular项目目录结构
2017/07/13 Javascript
Node.js如何实现注册邮箱激活功能 (常见)
2017/07/23 Javascript
详解在express站点中使用ejs模板引擎
2017/09/21 Javascript
JS实现的数组去除重复数据算法小结
2017/11/17 Javascript
vue组件之间的数据传递方法详解
2019/04/19 Javascript
vue 搭建后台系统模块化开发详解
2019/05/01 Javascript
js实现缓动动画
2020/11/25 Javascript
[37:47]IG vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
使用Python编写基于DHT协议的BT资源爬虫
2016/03/19 Python
Python使用gRPC传输协议教程
2018/10/16 Python
Windows系统下PhantomJS的安装和基本用法
2018/10/21 Python
将matplotlib绘图嵌入pyqt的方法示例
2020/01/08 Python
Python中断多重循环的几种方式详解
2020/02/10 Python
浅谈Python3多线程之间的执行顺序问题
2020/05/02 Python
用css3实现当鼠标移进去时当前亮其他变灰效果
2014/04/08 HTML / CSS
HTML5 video 事件应用示例
2014/09/11 HTML / CSS
方太官方网上商城:销售方太抽油烟机、燃气灶、消毒柜等
2017/01/17 全球购物
澳大利亚领先的内衣店:Bendon Lingerie澳大利亚
2020/05/15 全球购物
我想声明一个指针并为它分配一些空间, 但却不行。这些代码有什么 问题?char *p; *p = malloc(10);
2016/10/06 面试题
经验丰富大学生村干部自我鉴定
2014/01/22 职场文书
干部培训自我鉴定
2014/01/22 职场文书
安全生产年活动总结
2014/08/29 职场文书
张思德观后感
2015/06/09 职场文书