MyBatis日志(STDOUT_LOGGING,LOG4J)

logImpl
  • SLF4J
  • LOG4J
  • LOG4J2
  • JDK_LOGGING
  • COMMONS_LOGGING
  • STDOUT_LOGGING
  • NO_LOGGING
STDOUT_LOGGING标准日志输出

核心配置文件

<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

测试代码

// 获取SqlSession对象
try(SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
    // 执行sql
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = userMapper.selectUserList();
}

运行结果

Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Created connection 330084561.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@13acb0d1]
==>  Preparing: select id, name, pwd from user;
==> Parameters: 
<==    Columns: id, name, pwd
<==        Row: 1, 张三, 123456
<==        Row: 2, 李四, 123456
<==        Row: 3, 老王, 123456
<==        Row: 24, 熊二, 123456
<==      Total: 4
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@13acb0d1]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@13acb0d1]
Returned connection 330084561 to pool.
什么是log4j
  • log4j是Apache的一个开源项目
  • 通过log4j可以控制日志输出的目的地是控制台,文件,GUI组件等
  • 可以控制每一条日志的输出格式
  • 可以定义日志信息的级别
  • 可以通过配置文件进行灵活的配置,不需要修改代码
添加log4j依赖
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
创建log4j配置文件

log4j.properties

# stdout和R是取了两个别名,在下边的配置中用到,也可以用其他名称,stdout配置的是标准输出,R是文件
log4j.rootLogger=debug, stdout, R

# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 格式化输出文件名和行号
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
# 文件路径
log4j.appender.R.File=target/example.log
# 最大文件大小
log4j.appender.R.MaxFileSize=100KB
# 最大文件个数
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
# 格式化输出文件名和行号
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
mybatis核心配置文件
<settings>
    <setting name="logImpl" value="LOG4J"/>
</settings>

测试代码

private static Logger logger = Logger.getLogger(UserMapperTest.class);

@Test
public void test() {
    // 获取SqlSession对象
    try(SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
        // 执行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.selectUserList();

        Iterator<User> userIterator = userList.iterator();
        while (userIterator.hasNext()) {
            logger.debug(userIterator.next());
        }
    }
}

运行结果

DEBUG [main] (JdbcTransaction.java:137) - Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
DEBUG [main] (PooledDataSource.java:434) - Created connection 222511810.
DEBUG [main] (JdbcTransaction.java:101) - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@d4342c2]
DEBUG [main] (BaseJdbcLogger.java:137) - ==>  Preparing: select id, name, pwd from user;
DEBUG [main] (BaseJdbcLogger.java:137) - ==> Parameters: 
DEBUG [main] (BaseJdbcLogger.java:137) - <==      Total: 4
DEBUG [main] (UserMapperTest.java:26) - User(id=1, username=张三, password=123456)
DEBUG [main] (UserMapperTest.java:26) - User(id=2, username=李四, password=123456)
DEBUG [main] (UserMapperTest.java:26) - User(id=3, username=老王, password=123456)
DEBUG [main] (UserMapperTest.java:26) - User(id=24, username=熊二, password=123456)
DEBUG [main] (JdbcTransaction.java:123) - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@d4342c2]
DEBUG [main] (JdbcTransaction.java:91) - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@d4342c2]
DEBUG [main] (PooledDataSource.java:391) - Returned connection 222511810 to pool.
上一篇:scala读取mysql数据


下一篇:SSM框架底层搭建