mybatis Selective動態判斷屬性值新增或修改操作,batch批次操作

oktokeep發表於2024-06-08

mybatis Selective動態判斷屬性值新增或修改操作,batch批次操作


mybatis insert foreach批次新增
https://www.cnblogs.com/oktokeep/p/17629512.html

<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>            

相關文章