mybatis使用InsertProvider注解报错解决过程

??????? 目前项目在使用mybatis,并且是使用注解的方式。在使用InsertProvider注解的时候报了一下的错误:org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.? Cause: org.apache.ibatis.builder.BuilderException: Error creating SqlSource for SqlProvider. Method........

?????? 注解是如下这个样子的

@InsertProvider(method = "insertlist",type=SqlProvider.class)
?public int insertInnerTable(List list,String dbTable);

??????? 思路是要写一个通用的插入一个集合的方法,但是在执行的时候就报了上面的错误。在网上查资料未果。于是只能自己动手,丰衣足食了。一步步跟断点,跟到mybatis了报错的方法中,发现了如下的代码

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())) {
????????? if (m.getParameterTypes().length < 2
????????????? && m.getReturnType() == String.class) {
??????????? this.providerMethod = m;
??????????? this.providerTakesParameterObject = m.getParameterTypes().length == 1;
????????? }
??????? }
????? }
??? } catch (Exception e) {
????? throw new BuilderException("Error creating SqlSource for SqlProvider.? Cause: " + e, e);
??? }

??????? 注意标黄的位置,终于发现导致错误的罪魁祸首了,原来是这里限制了参数的个数,不能操作两个参数的啊。

??????? 于是将方法以及注解改为如下形式

@InsertProvider(method = "insert",type=SqlProvider.class)
?public int insert(SqlContext sqlContext);

在SqlProvider中对应的方法为

public String insert(SqlContext sqlContext){

????? ........

}

至此问题解决

文章来自:http://fhqfjfh.iteye.com/blog/2273430
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3