Mybatis中的foreach方法,批量插入和批量删除

介绍一种比较简单的批量操作,一个批量添加一个批量删除:

java代码:

public class User implements Serializable {

private Integer id;

private String name;

private String password;

//setter and getter

}

对应的Mapper.xml

column="id" property="id" jdbcType="Integer"/>

column="name" property="name" jdbcType="VARCHAR" /> 

<result column="password" property="password" jdbcType="VARCHAR" /> 



Mapper.xml中对应的批量插入方法:(传入的是一个List集合)

useGeneratedKeys="true" parameterType="java.util.List">

resultType="long" keyProperty="id" order="AFTER">

select last_insert_id()

insert into t_user (name,password) values

collection="list" item="item" index="index" separator=",">

(#{item.name},#{item.password})


Mapper.xml中对应的批量删除的方法:(传入的是一个string字符串,ids)

id="delete" parameterType="java.lang.String">

delete from t_user where id in (“${_param}”);($好像是#号,记不太清了,可以试下)


OK!

你可能感兴趣的:(总结)