尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

mybatis打印日志

mybatis打印日志 方案一直接安装插件mybatis插件MyBatisCodeHelperPro方案二使用代码在MybatisPlusConfig.java中嵌入以下代码/** * 自定义 优雅打印 SQL 插件 */ Bean public ConfigurationCustomizer configurationCustomizer(){ return configuration - { configuration.addInterceptor(new SqlInterceptor()); }; }在MybatisPlusConfig.java的同级目录中增加以下类SqlInterceptor.javapackage com.cnooc.pm.project.config; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.cache.CacheKey; 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.*; 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.springframework.util.ObjectUtils; import java.text.DateFormat; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Properties; import java.util.regex.Matcher; Intercepts({ Signature(type Executor.class, method query, args {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class}), Signature(type Executor.class, method query, args {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class, CacheKey.class,BoundSql.class}), Signature(type Executor.class, method update, args {MappedStatement.class,Object.class}), }) Slf4j public class SqlInterceptor implements Interceptor { Override public Object intercept(Invocation invocation) throws Throwable { long startTime System.currentTimeMillis(); Object proceed invocation.proceed(); long endTime System.currentTimeMillis(); String printSql null; try { //通过generateSql方法拿到最终生成的SQL printSql generateSql(invocation); } catch (Exception e) { log.error(获取sql异常, e); } finally { long costTime endTime - startTime; log.info(\n 执行SQL耗时{}ms \n 执行SQL{}, costTime, printSql); } return proceed; } private String generateSql(Invocation invocation) { //获取到BoundSql以及Configuration对象 MappedStatement statement (MappedStatement) invocation.getArgs()[0]; Object parameter null; if (invocation.getArgs().length 1) { parameter invocation.getArgs()[1]; } // Configuration 对象保存了 Mybatis 架构运行的所有配置信息 Configuration configuration statement.getConfiguration(); // BoundSql 对象存储了一条具体的 SQL 语句以及相关的参数信息 BoundSql boundSql statement.getBoundSql(parameter); //获取参数信息 Object parameterObject boundSql.getParameterObject(); //获取参数的映射 ListParameterMapping params boundSql.getParameterMappings(); //获取到执行的sql String sql boundSql.getSql(); //多个空格使用一个空格替代 sql.replaceAll([\\s], ); if (!ObjectUtils.isEmpty(parameterObject) !ObjectUtils.isEmpty(params)) { //TypeHandlerRegistry 是 Mybatis用来管理 TypeHandler 的注册器TypeHandler 用于在 Java 类型和 JDBC 类型之间进行转换 TypeHandlerRegistry typeHandlerRegistry configuration.getTypeHandlerRegistry(); //如果参数对象的类型有对应的 TypeHandler ,则使用 TypeHandler 进行处理 int num0; if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { sql sql.replaceFirst(\\?, Matcher.quoteReplacement(getParameterValue(parameterObject))); } else { //否则这个处理参数映射关系 for (ParameterMapping param : params) { //获取参数的属性名 String propertyName param.getProperty(); MetaObject metaObject configuration.newMetaObject(parameterObject); // 检查对象中是否存在该属性的 Getter 方法如果存在提取出来进行替换 if (metaObject.hasGetter(propertyName)) { Object obj metaObject.getValue(propertyName); sql sql.replaceFirst(\\?, Matcher.quoteReplacement(getParameterValue(obj))); //检查 BoundSql 中是否存在附加的参数附加参数可能是在动态 SQL 处理中形成的有的话进行替换 } else if (boundSql.hasAdditionalParameter(propertyName)) { Object obj boundSql.getAdditionalParameter(propertyName); sql sql.replaceFirst(\\?, Matcher.quoteReplacement(getParameterValue(obj))); } else { //如果都没有说明 SQL 匹配不上带上“缺失”方便找问题 sql sql.replaceFirst(\\?, 缺失); } } } } //删除多余的行 sql sql.replaceAll((?m)^[ \t]*\r?\n, ); return sql; } private static String getParameterValue(Object object) { String value ; if(object instanceof String){ valueobject.toString(); }else if (object instanceof Date){ DateFormat formatDateFormat.getDateInstance(DateFormat.DEFAULT,Locale.CHINA); valueformat.format(object); } else if (!ObjectUtils.isEmpty(object)) { valueobject.toString(); } return value; } Override public Object plugin(Object target) { return Plugin.wrap(target,this); } Override public void setProperties(Properties properties) { } }启动项目即可
返回列表