首页 > 文章

mybatis拦截器获取sql

my sql

mybatis获取sql代码

package com.icourt.alpha.log.interceptor; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DateFormat; import java.util.*; import java.util.regex.Matcher; /** * 用于记录mybatis中的sql中不包含officeId的sql * Created by shiwen on 2017/9/12. * <p> */ @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 MybatisSqlInterceptor implements Interceptor { private static final Logger logger = LoggerFactory.getLogger(MybatisSqlInterceptor.class); private static final Logger sqlLogger = LoggerFactory.getLogger("mybatisSql"); private static String [] officeIdNames = new String[]{"OFFICE_ID","OFFICEID","PK_OFFICE_ID"}; @Override public Object intercept(Invocation invocation) throws Throwable { try { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; //0.sql参数获取 Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } //1.获取sqlId String sqlId = mappedStatement.getId(); BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); //获取真实的sql语句 String sql = getSql(configuration, boundSql, sqlId, 0); //2.判断是否有officeId if (hasOfficeId(sql,officeIdNames)) { sqlLogger.warn("{}", sql); } else { sqlLogger.debug("{}", sql); } } catch (Exception e) { logger.error(e.getMessage(), e); } return invocation.proceed(); } /** * 判断sql语句中是否包含officeId字段 * * @param sql sql语句 * @return */ private boolean hasOfficeId(String sql,String[] officeIdNames) { //office ID 的可能名称 if (sql == null || sql.trim().length() == 0) { return false; } String afterWhereStatement = sql.toUpperCase().substring(sql.indexOf("where")); for (String officeIdName : officeIdNames){ if(afterWhereStatement.indexOf(officeIdName) > 0){ return true; } } return false; } @Override public Object plugin(Object o) { return Plugin.wrap(o, this); } @Override public void setProperties(Properties properties) { } private static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) { String sql = showSql(configuration, boundSql); StringBuilder str = new StringBuilder(100); str.append(sqlId); str.append(":"); str.append(sql); return str.toString(); } 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; } private static 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("\?", Matcher.quoteReplacement(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("\?", Matcher.quoteReplacement(getParameterValue(obj))); } else if (boundSql.hasAdditionalParameter(propertyName)) { Object obj = boundSql .getAdditionalParameter(propertyName); sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(obj))); } else { sql = sql.replaceFirst("\?", "缺失"); }//打印出缺失,提醒该参数缺失并防止错位 } } } return sql; } }

  • mybatis拦截器获取sql
  • Unix/Linux中shell调用sqlplus的方式
  • 本文地址:https://www.ytsfc.com/show-1-18119-0.html

    • 大结局为什么萧炎杀药老

      大结局为什么萧炎杀药老

      原著大结局为什么萧炎杀药老​1、原因就是萧炎自身的原因了。萧炎是远古八族之萧族的后人,萧炎手中的古玉是打开古帝遗迹的关键。收集古玉是药老他们一直的使命。所以,药老追...

    • 车标志识别图片大全  【图片】

      车标志识别图片大全 【图片】

      每一辆汽车都有自己的品牌标志,用于告诉大家它们的生产厂家。下面我们来认识一下一些比较常见的汽车标志,有不认识的小伙伴赶快记下来!中国汽车最著名的要属:奇瑞、吉利、长城、...

    • 达咩啥意思:达咩为什么不能随便说

      达咩啥意思:达咩为什么不能随便说

      哒咩什么意思达咩的意思是不行,不许,不可以的意思。达咩是日语的中文读音译字,是一句比较生活化的日常用语。日语常用语还有,晚安对长辈:お休みなさい(哦呀斯米那赛),再见:さようなら...

    • 水浒传人物介绍:水浒传15个人物介绍

      水浒传人物介绍:水浒传15个人物介绍

      水浒传15个人物介绍1, 及时雨,呼保义,宋江:及时雨:总在别人需要的时候出现 2,玉麒麟,卢俊义 ,麒麟是神兽,是百兽之长 富甲天下,仗义疏财3,智多星,吴用 智多星,聪明,智慧多多的上界星辰...

    评论底部pc
    评论底部 m

    豆豆的家

    http://www.ytsfc.com/

    '); })(); | 京ICP1234567-2号

    Powered By 电视大师网 XXX公司

    使用手机软件扫描微信二维码

    关注我们可获取更多热点资讯

    感谢小豆豆技术支持