mybatis批量增删改
程序员文章站
2024-03-25 21:36:22
...
mybatis 批量处理更新(增删改)操作
dao层接口:
/**
* 批量新增数据
*/
void addLanguageBatch(List<LanguageDTO> list);
/**
* 批量删除
*/
void deleteLanguageBatch(List<Integer> list);
/**
* 批量修改
*/
void updateLanguageBatch(List<LanguageDTO> list);
mapper.xml:
<!--批量新增-->
<insert id="addLanguageBatch" parameterType="java.util.List">
insert into language (language,grade) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.language}, #{item.grade})
</foreach>
</insert>
<!--批量删除-->
<delete id="deleteLanguageBatch">
delete from language where
<foreach collection="list" item="key" separator="or">
id=#{key}
</foreach>
</delete>
<!--批量修改 -->
<update id="updateLanguageBatch">
update language set
<trim prefix="language=case" suffix="end">
<foreach collection="list" item="language">
when id=#{language.id} then #{language.language}
</foreach>
</trim>
<where>
<foreach collection="list" item="language" separator="or">
id=#{language.id}
</foreach>
</where>
</update>
使用postman测试:
查看数据库是否插入成功
如上就是使用mybatis进行批量增删改操作,都已测试成功!