学习mybatis的时候遇到打印不出sql 的问题,在这里做个总结:
1:首先log4j.properties这样配置;
log4j.rootLogger=DEBUG,console,R log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.threshold=debug log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%M(%L) -%m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.Append=true log4j.appender.R.File=D:/springmvc.log log4j.appender.R.Threshold=warn log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%M(%L) -%m%n log4j.appender.R.MaxFileSize=20MB #避免debug等级日志输入 log4j.logger.org = info
2:检查自己导入的jar包,看是否存在这三个包 log4j slf4j-api slf4j-log4j12
如果这三个包都存在或只存在log4j,请跳过步骤3
3:项目对slf4j-api又不想导入slf4j-log12,那么作如下配置:
spring配置: <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/chencheng/mapping/*.xml"/> <!-- 注册mybatis配置文件 --!> <property name="configLocation" value="classpath:mybatis-config.xml"/> </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="LOG4J"/> //使用log4j </settings> </configuration>
完成上面的配置,我们应该能看到控制台打印的sql了。如图:
时间: 2024-10-05 11:57:45