首页 > 学院 > 开发设计 > 正文

mybatis 对 mysql的批量操作

2019-11-14 11:28:57
字体:
来源:转载
供稿:网友

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>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表