簡體   English   中英

MyBatis @foreach的更新問題

[英]MyBatis @Update issues with foreach

我用myBatis。 單條記錄語句可以正常工作,但是當我嘗試使用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);

這是我得到的錯誤:

由以下原因引起: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'}。 原因:org.apache.ibatis.type.TypeException:使用JdbcType null為參數#1設置非null時出錯。 嘗試為此參數或其他配置屬性設置不同的JdbcType。 原因:java.sql.SQLException:參數索引超出范圍(1>參數數量,為0)。 在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時出錯。 嘗試為此參數或其他配置屬性設置不同的JdbcType。 原因:java.sql.SQLException:參數索引超出范圍(1>參數數量,為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)。

實際上,這是tyopo應該使用的語法; 代替,用於分隔符。 這是可行的示例。

@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