Mybatis generates an error after adding a dependency: org.springframework.core.nestedioexception: failed to parse config resource: class path resource [mybatis/mybatis config. XML]; Needed 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
Attachment: springboot running error:
Problem recurrence
After adding logs for mybatis configuration
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
Dependencies in Maven will have red wavy lines: log4j:log4j:unknown
Check the dependency of log4j in pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>compile</scope>
</dependency>
Causes and Solutions
The version number is not written. After consulting the data, it is found that:
Because:
log4j has changed the jar package since version 1.2.17.
for example, the MVN dependency of versions 1.2.17 and earlier is written as follows:
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
However, the MVN dependency of versions after 1.2.17 is written as follows:
<!-- 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>
Therefore, the declaration displayed according to the first method uses version 1.2.17, and the problem is solved.
Later, I want to modify the dependency settings in POM to reproduce the problem. I guess the reason is that Maven has handled the relevant dependencies and needs Maven clean operation
Investigate the reasons:
We know that the dependency in springboot does not need to write the version number because of its automatic version arbitration mechanism. We click the parent project of the project
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
CTRL + left click spring boot starter parent to find that the project also has a parent project spring boot dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.4</version>
</parent>
After entering spring boot dependencies, you can see that it declares the version number and dependencies of jar packages commonly used in the development process
We searched log4j and found that
<log4j2.version>2.14.1</log4j2.version>
It looks like the same reason as we found before.
P.S. SpringBoot runs reporting errors:
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