遇到的问题
描述:在一个新建的模块内新增数据库连接+Mybatis-plus,添加依赖以后,出现启动失败。
报错信息如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mybatisInterceptorAutoConfiguration': Unsatisfied dependency expressed through field 'sqlSessionFactoryList'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [C:\Users\DELL\IdeaProjects\DBPT\dbpt-service\dbpt-auth\target\classes\mapper\CertificateAuthorizationMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [C:\Users\DELL\IdeaProjects\DBPT\dbpt-service\dbpt-auth\target\classes\mapper\CertificateAuthorizationMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'cn.org.enst.auth.domain.CertificateAuthorization'. Cause: java.lang.ClassNotFoundException: Cannot find class: cn.org.enst.auth.domain.CertificateAuthorization
报错问题点截图如下:
原因
我这个问题是因为项目中两个依赖包发生冲突,很明显一个tk的mybatis和mybatis-plus发生了冲突,导致Springboot加载bean的时候出现问题。
解决方案
解决方案很简单,排除掉其中一个依赖即可。
通过pom文件排除以来写法如下:
<dependency>
<groupId>cn.org.enst.cloud</groupId>
<artifactId>dbpt-common-core</artifactId>
<version>${dbpt.version}</version>
<exclusions>
<exclusion>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
注意:排除依赖的以来引入本项目中要写在pom文件上方,pom文件有加载顺序,尽量避免这个可能发生的问题。
修改前有tk.mybatis依赖在该项目中。
排除后该依赖会不加载即为成功。
排查思路
正常的排查思路应该是。
①controller层注解
②service层注解
③dao层注解或者启动类上的包扫描
④Autowired
⑤配置文件中mybatis相关配置
⑥pom文件依赖问题