Mybatis 添加语句以及批量操作

<insert id="add">
      insert into t_watch_log
      <!-- 添加表字段 -->
         <trim prefix="(" suffix=")" suffixOverrides=",">
                 <if test="CustomerId != null and CustomerId !=''">CustomerId,</if>
                 <if test="watch_code != null and watch_code !=''">watch_code,</if>
        </trim>
    <!-- 注入控制层字段 -->
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="CustomerId != null and CustomerId !=''">#{CustomerId},</if>
        <if test="watch_code != null and watch_code !=''">#{watch_code},</if>
    </trim>
</insert>

1、根据主键进行新增操作。传入List集合,如果主键重复,则为更新操作。不重复,则是新增操作

<insert id="updateOilList" parameterType="Oil" >  
        insert into oilno(sinopec_code,content,density)  
        VALUES  
        <foreach collection="oilList" item="list" separator=",">  
            (#{list.sinopec_code},#{list.content},#{list.density})  
        </foreach>  
        ON DUPLICATE KEY UPDATE sinopec_code=VALUES(sinopec_code),content=VALUES(content),density=VALUES(density)
</insert>  

2、更新批量操作

<update id="updateOilList"  parameterType="java.util.List">  
    <foreach collection="oilList" item="item" index="index" separator=";">
        update oilno
        <set >
            <if test="item.density != null" >
                density = #{item.density},
            </if>
            <if test="item.listprice != null" >
                listprice = #{item.listprice},
            </if>
        </set>
        where oilclass = #{item.oilclass}
    </foreach>       
</update>

3、删除批量操作

<delete id="deleteBatch" parameterType ="java.util.List">
    DELETE FROM im_base_account WHERE id in(
    <foreach item="o" collection="list" open="" separator="," close="" >
        #{o.accountId}
    </foreach>
    )
</delete>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值