mybatis 批量插入代码样例

注意事项:

mysql开启:allowMultiQueries=true 是 MySQL 连接字符串的一个属性。将其设置为 true 表示允许一次性执行多条 SQL 查询,这些查询可以用分号 ; 分隔。通常情况下,这个属性默认是 false,以确保安全性,防止 SQL 注入攻击。

<insert id="addAllVolunteerList04" parameterType="java.util.List">
        <foreach collection="volunteers" item="item" index="index" separator=";">
            insert into volunteer
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.vtIntegralCount != null">vt_integral_count,</if>
                <if test="item.vtName != null and item.vtName !=''">vt_name,</if>
            </trim>
            <trim prefix="values(" suffix=")" suffixOverrides=",">
                <if test="item.vtIntegralCount != null">#{item.vtIntegralCount},</if>
                <if test="item.vtName != null and item.vtName !=''">#{item.vtName},</if>
            </trim>
        </foreach>
    </insert>

 

 

public int addAllVolunteerList04(@Param("volunteers") List<Volunteer> volunteers);
    <insert id="addAllVolunteerList02" parameterType="java.util.List">
        insert into volunteer(vt_integral_count,vt_name)
        values
        <foreach collection="volunteers" item="volunteer" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="volunteer.vtIntegralCount != null">#{volunteer.vtIntegralCount,jdbcType=INTEGER},</if>
                <if test="volunteer.vtName != null and volunteer.vtName !=''">#{volunteer.vtName,jdbcType=VARCHAR},</if>
            </trim>
        </foreach>
    </insert>

 

posted @ 2024-04-03 17:00  张载zz  阅读(11)  评论(0编辑  收藏  举报