手记

(二)mybatis打印sql语句

(二)打印sql语句

标签(空格分隔): mybatis


只在控制台输出
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
     <settings>
        <setting name="lazyLoadingEnabled" value="false" />
        <setting name="cacheEnabled" value="true" />
        <setting name="logImpl" value="STDOUT_LOGGING"/>
     </settings>
</configuration>

mybatis的日志打印方式比较多,SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING,可以根据自己的需要进行配置

这种打印方式只在控制太输出,不在日志文件里输出

debug级别输出到日志文件中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
     <settings>
        <setting name="lazyLoadingEnabled" value="false" />
        <setting name="cacheEnabled" value="true" />
        <setting name="logImpl" value="LOG4J"/>
     </settings>
</configuration>
#debug level
log4j.rootLogger=debug,stdout,debug,error

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

# debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.Threshold =debug 
log4j.appender.debug.Append = true
log4j.appender.debug.DatePattern='.'yyyy-MM-dd
log4j.appender.debug.File=log/huiyuandingdan_debuginfo.log
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{yyyyMMdd HH:mm} | %m%n

log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.Threshold = ERROR 
log4j.appender.error.Append = true
log4j.appender.error.DatePattern='.'yyyy-MM-dd
log4j.appender.error.File=log/huiyuandingdan_errorinfo.log
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{yyyyMMdd HH:mm} | %m%n

1.这种方式会记录到日志文件中,但是需要注意的是sql语句打印的级别的DEBUG级别,所以日志文件的级别也许配置到DEBUG级别log4j.rootLogger=debug
2.<setting name="logImpl" value="LOG4J"/>似乎是必须要配置的
3.如果需要打印出sql语句,还不想设置debug级别,可以配置拦截器

2人推荐
随时随地看视频
慕课网APP