欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决tk mapper 通用mapper的bug问题

程序员文章站 2022-03-11 12:36:40
如下所示:

如下所示:

<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仓库() ,发现公司现在使用的版本已经更新好多个版本了,第一直觉认为是公司用的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下完美解决了~

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

相关标签: tk mapper bug