mybatis3.2.8配置log4j2打印sql

来源:互联网 发布:淘宝无线端怎么推广 编辑:程序博客网 时间:2024/06/16 14:06

一、环境

mybatis版本3.2.8,log4j2版本为2.2,采用spring集成mybatis


二、gradle中配置mybatis和log4j2

//log4j2    def log4j_version = "2.2";    compile "org.apache.logging.log4j:log4j-api:$log4j_version"compile "org.apache.logging.log4j:log4j-core:$log4j_version"compile "org.apache.logging.log4j:log4j-web:$log4j_version"compile "org.apache.logging.log4j:log4j-slf4j-impl:$log4j_version"//mybatis    compile "org.mybatis:mybatis:3.2.8"     compile "org.mybatis:mybatis-spring:1.2.2" 


三、log4j2.xml中配置

<?xml version="1.0" encoding="UTF-8"?><Configuration status="WARN">  <Appenders>    <Console name="Console" target="SYSTEM_OUT">      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %l %msg%n"/>    </Console>      </Appenders>    <Loggers>    <Root level="error">      <AppenderRef ref="Console"/>    </Root>    <Logger name="com.mango.mapper" level="TRACE" additivity="false">       //为dao mapper所在的包,level为TRACE      <AppenderRef ref="Console"/>    </Logger>  </Loggers></Configuration>


四、Spring配置文件中配置

<!-- sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:sqlMapConfig.xml" /></bean>
<!-- sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:sqlMapConfig.xml" /></bean><!-- mapper扫描器 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 --><property name="basePackage" value="com.mango.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />     //采用mapper文件的方式</bean>


五、mybatis配置文件指定log4j2为日志输出

<settings><setting name="logImpl" value="LOG4J2" /></settings>


六、打印日志如下

11:03:07.717 [http-apr-8090-exec-104] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139) ==>  Preparing: SELECT * FROM product 11:03:07.738 [http-apr-8090-exec-104] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139) ==> Parameters: 11:03:07.749 [http-apr-8090-exec-104] TRACE org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:145) <==    Columns: product_id, product_name, unit_price, quantity, unit, picture11:03:07.750 [http-apr-8090-exec-104] TRACE org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:145) <==        Row: t8rf1646, ETeam, 100.00, 100, 件, 11:03:07.752 [http-apr-8090-exec-104] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139) <==      Total: 111:06:26.553 [http-apr-8090-exec-114] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139) ==>  Preparing: SELECT * FROM product where product_id=? 11:06:26.554 [http-apr-8090-exec-114] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139) ==> Parameters: t8rf1646(String)11:06:26.557 [http-apr-8090-exec-114] TRACE org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:145) <==    Columns: product_id, product_name, unit_price, quantity, unit, picture11:06:26.557 [http-apr-8090-exec-114] TRACE org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:145) <==        Row: t8rf1646, ETeam, 100.00, 100, 件, 11:06:26.558 [http-apr-8090-exec-114] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:139) <==      Total: 1

参考文档:

http://www.mybatis.org/mybatis-3/zh/logging.html

http://www.cnblogs.com/yjmyzz/p/4033729.html



0 0
原创粉丝点击