Maven source change & Mybatis open Log4j log framework

This article mainly explains how to replace the maven domestic mirror source and Mybatis to open the Log4j log framework related operations in the idea development environment

Maven source change

Domestic mirror address : https://developer.aliyun.com/mvn/guide

Find conf in maven and configure settings.xml

Do not modify the configuration in the default maven plugin directory of idea, otherwise it will be overwritten again in the next update and will be invalid! ! !

Others will not be described in detail, readers are asked to inquire by themselves

Here we mainly describe the configuration items for maven in the idea development environment. Before starting, we first distinguish two settings

  • Project settings, only take effect for the current project;
  • The development environment is set by default, and the relevant configuration is read from here every time a project is built;

Since we have configured the maven repository locally and do not want to use the default location (usually under the c drive letter), we need to update it in the idea

1. Project settings

Enter the settings and find maven

2. Default settings

If you don't want to need to reconfigure every time you create a new project, it's better to do the following

Mybatis opens Log4j log framework

ration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation. Cause: java.lang.NoClassDefFoundError: org/apache/log4j/Priority

Most of the reference tutorial configuration items on the Internet are still log4j 1.2.17 version. The security holes of this version are disabled in idea. If readers cannot find a higher or latest version by themselves, please refer to the following tutorial

maven central warehouse search log4j results

Found that the location has been moved and came to a new location

It can be seen that the bug has been fixed

Click the appropriate version and select the corresponding import method, (here take maven as an example) and add it to the pom file

pom.xml

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.19.0</version>
</dependency>

log4j.properties

#声明日志的输出级别及输出方式
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
# MyBatis 日志配置 (记得修改为你的项目对应文件)
log4j.logger.com.jason.jasonMapper=TRACE
# Console output...
#声明日志的输出位置在控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#定义日志的打印格式  %t 表示线程名称  %5p表示输出日志级别   %n表示换行
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatis-config.xml

<settings>
    <!-- 开启日志 -->
    <setting name="logImpl" value="LOG4J"/>
</settings>

Restart the test, success

"C:\Program Files\Java\jdk-17.0.2\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\IDE\IntelliJ IDEA 2021.2\lib\idea_rt.jar=1831:C:\IDE\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\IDE\IntelliJ IDEA 2021.2\lib\idea_rt.jar;C:\IDE\IntelliJ IDEA 2021.2\plugins\junit\lib\junit5-rt.jar;C:\IDE\IntelliJ IDEA 2021.2\plugins\junit\lib\junit-rt.jar;D:\ProgrammeDisk\java-workspace\Mybatis\target\classes;D:\SoftwareDownloading\Maven\mvn_repo\org\mybatis\mybatis\3.5.10\mybatis-3.5.10.jar;D:\SoftwareDownloading\Maven\mvn_repo\mysql\mysql-connector-java\8.0.30\mysql-connector-java-8.0.30.jar;D:\SoftwareDownloading\Maven\mvn_repo\com\google\protobuf\protobuf-java\3.19.4\protobuf-java-3.19.4.jar;D:\SoftwareDownloading\Maven\mvn_repo\junit\junit\4.13.2\junit-4.13.2.jar;D:\SoftwareDownloading\Maven\mvn_repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\SoftwareDownloading\Maven\mvn_repo\org\apache\logging\log4j\log4j-core\2.19.0\log4j-core-2.19.0.jar;D:\SoftwareDownloading\Maven\mvn_repo\org\apache\logging\log4j\log4j-api\2.19.0\log4j-api-2.19.0.jar;D:\SoftwareDownloading\Maven\mvn_repo\log4j\log4j\1.2.17\log4j-1.2.17.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jason.test.UserTest
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Listing file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Reader entry: x   = H
DEBUG [main] - Checking to see if class com.jason.pojo.User matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1198817209.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - ==>  Preparing: insert into tb_user(name,sex,age,phone) values (?,?,?,?,?)
DEBUG [main] - ==> Parameters: ooo(String), ooo(String), ooo(String), ooo(String), ooo(String)
DEBUG [main] - <==    Updates: 1
成功插入 1 条数据
DEBUG [main] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - Returned connection 1198817209 to pool.
DEBUG [Finalizer] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Listing file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Reader entry: x   = H
DEBUG [main] - Checking to see if class com.jason.pojo.User matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 470132045.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c05a54d]
DEBUG [main] - ==>  Preparing:  select * from tb_user where id=?
DEBUG [main] - ==> Parameters: 41(Integer)
DEBUG [main] - <==      Total: 1
User{
    
    id=41, name='jason', sex='男', age='00', phone='12345678901'}
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c05a54d]
DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c05a54d]
DEBUG [main] - Returned connection 470132045 to pool.

Process finished with exit code 0

References:

Guess you like

Origin blog.csdn.net/m0_51269961/article/details/126990304