Mybatis 通过使用内置的日志工厂提供日志功能。内置日志工厂将会把日志工作委托给下面的实现之一:
- SLF4J
- Apache Commons Logging
- Log4j 2
- Log4j
- JDK logging
MyBatis 内置日志工厂会基于运行时检测信息选择日志委托实现。它会(按上面罗列的顺序)使用第一个查找到的实现。当没有找到这些实现时,将会禁用日志功能。
不少应用服务器(如 Tomcat 和 WebShpere)的类路径中已经包含 Commons Logging。注意,在这种配置环境下,MyBatis 会把 Commons Logging 作为日志工具。这就意味着在诸如 WebSphere 的环境中,由于提供了 Commons Logging 的私有实现,你的 Log4J 配置将被忽略。这个时候你就会感觉很郁闷:看起来 MyBatis 将你的 Log4J 配置忽略掉了(其实是因为在这种配置环境下,MyBatis 使用了 Commons Logging 作为日志实现)。如果你的应用部署在一个类路径已经包含 Commons Logging 的环境中,而你又想使用其它日志实现,你可以通过在 MyBatis 配置文件 mybatis-config.xml 里面添加一项 setting 来选择其它日志实现。
<configuration>
<settings>
...
<setting name="logImpl" value="LOG4J"/>
...
</settings>
</configuration>
可选的值有:
- SLF4J、
- LOG4J、
- LOG4J2、
- JDK_LOGGING、
- COMMONS_LOGGING、
- STDOUT_LOGGING、
- NO_LOGGING,
或者是实现了 org.apache.ibatis.logging.Log
接口,且构造方法以字符串为参数的类完全限定名。
实例
(1)在mybatis-config.xml文件中插入:
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
【注】:在<configuration>中,元素必须按照以下顺序出现在configuration中 (properties, settings, typeAliases, typeHandlers, objectFactory, objectWrapperFactory, reflectorFactory, plugins, environments, databaseIdProvider, mappers)。
(2)执行【Mybatis】动态SQL 实例中的getVisibleBlogListByLabelAndTitle():
@Test
public void getVisibleBlogListByLabelAndTitle(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("label", "Spring");
map.put("title", "%AOP%");
List<Blog> blogs = mapper.getBlogListByLabelAndTitle(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
输出结果为:
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
Created connection 594651850.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2371aaca]
==> Preparing: select * from blog where label = ? AND title like ?
==> Parameters: Spring(String), %AOP%(String)
<== Columns: id, title, author, _time, read_count, label, _like, visible
<== Row: 117374430, 【Spring】AOP(一)使用Spring的API接口, 牧心., 2021-05-30 19:00:46, 228, Spring, 1, 1
<== Row: 118054659, 【Spring】AOP(二)自定义来实现AOP, 牧心., 2021-06-19 15:50:23, 192, Spring, 1, 1
<== Row: 118058442, 【Spring】AOP(三)注解实现AOP, 牧心., 2021-06-19 20:06:54, 203, Spring, 1, 1
<== Total: 3
Blog{id=117374430, title='【Spring】AOP(一)使用Spring的API接口', author='牧心.', time='null', read_count=228, label='Spring', like=0, visible=1}
Blog{id=118054659, title='【Spring】AOP(二)自定义来实现AOP', author='牧心.', time='null', read_count=192, label='Spring', like=0, visible=1}
Blog{id=118058442, title='【Spring】AOP(三)注解实现AOP', author='牧心.', time='null', read_count=203, label='Spring', like=0, visible=1}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2371aaca]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2371aaca]
Returned connection 594651850 to pool.
更多推荐
【Mybatis】日志
发布评论