简体   繁体   English

MyBatis @foreach的更新问题

[英]MyBatis @Update issues with foreach

I use myBatis. 我用myBatis。 Single record statement works ok, but when I try to use foreach to perform update of list of records an error occurs regarding mapping a bit weird since it works ok for single record. 单条记录语句可以正常工作,但是当我尝试使用foreach来执行记录列表更新时,由于它对单条记录可以正常工作,因此在映射时出现了一些奇怪的错误。

@Update("UPDATE table SET field_one=#{input.fieldOne} WHERE field_two =#{input. fieldTwo}")
    public void updateDomain(@Param("input") ObjectList input);

@Update({"<script>",
        "<foreach item='item' index='index' collection='input' separator=','>",
        "UPDATE table",
        "SET field_one = '#{item.fieldOne}'",
        "WHERE field_two = '#{item.fieldTwo}'", 
        "</foreach> ",
        "</script>"})
public void updateDomains(@Param("input") List<ObjectList> input);

Here's the error I get: 这是我得到的错误:

Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='__frch_item_0.fieldOne', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. 由以下原因引起:org.apache.ibatis.type.TypeException:无法设置用于映射的参数:ParameterMapping {property ='__ frch_item_0.fieldOne',mode = IN,javaType = class java.lang.String,jdbcType = null,numericalScale = null ,resultMapId ='null',jdbcTypeName ='null',expression ='null'}。 Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . 原因:org.apache.ibatis.type.TypeException:使用JdbcType null为参数#1设置非null时出错。 Try setting a different JdbcType for this parameter or a different configuration property. 尝试为此参数或其他配置属性设置不同的JdbcType。 Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 原因:java.sql.SQLException:参数索引超出范围(1>参数数量,为0)。 at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:89) at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93) Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . 在org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93)的org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:89)造成原因:org.apache.ibatis .type.TypeException:使用JdbcType null为参数#1设置非null时出错。 Try setting a different JdbcType for this parameter or a different configuration property. 尝试为此参数或其他配置属性设置不同的JdbcType。 Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 原因:java.sql.SQLException:参数索引超出范围(1>参数数量,为0)。 at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:55) at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:87) ... 45 common frames omitted Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 在org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:87)处org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:55)...省略了45个常见框架java.sql.SQLException:参数索引超出范围(1>参数数量,为0)。

Actually it was syntax tyopo should ; 实际上,这是tyopo应该使用的语法; instead of , for separator. 代替,用于分隔符。 Here is Workable Example. 这是可行的示例。

@Update({ "<script>",
            "<foreach item='item' index='index' collection='collectionToUpdate' separator=';'>",
            "UPDATE domains",
            "SET columnOne =  #{item.valueOne} WHERE columnTwo = #{item.valueTwo}", 
            "</foreach> ",
            "</script>"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM