项目环境

通用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错误(问题一)。

  1. 问题一

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】项目升级通用Mapper引发的一连串问题以及问题解决-LMLPHP

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

问题一的最终解决

结合问题三的解决思路,那么咱们在不升级通用mapper的前提下,让项目依赖打包出来的jar运行,则仅需在
项目src/main/resources目录新建一个文件夹META-INF(项目右键-new-Source Folder),添加热部署配置文件spring-devtools.properties,在文件里面添加

restart.include.companycommonlibs=kemean.*

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

end

推荐文章:
MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件
Eclipse新建Spring-boot项目,打包部署并输出HelloWord
Spring boot基于Redis缓存商城分类,商品信息

10-03 20:31