Mybatis怎么能看是否执行了sql语句

项目需要学习mybatis中,本来mybatis也不是什么新技术,无奈之前没接触过。

验证缓存机制时,需要能看到是否sql被执行了。这就需要增加日志的打印

 

配置如下

在pom中增加如下依赖:

<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-beta9</version>
</dependency>

 

log4j.properties 如下配置 log4j.rootLogger=debug,stdout  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

 

增加一个 log4jdbc.log4j2.prpperties 内容如下:

dbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.debug.stack.prefix=
log4jdbc.sqltiming.warn.threshold=
log4jdbc.sqltiming.error.threshold=
log4jdbc.dump.booleanastruefalse=
log4jdbc.dump.sql.maxlinelength=
log4jdbc.dump.fulldebugstacktrace=
log4jdbc.statement.warn=
log4jdbc.dump.sql.select=
log4jdbc.dump.sql.insert=
log4jdbc.dump.sql.update=
log4jdbc.dump.sql.delete=
log4jdbc.dump.sql.create=
log4jdbc.dump.sql.addsemicolon=
log4jdbc.auto.load.popular.drivers=
log4jdbc.trim.sql=
log4jdbc.trim.sql.extrablanklines=
log4jdbc.suppress.generated.keys.exception=
log4jdbc.drivers=

最后在mybatis的全局配置文件中

<dataSource type="POOLED">
<property name="driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
<property name="url" value="jdbc:log4jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>

 

logback写法

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <!--<define name="SN_SERVER_NAME" class="com.suning.b2c.util.ip.ServerIPPropertyDefiner" />-->
    <!-- 控制台输出日志 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</pattern>
        </layout>
    </appender>

    <!-- 文件输出日志 (文件大小策略进行文件输出,每小时产生一个日志文件给异常监控平台进行分析) -->
    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>$[log.path]/rdrs_error.log</File>
        <!-- 日志文件rolling策略 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>$[log.path]/rdrs_error_%d{yyyy-MM-dd_HH}.log</FileNamePattern>
            <maxHistory>24</maxHistory>
        </rollingPolicy>

        <!-- 日志输出格式 -->
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</Pattern>
        </layout>
    </appender>
    
    <logger name="rsf.statistic" level="ERROR" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
    <logger name="com.suning.windq.jms" level="OFF" additivity="false">
        
    </logger>
    
       <logger name="com.suning.rdrs.admin.dao" level="DEBUG"/>
    <!--<logger name="org.springframework" level="debug" /> -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="info" />
    </root>
</configuration>

 

posted on 2017-06-22 12:48  MaXianZhe  阅读(5284)  评论(0编辑  收藏  举报

导航