mybatis sql 打印

mybatis 怎么打印执行的sql?

查看了官方文档,跟踪了mybatis源码发现有两种方式:

方法一、在mybatis配置的xml中标签下添加


	

sql打印成控制台

==>  Preparing: select FIELD_VALUE,CATE_CODE from GOODS_CATEGORY_DICT_MAPPING 
==> Parameters: 
<==    Columns: FIELD_VALUE, CATE_CODE
<==        Row: 22001, V1030011001001
//.....
<==      Total: 27

方法二、在log4j/logback/logck4j2中添加我们项目包路径mapper为debug


        

sql通过DEBUG打印效果在控制台

2018-03-29 15:29:44.757 DEBUG c.h.s.g.b.m.G.selectAll [main] -==>  Preparing: select FIELD_VALUE,CATE_CODE from GOODS_CATEGORY_DICT_MAPPING 
2018-03-29 15:29:44.774 DEBUG c.h.s.g.b.m.G.selectAll [main] -==> Parameters: 
2018-03-29 15:29:44.792 DEBUG c.h.s.g.b.m.G.selectAll [main] -<==      Total: 27


mybatis日志打印源码:org.apache.ibatis.logging.jdbc.ConnectionLogger

 public Object invoke(Object proxy, Method method, Object[] params)
      throws Throwable {
    try {
      //....... 
      if ("prepareStatement".equals(method.getName())) {
        if (isDebugEnabled()) {//是否打印日志判断
          debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
        }        
        PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
        stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
        return stmt;
      } //.........
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }


你可能感兴趣的:(mybatis,mybatis,打印sql)