mybatis

mybatis exception 'There is no getter for property named (foreach item) in VO'


I found similar exceptions but they were about collection getter.

This is my code and the exceptions is There is no getter for property named 'variableData' in 'MyDataVO'

Why eclipse looking a foreach item getter?

Thanks for reading

Java

public class MyDataVO {
    private String commonData;
    private String commonData2;
    private List<String[]> variableDataList;

    public List<String[]> getVariableDataList() {
        return variableDataList;
    }
    public void setVariableDataList(List<String[]> variableDataList){
        this.variableDataList = variableDataList;
    }
}

SQL

<insert id="insertMyData" parameterType="MyDataVO">
    INSERT INTO my_data_table (common_data, common_data2, variable_data1, variable_data2, variable_data3)
    VALUES
    <foreach collection="variableDataList" item="variableData" separator=",">
        (
            #{commonData},
            #{commonData2},
            #{variableData[0]},
            #{variableData[1]},
            #{variableData[2]}
        )
    </foreach>
</insert>

Solution

  • You may have to use <foreach> to access the array item.

    ...
    (
      #{commonData},
      #{commonData2},
      <foreach collection="variableData" item="str" index="idx" separator=",">
        <if test="3 > idx">
          #{str}
        </if>
      </foreach>
    )
    ...
    

    The <if> is unnecessary if the array always contains 3 items.