Mybatis添加依赖后产生错误 org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.

Mybatis添加依赖后产生错误:org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation. Cause: java.lang.reflect.InvocationTargetExceptionlog4j引发的错误

问题复现

mybatis配置添加日志后

<settings>
    <setting name="logImpl" value="LOG4J" />
</settings>

maven中的Dependencies会有红色波浪线:log4j:log4j:unknown

查看了一下pom.xml中关于log4j的依赖

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <scope>compile</scope>
</dependency>

产生原因及解决

其中没有写版本号,查阅资料后发现:

因为:
log4j自1.2.17版本以后,就改变jar包了
比如:1.2.17及以前的版本的mvn依赖是这样写的:

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

然而1.2.17以后的版本的mvn依赖是这样写的:

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.13.1</version>
</dependency>

于是,按第一种方法显示的声明使用1.2.17版本,问题解决。

之后想要通过修改pom中依赖的设置再复现问题发现复现不了,我猜测的原因是maven已经处理好了相关的依赖,需要maven clean操作

深究原因:

我们知道SpringBoot中的依赖不用写版本号是因为其自动版本仲裁机制,我们点击项目的父项目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

ctrl加左键点击spring-boot-starter-parent,发现这个项目还有父项目spring-boot-dependencies

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.5.4</version>
</parent>

进入spring-boot-dependencies后可以看到里面声明了开发过程中常用的jar包的版本号和依赖

我们搜索log4j后看到结果是

<log4j2.version>2.14.1</log4j2.version>

看起来和我们之前找到的原因相同。

附:SpringBoot运行报错:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookService': Unsatisfied dependency expressed through field 'bookMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookMapper' defined in file [D:\tsgl\target\classes\com\by\tsgl\mapper\BookMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.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 config resource: class path resource [mybatis/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation. Cause: java.lang.reflect.InvocationTargetException

猜你喜欢

转载自blog.csdn.net/Zilong0128/article/details/121129210
今日推荐