Java -- MyBatis学习笔记3、输出日志
1、为什么要配置日志?
在使用MyBatis进行开发时候、每次对数据库操作后在控制台只有结果而没有详细信息。比如:执行添加数据后返回受影响行数、执行查询语句后返回一个对象或者集合,但是、都不知道是执行的那个SQL语句的过程、期间都发生了什么、语句中都有什么值。所以、给MyBatis配置日志,可以输出详细信息,如果结果有错误、可以很快定位到某一个地方。
1.1、在MyBatis中的主配置文件中加入日志配置
- 如下:
<!-- settings:控制mybatis全局行为、告诉mybatis怎么做 -->
<settings>
<!--logImpl:输出日志
STDOUT_LOGGING:输出到控制台
-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
- 控制台:
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Sat May 08 12:16:36 CST 2021 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 84739718.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@50d0686]
==> Preparing: select * from UserInfo
==> Parameters:
<== Columns: id, Name, Age
<== Row: 2, 哈哈, 18
<== Row: 3, 王五, 16
<== Total: 2
com.rg.entity.UserInfo@396e2f39
com.rg.entity.UserInfo@a74868d
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@50d0686]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@50d0686]
Returned connection 84739718 to pool.
可以看到、控制台输出的本次结果的执行详细信息、输出日志使用的是StdOutImpl、还有创建连接、关闭连接、SQL语句、行数、最后将对象放回到连接池中等等详细信息。