欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis批量操作

程序员文章站 2022-07-07 20:12:22
mybatis 批量新增int batchInsertGoods(List list); INSERT INTO goods(i_id, goods_name) VALUES (#{i...

mybatis 批量新增


int batchInsertGoods(List<Goods> list);
<insert id="batchInsertGoods" parameterType="java.util.List">
    INSERT INTO goods(i_id, goods_name)
    VALUES
    <foreach collection="list" item="item" separator=",">
        (#{item.id,jdbcType=VARCHAR},#{item.goodsName,jdbcType=VARCHAR})
    </foreach>
</insert>

mybatis 批量更新

public int updateUserBatch(List<User> userList);
<!-- 
	批量修改用户
	当传入的是一个List,collection="list"
	当传入的是一个数组,collection="array"
	当传入的是包装类或者map,collection为包装类的属性或者map的key
	注意:在mysql中,用多条语句运行的方式,需要在url连接中开启allowMultiQueries设置为true
-->
<update id="updateUserBatch" parameterType="list">
	<foreach collection="list" item="user" separator=";">
		UPDATE user
		<set>
			<if test="user.userName != null">
				user_name = #{user.userName},
			</if>
			<if test="user.loginName != null">
				login_name = #{user.loginName},
			</if>
			<if test="user.password != null">
				password = #{user.password},
			</if>
			<if test="user.age != null">
				age = #{user.age},
			</if>
			<if test="user.sex != null">
				sex = #{user.sex},
			</if>
			<if test="user.deptId != null">
				dept_id = #{user.deptId},
			</if>
			<if test="user.birthday != null">
				birthday = #{user.birthday},
			</if>
			<if test="user.tvUpdate != null">
				tv_update = #{user.tvUpdate},
			</if>
		</set>
		WHERE user_id = #{user.userId}
	</foreach>
</update>

mybatis批量删除

  void batchDeleteEmps(int[] empno);
<delete id="batchDeleteEmps" parameterType="int">
        delete from emp where empno in
        <foreach item="empnoItem" collection="array" open="(" separator="," close=")">
            #{empnoItem}
        </foreach>
</delete>

emp : 表名
empno : 字段名
collection:表示类型,这里参数是数组,就写成array,如果是集合,就写成list
item : 是一个变量名,自己随便起名

本文地址:https://blog.csdn.net/shisen0530/article/details/107152913