mybatis Selective动态判断属性值新增或修改操作,batch批量操作

2024-10-09 11:26:27 浏览数 (3)

mybatis Selective动态判断属性值新增或修改操作,batch批量操作

mybatis insert foreach批量添加

https://cloud.tencent.com/developer/article/2455763

代码语言:javascript复制
<insert id="insertSelective" parameterType="com.common.entity.OrderDO">
        insert into tbl_order
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="partner != null">`partner` </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="partner != null">#{partner}  </if>
        </trim>
    </insert>

<update id="updateByPrimaryKeySelective" parameterType="com.common.entity.OrderDO">
    update tbl_order 
    <set>
            <if test="partner != null">`partner` = #{partner} </if>
    </set>
    where id = #{id}
</update>
    

##批量新增操作,根据第一个字段的属性值来判断    
    <insert id="insertSelectiveBatch" parameterType="java.util.List">
        insert into tbl_order
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="list[0].partner != null">`partner` </if>
        </trim>
        values
        <foreach collection="list" item="coupon" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides="," >
                <if test="coupon.partner != null">#{coupon.partner}  </if>
            </trim>
        </foreach>
    </insert>            
    

##全部添加
    <insert id="insertSelectiveBatch" parameterType="java.util.List">
        insert into tbl_order
        <trim prefix="(" suffix=")" suffixOverrides="," >
            `partner`
        </trim>
        values
        <foreach collection="list" item="coupon" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides="," >
                #{coupon.partner}
            </trim>
        </foreach>
    </insert>            

0 人点赞