MyBatis foreach with spring not working

3.2k Views Asked by At

I am Trying to update list for records but i got the following error in mybatis.

 org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'list' in 'class com.model.DataParameters'

and my mybatis xml query is as follow

<update id="deleteAssociatedEntityForParentEntity" parameterType="com.model.DataParameters">
    update dataTable set deleted = #{deleted}, syncTS = #{syncTS} where
    data_id in
    <foreach item="dataIds" index="index" collection="list"
        open="(" separator="," close=")">
        #{dataIds}
    </foreach>
    and aData_type = #{dataType};

</update>

DataParameter class getter setter has been declared in this class. dataIds is my list.

please let me know if is their any wrong in my query. why list is not taking in? Any other way Guys ?

2

There are 2 best solutions below

0
On BEST ANSWER

Put your list name of class in collection attribute and an opcional name in item attribute to use below

you try this code:

<update id="deleteAssociatedEntityForParentEntity" parameterType="com.model.DataParameters">
    update dataTable set deleted = #{deleted}, syncTS = #{syncTS} where
    data_id in
    <foreach item="id" index="index" collection="dataIds"
        open="(" separator="," close=")">
        #{id}
    </foreach>
    and aData_type = #{dataType};

</update>
3
On

I suggest you some additional check before the foreach .. dataIds should be List.

<update id="deleteAssociatedEntityForParentEntity" parameterType="com.model.DataParameters">
   update dataTable set deleted = #{deleted}, syncTS = #{syncTS} where 1=1
   <if test="dataIds!= null">
        and data_id in
        <foreach item="item" index="index" collection="dataIds"
        open="(" separator="," close=")">
        #{item}
        </foreach>
    </if>
   and aData_type = #{dataType};
</update>

This is only a snippet of code

SqlSession session = sessionFactory.openSession();

Map<String, Object> mapParameter = new HashMap<String, Object>();
List<Integer> listDataIds = ....

mapParameter.put("dataIds",listDataIds );

session.update("deleteAssociatedEntityForParentEntity",mapParameter);