mybatis拦截器sql日志进程

时间:2018-03-02 02:01:58

标签: mybatis interceptor

我在我的项目中制作了mybatis sqllog拦截器。

以防万一。 #{__frch_CUST_0} #{__frch_CUST_1} ~~~ #{__frch_CUST_N}

如何获取参数值"__frch_CUST_n"

// mybatis interceptor sql log function

public String getSqlLog(StatementHandler handler)
{
    String sql = handler.getBoundSql().getSql();
    Object param = handler.getParameterHandler().getParameterObject();
    List<ParameterMapping> paramMapping = handler.getBoundSql().getParameterMappings();

    // change \? value to replace point 
    for(ParameterMapping mapping:paramMapping)
    {
        sql = sql.replaceFirst("\\?", "#{"+mapping.getProperty()+"}");
    }

    for(ParameterMapping mapping:paramMapping)
    {
        String sqlparam      = "#{"+mapping.getProperty()+"}";
        String sqlparamname  = mapping.getProperty();
        Object sqlparamvalue = ((Map) param).get(sqlparamname);

        if( !sqlparamname.startsWith("__frch_") )
        {
            if(isNull(sqlparamvalue))
            {
                sql = sql.replace(sqlparam, "NULL");
            }
            else
            {
                if(sqlparamvalue instanceof String)
                {
                    try
                    {
                        sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
                    }
                    catch(Exception e)
                    {
                        sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
                    }
                }
                else
                {
                    sql = sql.replace(sqlparam, sqlparamvalue.toString());
                }
            }
        }
        else
        {
            **// HOW CAN I DO HERE?**   
        }
    }       

    return sql;
}

感谢您阅读我的问题..

1 个答案:

答案 0 :(得分:0)

我解决了自己。

mybatis创建新的arrayList参数。并使用它。

示例列表值

DATE = ['20180101','20180102','20180103'];

STEP = ['PLAN','MAKE','SETUP'];

__ frch_DATE_0

__ frch_DATE_1

__ frch_DATE_2

__ frch_STEP_3

__ frch_STEP_4

__ frch_STEP_5

mybatis __frch_ = ['20180101','20180102','20180103','PLAN','MAKE','SETUP']

所以我用它。我的问题已经解决..

public String getSqlLog(StatementHandler handler)
{
    String sql = handler.getBoundSql().getSql();
    Object param = handler.getParameterHandler().getParameterObject();
    List<ParameterMapping> paramMapping = handler.getBoundSql().getParameterMappings();        
    List foreachlist = new ArrayList();

    for(ParameterMapping mapping:paramMapping)
    {
        sql = sql.replaceFirst("\\?", "#{"+mapping.getProperty()+"}");
    }

    for(ParameterMapping mapping:paramMapping)
    {
        String sqlparam      = "#{"+mapping.getProperty()+"}";
        String sqlparamname  = mapping.getProperty();
        Object sqlparamvalue = ((Map) param).get(sqlparamname);

        if( !sqlparamname.startsWith("__frch_") )
        {
            if(isNull(sqlparamvalue))
            {
                sql = sql.replace(sqlparam, "NULL");
            }
            else
            {
                if(sqlparamvalue instanceof String)
                {
                    try
                    {
                        sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
                    }
                    catch(Exception e)
                    {
                        sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
                    }
                }
                else
                {
                    sql = sql.replace(sqlparam, sqlparamvalue.toString());
                }
            }
        }
        else
        {
            if( foreachlist.size() == 0 )
            {
                Map parammap = (Map)param;
                Iterator iterator = parammap.keySet().iterator();
                while(iterator.hasNext()) 
                {
                   String key = iterator.next().toString();
                   Object value = (Object)parammap.get(key);
                   if( value instanceof List )
                   {
                       List valuelist = (List)value;
                       for(int i=0;i<valuelist.size();i++)
                       {
                           foreachlist.add(valuelist.get(i));
                       }
                   }
                }
            }

            String buff = sqlparamname.split("__frch_")[1];
            int index  = Integer.parseInt(buff.substring(buff.lastIndexOf("_")+1));

            try
            {
                sql = sql.replace(sqlparam, "'"+getSQLString((String)foreachlist.get(index))+"'");
            }
            catch(Exception e)
            {
                ;
            }
        }
    }       

    return sql;
}