之前已经用标签处理了List 集合数据,foreach 标签还有强大的功能,用于批量的 增、删、改 操作
1.批量添加
public void insertEmployeeBatch(@Param("emps")List<Employee> emps )
<insert id="insertEmployeeBatch">
insert into tbl_employee(last_name, gender, descr ) values
<foreach collection="emps" item="currEmp" separator=",">
(#{currEmp.lastName},#{currEmp.gender},#{currEmp.descr})
</foreach>
</insert>
2.批量修改, 原生的jdbc不支持多条sql语句的拼接执行,要添加一个配置 allowMultiQueries=true,例
jdbc.url=jdbc:mysql://localhost:3306/data?allowMultiQueries=true
<update id="insertEmployeeBatch">
<foreach collection="emps" item="currEmp" >
update tbl_employee set
last_name = #{currEmp.lastName} , gender = #{currEmp.gender}, descr = #{currEmp.descr}
where id = #{currEmp.id};
</foreach>
</update>
3.批量删除
public List<TAdmin> selectByIds(@Param("ids") List ids);
<delete id="selectByIds" resultType="com.mybatis.bean. Admin">
delete from admin where id in
<foreach collection="ids" item="currentId" open="(" close=")" separator=",">
#{currentId}
</foreach>
</delete>