mybatis怎么批量删除和添加

mybatis怎么批量删除和添加,第1张

批量增加

<insert id="batchSaveUser">

insert into t_user (user_name,sex) values

<!-- 动态SQL之foreach的用法 -->

<!-- collection="users" 用于指定循环集合的名称,如果接口中并未指定参数别名,那么默认就是list

item="u" 用于指定每次循环后的对象的别名

separator="," 用于指定每次循环后之间的分割符-->

<foreach collection="users" item="u" separator=",">

(#{u.userName},#{u.sex})

</foreach>

</insert>

批量删除

<delete id="batchDeleteUser">

delete from t_user where id in (

<foreach collection="ids" item="id" separator=",">

#{id}

</foreach>

)

</delete>

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

Java代码:

public class User implements Serializable {

private Integer id

private String name

private String password

//setter and getter

}

对应的Mapper.xml

<ResultMap type="User" id="UserResultMap">

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

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

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

</ResultMap>

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

<insert id="add" useGeneratedKeys="true" parameterType="java.util.List">

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

select last_insert_id()

</selectKey>

insert into t_user (name,password) values

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

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

</foreach>

</insert>

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

<delete

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

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

</delete>

OK!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/7925866.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存