注意 item.的方式获取循环中的对象属性
注意 item.的方式获取循环中的对象属性
注意 item.的方式获取循环中的对象属性
---------------------------------------------
首先要在数据库连接配置中设置允许批量操作(连接数据源参数配置中新增 allowMultiQueries=true)
spring:
profiles: dev
datasource:
name: testDatabase
url: jdbc:mysql://localhost:3306/testTable?allowMultiQueries=true
---------------
1.批量插入
Mapper 函数
Integer insertMergeRequests(List<MergeRequest> mergeRequests);
-----
mybatis xml
<insert id="insertMergeRequests" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="requestId"> insert into user_merge_request (merge_uid, uid, create_time, update_time) values <foreach collection="list" item="item" index="index" separator=","> (#{item.mergeUid,jdbcType=BIGINT},#{item.uid,jdbcType=BIGINT},#{item.createTime,jdbcType=TIMESTAMP},#{item.updateTime,jdbcType=TIMESTAMP}) </foreach> </insert>
2.批量更新
Mapper函数
int updateTeamMembers(@Param("teamMembers") List<TeamMember> teamMembers);
----
mybatis xml
<update id="updateTeamMembers" parameterType="java.util.List"><foreach collection="teamMembers" item="item" index="index" open="" close="" separator=";">update lop_team_member <set><if test="item.memberType != null">member_type=#{item.memberType},</if><if test="item.status != null">status = #{item.status}</if> </set> where uid = #{item.uid} and team_id = #{item.teamId}</foreach></update>