您好,登錄后才能下訂單哦!
這篇文章主要介紹“tk mapper 通用mapper的bug怎么解決”,在日常操作中,相信很多人在tk mapper 通用mapper的bug怎么解決問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”tk mapper 通用mapper的bug怎么解決”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
<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版本
<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
再啟動服務,服務正常使用,熱部署也能夠保留
結合問題三的解決思路,那么咱們在不升級通用mapper的前提下,讓項目依賴打包出來的jar運行,則僅需在
項目src/main/resources目錄新建一個文件夾META-INF(項目右鍵-new-Source Folder),添加熱部署配置文件spring-devtools.properties,在文件里面添加
restart.include.companycommonlibs=kemean.*
問題一就在不升級通用mapper下完美解決了~
到此,關于“tk mapper 通用mapper的bug怎么解決”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。