zoukankan      html  css  js  c++  java
  • 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;
      }
    
    }
  • 相关阅读:
    BNU 沙漠之旅
    手把手教你在Windows端搭建Redmine项目管理软件
    [置顶] mybatis批量新增系列之有主键的表的批量新增
    linux 命令之sar——监视系统状态
    简单的串行通信程序
    Hibernate_12_HQL句子
    初次使用glog
    2014辛星在读CSS第八节 使用背景图片
    poj 2763 Housewife Wind(树链拆分)
    堆,队列,单一列表,双向链表
  • 原文地址:https://www.cnblogs.com/huiy/p/10572117.html
Copyright © 2011-2022 走看看