1. 程式人生 > >mybatis 對 mysql的批量操作

mybatis 對 mysql的批量操作

1:插入返回主鍵

	<insert id="save" parameterType="map" useGeneratedKeys="true" keyProperty="ID" flushCache="false">
		insert into table (
			CODE
		) values (
			#{CODE}
		)
	</insert>
獲取的主鍵方式:map.get("ID")


2:批量插入  傳入引數list

	<insert id="save" parameterType="java.util.List" flushCache="false">
		insert into table 
		(
			
CODE ) values <foreach collection="list" item="item" separator=","> (#{item.CODE}) </foreach> </insert>
3:批量更新  傳入引數list

注:需在 properties的配置檔案中的URL 新增“allowMultiQueries=true”

	<update id="updates" parameterType="java.util.List" flushCache="false">
		<foreach collection="list" item="item" index="index" open="" close="" separator=";">
			update table 
<set> CODE =${item.CODE}, UPDATE_TIME =${item.UPDATE_TIME} </set> where ID =${item.ID} </foreach> </update>
4:批量刪除 傳入引數陣列

<delete id="deleteAll" parameterType="String" flushCache="false">
	delete from table
	where 
		ID in
  		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                  #{item}
             	
</foreach> </delete>