spring+mybatis+log4j 输出SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 1、在mybatis-config.xml配置中添加setting配置参数,会打印SQL执行结果<?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> </configuration>2、在spring-mybatis配置文件中添加配置参数<bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref = "dataSource" /> <property name= "configLocation" value= "classpath:mybatis-config.xml" ></property> <property name= "mapperLocations" value= "classpath:xxx/xxx/xxx/xxx/*.xml" ></property> </bean>备注: 之前通过log4j的配置文件方式来实现,没有成功!!!项目环境:ssm+shiro第二种方式(通过拦截器的方式)1、在sqlSessionFactory 配置参数中,添加参数配置:<property name= "plugins" > <list> <bean class = "xxx.MybatisInterceptor" ></bean> </list> </property>2、mybatis sql拦截器类package xxx; import java.text.DateFormat; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Properties; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.type.TypeHandlerRegistry; import org.apache.log4j.Logger; /** * @author Tim * * 2019年4月24日 * * mybatis sql 拦截器 */ @Intercepts({ @Signature(type = Executor. class , method = "update" , args = { MappedStatement. class , Object. class }), @Signature(type = Executor. class , method = "query" , args = { MappedStatement. class , Object. class , RowBounds. class , ResultHandler. class }) }) public class MybatisInterceptor implements Interceptor { private static final Logger log = Logger.getLogger(MybatisInterceptor. class ); @Override public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = null ; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); Object returnVal = invocation.proceed(); //获取sql语句 String sql = getSql(configuration, boundSql); log.info( "#####拦截器获取SQL#### " +sql); //统计SQL执行时间 Object target = invocation.getTarget(); Object result = null; if (target instanceof Executor) { long start = System.currentTimeMillis(); Method method = invocation.getMethod(); /**执行方法*/ result = invocation.proceed(); long end = System.currentTimeMillis(); logger.info( "[" + method.getName() + "] 耗时 [" + (end - start) + "] ms" ); } return returnVal; } @Override public Object plugin(Object target) { return Plugin.wrap(target, this ); } @Override public void setProperties(Properties arg0) { } /** * 获取SQL * @param configuration * @param boundSql * @return */ private String getSql(Configuration configuration, BoundSql boundSql) { Object parameterObject = boundSql.getParameterObject(); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); String sql = boundSql.getSql().replaceAll( "[\\s]+" , " " ); if (parameterObject == null || parameterMappings.size() == 0) { return sql; } TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { sql = sql.replaceFirst( "\\?" , getParameterValue(parameterObject)); } else { MetaObject metaObject = configuration.newMetaObject(parameterObject); for (ParameterMapping parameterMapping : parameterMappings) { String propertyName = parameterMapping.getProperty(); if (metaObject.hasGetter(propertyName)) { Object obj = metaObject.getValue(propertyName); sql = sql.replaceFirst( "\\?" , getParameterValue(obj)); } else if (boundSql.hasAdditionalParameter(propertyName)) { Object obj = boundSql.getAdditionalParameter(propertyName); sql = sql.replaceFirst( "\\?" , getParameterValue(obj)); } } } return sql; } private String getParameterValue(Object obj) { String value = null ; if (obj instanceof String) { value = "'" + obj.toString() + "'" ; } else if (obj instanceof Date) { DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); value = "'" + formatter.format(obj) + "'" ; } else { if (obj != null ) { value = obj.toString(); } else { value = "" ; } } return value; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?