1:从maven依赖仓库找到log4j依赖包 maven仓库地址:http://mvnrepository.com/artifact/ojdbc/ojdbc/10.2.0.4.0
2:在maven项目下 resource下添加log4j.xml
这里提示找不到dtd文件,自行百度解决,大致是项目下没有.dtd文件 找到 eclipse版本自带的STS又对不上版本号,所以没有去理会了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${user.home}/foss-framework.log" />
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="100" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
</layout>
</appender>
<!-- 下面是打印mybatis语句的配置 -->
<logger name="com.gradPro.dao" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="com.ibatis" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.ResultSet" additivity="true">
<level value="DEBUG" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
注意:<logger neme="自己接口方法对应的包"> 且不要把dao和xml分开放 放在同一个包下
mybtis-config.xml 开启sql打印配置
<!-- 配置全局屬性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库的自增主键值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列别名替换列名 -->
<setting name="useColumnLabel" value="true" />
<!-- 开启驼峰命名规范 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- log4j打印sql配置 -->
<setting name="lazyLoadingEnabled" value="false" />
<setting name="logImpl" value="LOG4J" />
</settings>
用类的CLASS对象做参数
点击事件
控制台打印成功