mybatis 通过拦截器打印完整的sql语句以及执行结果操作

ice204 / 2024-02-08 / 原文

  1. 下面的文件放在source-fw的【jp.co.token.sikyuu.iterceptor】包下面
  • MybatisInterceptor.java

  • InterceptorForQry.java

  • 下面的文件放在source-fw的【jp.co.token.sikyuu.common】包下面

  • FastJsonUtils.java

  • 下面的文件放在WebRoot/WEB-INF/lib/ 路径下面

  • fastjson-1.2.76.jar

  • 给source里面的【myBatis-config.xml】文件里面,添加下面的内容

具体添加的位置,参照下记的图片

img

MybatisInterceptor.java

package jp.co.token.SIKYUU.iterceptor;

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;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <pre>
* [功 能] 完整的SQL语句输出
* </pre>
*/
@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 }) })
@SuppressWarnings({})
public class MybatisInterceptor implements Interceptor {
   /**
    * 记录对象
    */
   protected Logger log = LoggerFactory.getLogger(MybatisInterceptor.class);
   @Override
   public Object intercept(Invocation invocation)
           throws Throwable {
       try {
           // 获取xml中的一个select/update/insert/delete节点,是一条SQL语句
           MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
           Object parameter = null;
           // 获取参数,if语句成立,表示sql语句有参数,参数格式是map形式
           if (invocation.getArgs().length > 1) {
               parameter = invocation.getArgs()[1];
               log.debug("パラメータデータ: " + parameter);
           }
           // 获取到节点的id, 即sql语句的id
           String sqlId = mappedStatement.getId();
           log.debug("実行SQLID: " + sqlId);
           // BoundSql就是封装myBatis最终产生的sql类
           BoundSql boundSql = mappedStatement.getBoundSql(parameter);
           // 获取节点的配置
           Configuration configuration = mappedStatement.getConfiguration();
           // 获取到最终的sql语句
           String sql = getSql(configuration, boundSql, "");
           log.debug("sql = " + sql);
       } catch (Exception e) {
           e.printStackTrace();
       }
       return invocation.proceed();
   }
   // 封装了一下sql语句,使得结果返回完整xml路径下的sql语句节点id + sql语句
   public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) {
       String sql = showSql(configuration, boundSql);
       StringBuilder str = new StringBuilder(100);
       str.append(sqlId);
       str.append(":");
       str.append(sql);
       return str.toString();
   }
   // 如果参数是String,则添加单引号, 如果是日期,则转换为时间格式器并加单引号; 对参数是null和不是null的情况作了处理
   private static 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(new Date()) + "'";
       } else {
           if (obj != null) {
               value = obj.toString();
           } else {
               value = "";
           }
       }
       return value;
   }
   // 进行?的替换
   public static String showSql(Configuration configuration, BoundSql boundSql) {
       // 获取参数
       Object parameterObject = boundSql.getParameterObject();
       List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
       // sql语句中多个空格都用一个空格代替
       String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
       if ((parameterMappings != null && !parameterMappings.isEmpty()) && parameterObject != null) {
           // 获取类型处理器注册器,类型处理器的功能是进行java类型和数据库类型的转换
           TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
           // 如果根据parameterObject.getClass()可以找到对应的类型,则替换
           if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
               sql = sql.replaceFirst("\\?",
                       Matcher.quoteReplacement(getParameterValue(parameterObject)));
           } else {
               // MetaObject主要是封装了originalObject对象,提供了get和set的方法用于获取和设置originalObject的属性值,主要支持对JavaBean、Collection、Map三种类型对象的操作
               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("\\?",
                               Matcher.quoteReplacement(getParameterValue(obj)));
                   } else if (boundSql.hasAdditionalParameter(propertyName)) {
                       // 该分支是动态sql
                       Object obj = boundSql.getAdditionalParameter(propertyName);
                       sql = sql.replaceFirst("\\?",
                               Matcher.quoteReplacement(getParameterValue(obj)));
                   } else {
                       // 打印出缺失,提醒该参数缺失并防止错位
                       sql = sql.replaceFirst("\\?", "欠ける");
                   }
               }
           }
       }
       return sql;
   }
   @Override
   public Object plugin(Object target) {
       return Plugin.wrap(target, this);
   }
   @Override
   public void setProperties(Properties properties) {
   }
}

InterceptorForQry.java

package jp.co.token.SIKYUU.iterceptor;

import java.util.Properties;

import jp.co.token.SIKYUU.common.FastJsonUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
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.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class,
        Object.class, RowBounds.class, ResultHandler.class }) })
public class InterceptorForQry implements Interceptor {
    /**
     * 记录对象
     */
    protected Logger log = LoggerFactory.getLogger(InterceptorForQry.class);
    @SuppressWarnings({})
    public Object intercept(Invocation invocation)
            throws Throwable {
        Object result = invocation.proceed();
        String str = FastJsonUtils.toJSONString(result);
        log.debug("SQL执行结果: " + str);
        return result;
    }
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
    public void setProperties(Properties arg0) {
    }
}

FastJsonUtils.java

package jp.co.token.SIKYUU.common;

import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
 * <pre>
 * [功 能] fastjson
 * </pre>
 */
public class FastJsonUtils {
    private static final SerializeConfig config;
    static {
        config = new SerializeConfig();
        config.put(java.util.Date.class, new JSONLibDataFormatSerializer());
        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
    }
    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty
    };
    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, config, features);
    }
    public static String toJSONNoFeatures(Object object) {
        return JSON.toJSONString(object, config);
    }
    public static Object toBean(String text) {
        return JSON.parse(text);
    }
    public static <T> T toBean(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }
    public static <T> Object[] toArray(String text) {
        return toArray(text, null);
    }
    public static <T> Object[] toArray(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz).toArray();
    }
    public static <T> List<T> toList(String text, Class<T> clazz) {
        return (List<T>) JSON.parseArray(text, clazz);
    }
    /*public static Object beanToJson(KeyValue keyvalue) {
        String textJson = JSON.toJSONString(keyvalue);
        Object objectJson  = JSON.parse(textJson);
        return objectJson;
    }*/
    public static Object textToJson(String text) {
        Object objectJson  = JSON.parse(text);
        return objectJson;
    }
    @SuppressWarnings("rawtypes")
    public static Map stringToCollect(String s) {
        Map m = JSONObject.parseObject(s);
        return m;
    }
    @SuppressWarnings("rawtypes")
    public static String collectToString(Map m) {
        String s = JSONObject.toJSONString(m);
        return s;
    }
}

[fastjson-1.2.76.jar]:

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.76</version>
</dependency>