mybaitis的批量添加修改
添加:
<insert id="savelist" parameterType="java.util.List">
insert into table1(
a,
b,
c,
d
) values
<foreach collection="list" item="l" index="index" separator=",">
(
#{l.a},
#{l.b},
#{l.c},
#{l.d}
)
</foreach>
</insert>
批量添加时碰到的错误:Parameter '__frch_item_0' not found. Available parameters are [list]
原因可能如下:
1.查看parameterType的类型是不是Java.util.List类型
2.看foreach的collection属性是不是list
3.看foreach里取的属性值是否写错,大小写是否相同
4.查看foreach里取的属性值实体对象中是否存在
尤其是第四条,我本来还想找能不能动态批量添加,但是并没有找到。如果有哪个大神知道,麻烦评论告诉我,非常感谢
批量修改:
<update id="editlist" parameterType="java.util.Map">
update table1
<trim prefix="set" suffixOverrides=",">
<!-- 拼接case when 这是一种写法 -->
<!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->
<!--when #{cus.id} then #{cus.age}-->
<!--</foreach>-->
<!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->
<trim prefix="b =case" suffix="end,">
<foreach collection="list" item="l">
<if test="l.b!=null and l.b != ''">
when id = #{l.id} then #{l.b}
</if>
</foreach>
</trim>
<trim prefix="c =case" suffix="end,">
<foreach collection="list" item="l">
<if test="l.c!=null and l.c != ''">
when id = #{l.id} then #{l.c}
</if>
</foreach>
</trim>
<trim prefix="d =case" suffix="end,">
<foreach collection="list" item="l">
<if test="l.d!=null and l.d != ''">
when id = #{l.id} then #{l.d}
</if>
</foreach>
</trim>
<where>
<foreach collection="list" separator="or" item="l">
id = #{l.id}
</foreach>
</where>
</update>
这个可以动态批量修改,还是很方便好用的