您的位置:首页 > 编程语言 > Java开发

mybatis3.3@sqlprovider nested exception is java.lang.NumberFormatException: null

2018-03-29 14:47 543 查看
使用@selectprovider的时候,方法中传了两个参数,然后编译时报错。
经查看 Itype @cjm0000000大神的博客http://qurey.iteye.com/blog/1944785后才懂得,
原来mybatis3.3中selectprovider中type类的方法的参数只能小于2个(标红的部分):
org.apache.ibatis.builder.annotation.ProviderSqlSource
[java] view plain copy public ProviderSqlSource(Configuration config, Object provider) {  
    String providerMethodName = null;  
    try {  
      this.sqlSourceParser = new SqlSourceBuilder(config);  
      this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);  
      providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);  
  
      for (Method m : this.providerType.getMethods()) {  
        if (providerMethodName.equals(m.getName())) {  
          <span style="color:#ff0000;">if (m.getParameterTypes().length < 2  
              && m.getReturnType() == String.class) </span>{  
            this.providerMethod = m;  
            this.providerTakesParameterObject = m.getParameterTypes().length == 1;  
          }  
        }  
      }  
    } catch (Exception e) {  
      throw new BuilderException("Error creating SqlSource for SqlProvider.  Cause: " + e, e);  
    }  
    if (this.providerMethod == null) {  
      throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"  
          + providerMethodName + "' not found in SqlProvider '" + this.providerType.getName() + "'.");  
    }  
  }  
而mybatis3.4中的代码就取消了参数个数限制:[java] view plain copy public ProviderSqlSource(Configuration config, Object provider) {  
    String providerMethodName;  
    try {  
      this.sqlSourceParser = new SqlSourceBuilder(config);  
      this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);  
      providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);  
  
      for (Method m : this.providerType.getMethods()) {  
        if (providerMethodName.equals(m.getName())) {  
          <span style="color:#ff0000;">if (m.getReturnType() == String.class) </span>{  
            if (providerMethod != null){  
              throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"  
                      + providerMethodName + "' is found multiple in SqlProvider '" + this.providerType.getName()  
                      + "'. Sql provider method can not overload.");  
            }  
            this.providerMethod = m;  
            this.providerMethodArgumentNames = new ParamNameResolver(config, m).getNames();  
          }  
        }  
      }  
    } catch (BuilderException e) {  
      throw e;  
    } catch (Exception e) {  
      throw new BuilderException("Error creating SqlSource for SqlProvider.  Cause: " + e, e);  
    }  
    if (this.providerMethod == null) {  
      throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"  
          + providerMethodName + "' not found in SqlProvider '" + this.providerType.getName() + "'.");  
    }  
  }  
所以,在mybatis3.3中使用@sqlprovider传多个参数时,要么直接用实体bean替换,要么用Map包装一下。

虽拾人牙慧,但自己再记一下有助自己提高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐