网上说mybatis的早前版本配置打印sql还比较简单,在3.0.6之后配置方式修改了。
现在的spring-mybatis.xml配置如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:conf/mybatis-config.xml"></property> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/cyber/vip/dao/*.xml"></property> </bean>
mybatis-config.xml:
<?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="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- mapper已经在spring-mybatis.xml中的sqlSessionFactory配置,这里不再需要配置 --> <!-- <mappers> --> <!-- <mapper resource="com/a/b/c/dao/BusinessInfoDaoMapper.xml" /> --> <!-- </mappers> --> </configuration>
不错,打印SQL只需要加一个setting就可以了。
mybatis的日志打印方式比较多,SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING,可以根据自己的需要进行配置
log4j.properties
log4j.rootLogger=INFO,ERROR,stdout , R
log4j.addivity.org.apache=false
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO,ERROR
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Encoding=utf-8
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/privatefund/privatefundlog.txt
log4j.appender.R.DatePattern=yyyy-MM-dd'.log'
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[FH_sys] %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n