简体   繁体   中英

MyBatis foreach with spring not working

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 ?

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);

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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