• mybatis:SQL拦截器


    打印执行的SQL语句

    import java.sql.Connection;
    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.statement.StatementHandler;
    import org.apache.ibatis.mapping.BoundSql;
    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.type.TypeHandlerRegistry;
    import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    @Intercepts({
    @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
    public class MybatisInterceptor implements Interceptor {
    
      private Logger log = LoggerFactory.getLogger(MybatisInterceptor.class);
    
      @SuppressWarnings("unused")
      @Autowired
      private MybatisProperties mybatisProperties;
    
      @Override
      public Object intercept(Invocation invocation) throws Throwable {
        try {
          StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
          BoundSql boundSql = statementHandler.getBoundSql(); // BoundSql就是封装myBatis最终产生的sql类
          log.debug(boundSql.getSql());
        } catch (Exception e) {
          log.error(e.toString());
        }
        return invocation.proceed(); // 执行完上面的任务后,不改变原有的sql执行过程
      }
    
      @Override
      public Object plugin(Object target) {
        if (target instanceof StatementHandler) {
          return Plugin.wrap(target, this);
        }
        return target;
      }
    
      @Override
      public void setProperties(Properties properties) {
    
      }
    
      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;
      }
    
      public String showSql(Configuration configuration, BoundSql boundSql) {
        Object parameterObject = boundSql.getParameterObject();
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        String sql = boundSql.getSql().replaceAll("[\s]+", " ");
        if (!parameterMappings.isEmpty() && parameterObject != null) {
          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;
      }
    
    }
  • 相关阅读:
    POJ3233 构造子矩阵+矩阵快速幂
    HDU4565-数学推导求递推公式+矩阵快速幂
    记录一个状压DP用到的骚操作
    POJ1273 最大流模板
    图论复习...
    2017-7 实训经验贴
    Polya定理应用实例
    直线,椭圆,三角形,折线分割平面问题
    hdu4801 PocketCube 2阶魔方
    1256:献给阿尔吉侬的花束
  • 原文地址:https://www.cnblogs.com/huiy/p/10572117.html
Copyright © 2020-2023  润新知