如果一个数据库操作,出现了异常,我们需要排错,日志就是最好的助手!

【MyBatis笔记】日志_java

在Mybatis中具体使用哪一个日志实现,在设置中设定。

LOG4J:

●log4j可以控制日志信息输送的目的地是控制台、文件、GUI组件

●可以控制每一条日志的输出格式

●通过定义每一条日志信息的级别,能够更加细致地控制日志的生成过程

●通过一个配置文件来灵活地进行配置,而不需要修改应用的代码

导入LOG4J依赖:

<dependencies>
    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>1.2.17</version>
    </dependency>
</dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

log4j配置文件:log4j.properties

### set log levels ###
log4j.rootLogger = DEBUG,console,file

### 输出到控制台 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold = DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%c]-%m%n

### 输出到日志文件 ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/kuang.log
log4j.appender.file.MaxFileSize=10mb 
log4j.appender.file.Threshold=DEBUG 
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

# 日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

修改日志模式 

【MyBatis笔记】日志_mybatis_02

 

STDOUT_LOGGING:标准日志输出

【MyBatis笔记】日志_java_03

运行成功时控制台的输出 

【MyBatis笔记】日志_apache_04

 

//使用stdout日志工厂实现
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.

Class not found: org.jboss.vfs.VFS
JBoss 6 VFS API is not available in this environment.
Class not found: org.jboss.vfs.VirtualFile
VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
Using VFS adapter org.apache.ibatis.io.DefaultVFS
//处理一些文件
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/pojo
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/pojo
Reader entry: User.class
Listing file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/pojo
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/pojo/User.class
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/pojo/User.class
Reader entry: ����   4 V
Checking to see if class com.kuang.pojo.User matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/test-classes/com/kuang/dao
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/test-classes/com/kuang/dao
Reader entry: UserMapperTest.class
Listing file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/test-classes/com/kuang/dao
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/test-classes/com/kuang/dao/UserMapperTest.class
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/test-classes/com/kuang/dao/UserMapperTest.class
Reader entry: ����   4 6
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao
Reader entry: UserMapper.class
Reader entry: UserMapper.xml
Listing file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao/UserMapper.class
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao/UserMapper.class
Reader entry: ����   4 	  com/kuang/dao/UserMapper  java/lang/Object getUserById (I)Lcom/kuang/pojo/User; 
Find JAR URL: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao/UserMapper.xml
Not a JAR: file:/C:/Users/%e6%9a%ae%e8%89%b2%e5%b9%b4%e5%8d%8e/IdeaProjects/cn.li/Mybatis-study/mybatis-03/target/classes/com/kuang/dao/UserMapper.xml
Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
Checking to see if class com.kuang.dao.UserMapperTest matches criteria [is assignable to Object]
Checking to see if class com.kuang.dao.UserMapper matches criteria [is assignable to Object]




//正式连接数据库
//打开JDBC连接
Opening JDBC Connection
Sat Apr 23 14:27:04 CST 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

//创建连接对象
Created connection 884603232.
//自动提交为false
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@34b9f960]
//sql语句,参数
==>  Preparing: select * from mybatis.user where id=? 
==> Parameters: 2(Integer)
<==    Columns: id, name, pwd
<==        Row: 2, 张三, 123456
<==      Total: 1
//结果
User{id=2, name='张三', password='123456'}
//关闭连接对象
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@57a3e26a]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@57a3e26a]
Returned connection 1470358122 to pool.

进程已结束,退出代码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.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.