美文网首页
如何获取mybatis的sql

如何获取mybatis的sql

作者: 剑道_7ffc | 来源:发表于2020-04-07 19:03 被阅读0次

    需求

    在maven编译过程中,获取mybatis的sql

    核心基础

    org.apache.ibatis.binding.MapperMethod#getParam

      private Object getParam(Object[] args) {
        final int paramCount = paramPositions.size();
        if (args == null || paramCount == 0) {
          return null;
        } else if (!hasNamedParameters && paramCount == 1) {
          return args[paramPositions.get(0)];
        } else {
          Map<String, Object> param = new MapperParamMap<Object>();
          for (int i = 0; i < paramCount; i++) {
            param.put(paramNames.get(i), args[paramPositions.get(i)]);
          }
          // issue #71, add param names as param1, param2...but ensure backward compatibility
          for (int i = 0; i < paramCount; i++) {
            String genericParamName = "param" + String.valueOf(i + 1);
            if (!param.containsKey(genericParamName)) {
              param.put(genericParamName, args[paramPositions.get(i)]);
            }
          }
          return param;
        }
      }
    

    核心代码

    获取参数

        private Object getMethodParamValues(Method method){
            int paraCount = method.getParameterTypes().length;
            if(paraCount <= 1){
                return null;
            }
            return Maps.newHashMap();
        }
    

    获取sql

    mappedStatement.getBoundSql(paramValues).getSql();
    

    相关文章

      网友评论

          本文标题:如何获取mybatis的sql

          本文链接:https://www.haomeiwen.com/subject/mzfophtx.html