• Maven 换源 & Mybatis 开启 Log4j 日志框架


    本文主要讲解如何在 idea 开发环境下更换 maven 国内镜像源与 Mybatis 开启 Log4j 日志框架相关操作

    Maven 换源

    国内镜像地址 : https://developer.aliyun.com/mvn/guide

    在 maven 中找到 conf 然后配置 settings.xml 即可

    不要在 idea 默认的 maven 插件目录下修改配置,否则下次更新会重新覆盖,失效!!!

    其他不再赘述,请读者自行查询

    这里主要叙述下 idea 开发环境中 针对 maven 的配置项,开始前首先区分两个设置

    • 项目设置,只针对当前项目生效;
    • 开发环境默认设置,每次建立项目都从这里读取相关配置;

    由于我们在本地配置好了 maven 仓储库,并不想使用默认指定的位置(一般都在 c 盘符下),所以需要在 idea 中更新下

    1.项目设置

    进入设置,找到 maven

    2.默认设置

    如果你不想每次新建项目都需要重新配置,最好执行以下操作

    Mybatis 开启 Log4j 日志框架

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

    网络现在大多的参考教程配置项仍为 log4j 1.2.17 版本,单此版本的安全漏洞在 idea 中禁用,若读者不能自行找到更高或最新版本,请参考以下教程

    maven 中央仓库搜索 log4j 结果

    发现已经迁移位置,来到新位置

    可以看到已经针对漏洞进行了修复

    点击合适版本并选择对应引入方式,(这里以 maven 为例)加入到 pom 文件下即可

    pom.xml

    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>2.19.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    mybatis-config.xml

    <settings>
        
        <setting name="logImpl" value="LOG4J"/>
    settings>
    
    • 1
    • 2
    • 3
    • 4

    重新启动测试,成功

    "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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    参考资料:

  • 相关阅读:
    2023年09月青少年软件编程(C语言)等级考试试卷(四级)
    Databricks 收购 Tabular 的意义:数据开放框架的胜利
    BeanFactory版本的快速入门
    数值分析思考题(钟尓杰版)参考解答——第四章
    Spring Bean的作用域
    树莓派66/100 - 用Pico W访问互联网
    [坐标系转换]车体坐标系 转 像素坐标系
    使用C语言进行问题分析
    [洛谷月赛]终于结束的起点
    [vulnhub] Tr0ll: 2
  • 原文地址:https://blog.csdn.net/m0_51269961/article/details/126990304