实践-mybatis+h2+logback单元测试

mybatis小试牛刀 在此基础上添加log日志。

0x01 测试控制台的显示内容

1
2
3
User(id=1, name=TOM, age=12, gender=0)
User(id=2, name=JON, age=13, gender=1)
User(id=3, name=King, age=11, gender=0)

0x02 增加日志输出

根据官方文档显示,支持的log有很多实现,这里使用Slf4j + Logback 多组合。

  • 根据之前的项目,这里偷懒重新创建一个项目并把之前的代码都复制过来。
  • 添加maven依赖。
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
 <properties>
<mybatis.version>3.5.0</mybatis.version>
<junit.version>4.12</junit.version>
<lmbok.version>1.18.4</lmbok.version>
<mysql.version>5.1.47</mysql.version>
<h2.version>1.4.197</h2.version>
<!-- 新增内容 -->
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
<!-- pom文件需要指定打包编码集,[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
  • 添加logback配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>z201.github.cn %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
  • 再次运行单元测试。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
z201.github.cn 15:08:30.046 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
z201.github.cn 15:08:30.049 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
z201.github.cn 15:08:30.076 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
z201.github.cn 15:08:30.076 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
z201.github.cn 15:08:30.076 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
z201.github.cn 15:08:30.076 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
z201.github.cn 15:08:30.155 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
z201.github.cn 15:08:30.356 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 1839168128.
z201.github.cn 15:08:30.356 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [conn0: url=jdbc:h2:mem:mybatis_learning_01_db user=ROOT]
z201.github.cn 15:08:30.358 [main] DEBUG c.z201.mybatis.learning.UserDao.list - ==> Preparing: SELECT * FROM `user`
z201.github.cn 15:08:30.384 [main] DEBUG c.z201.mybatis.learning.UserDao.list - ==> Parameters:
z201.github.cn 15:08:30.411 [main] DEBUG c.z201.mybatis.learning.UserDao.list - <== Total: 3
z201.github.cn 15:08:30.414 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [conn0: url=jdbc:h2:mem:mybatis_learning_01_db user=ROOT]
z201.github.cn 15:08:30.415 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [conn0: url=jdbc:h2:mem:mybatis_learning_01_db user=ROOT]
z201.github.cn 15:08:30.415 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 1839168128 to pool.
User(id=1, name=TOM, age=12, gender=0)
User(id=2, name=JON, age=13, gender=1)
User(id=3, name=King, age=11, gender=0)
z201.github.cn 15:08:30.416 [main] DEBUG cn.z201.mybatis.test.MybatisTest01 - end...