使用log4j2打印mybatis的sql执行日志

【1】maven配置jar包依赖, 如下:

	
		
		
		    org.apache.logging.log4j
		    log4j-api
		    2.11.1
		
		
		    org.apache.logging.log4j
		    log4j-core
		    2.11.1
		
		
		    org.apache.logging.log4j
		    log4j-web
		    2.11.1
		
		
		    org.apache.logging.log4j
		    log4j-slf4j-impl
		    2.11.1
		
	

【2】配置log4j2.xml 文件,内容如下:



  
    
      
    
    
  
  
  
    
      
    
    
      
    
  

Logger 标签 配置了需要打印日志的mapper(Mapper类或mapper XML 文件所在包)

Logger标签中的 level属性取值为 DEBUG 或者 TRACE,(取其一)

【3】mybatis配置文件 mybatis-config.xml 中设置日志的工具类, 如下:




	
	
	
	
  	    
  		
  		
		
  	
  	
  	
	  
	
	
  	
  		
  	
  	
	
		
		
			
			
				
				
				
				
			
		
	
	
	
		
	
  

【4】日志打印效果:

20:35:28.742 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:181) ==>  Preparing: SELECT dept_id AS DEPT_ID , dept_name AS DEPT_NAME FROM dept_tbl WHERE rcrd_id IN ( ? , ? , ? ) 
20:35:28.786 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:181) ==> Parameters: 1(String), 2(String), 3(String)
20:35:28.807 [main] DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:181) <==      Total: 3

打印sql为:

 
  

References are as follows.

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

https://blog.csdn.net/honghailiang888/article/details/52594014 

 

你可能感兴趣的:(使用log4j2打印mybatis的sql执行日志)