han73748196
han73748196
2017-11-23 02:49
采纳率: 95.5%
浏览 7.4k
已采纳

mybatis 进行批量修改操作

批量修改时候传入多个数值不想在service层 进行循环修改,
想在写sql语句的时候进行批量修改
传入的有全部的id和需要修改的数据。怎么写sql语句。。。
只会删除的时候传入id的语句,,现在多个参数不会写了。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • qq_23292875
    Hefei19881002 2017-11-23 06:15
    已采纳
        <update id="batchUpdate" parameterType="java.util.List">  
                            UPDATE STUDENT
                            <set> 
                                    <if test="bannerName != null">
                                            t.banner_name = #{bannerName},
                                    </if>
                                    <if test="bannerUrl != null">
                                            t.banner_url = #{bannerUrl},
                                    </if>
                            </set>                                  
                            WHERE id IN  
                        <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >  
                                #{item}  
                        </foreach>  
    </update>  
    
    点赞 评论
  • qq_23292875
    Hefei19881002 2017-11-23 02:56

    update tablename set xxx = xxx where id in (xxx,xxx,xxx)

    点赞 评论
  • qq_23292875
    Hefei19881002 2017-11-23 02:58
    或者
            <update id="batchUpdate" parameterType="java.util.List">  
        UPDATE STUDENT SET name = "250" WHERE id IN  
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >  
            #{item}  
        </foreach>  
    </update>  
    
    点赞 评论
  • han73748196
    han73748196 2017-11-23 03:25

    set不是固定值,是前台传过来的

    点赞 评论
  • wbc_18989265729
    wbc_18989265729 2017-11-23 03:37

    mybatis自动生成的update就可以动态set

    点赞 评论
  • DongVagrant
    DongVagrant 2017-11-23 03:45

    使用动态sql

    update student


    id=#{id} ,


    last_name=#{lastName} ,


    email=#{email} ,


    点赞 评论
  • DongVagrant
    DongVagrant 2017-11-23 03:51
    <update id="Update" >  
        UPDATE STUDENT    
     <trim prefix="set " suffixOverrides=",">
                 <if test="lastName!=null and lastName!=''">
                     last_name=#{lastName} ,
                 </if>
                 <if test="email!=null and email.trim()!=''">
                     email=#{email} ,
                 </if>
             </trim>
                    where id in
            <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >  
            #{item}  
        </foreach> 
        </update>
    
    点赞 评论
  • welan123123
    奔跑的小鱼儿 2017-11-23 05:52

    给你个模板,自己换一下字段就好了,传入的参数是实体类

     <update id="delete">
            DELETE FROM wsd_count
            <where>
                <if test="id != null and id != ''">
                    AND id = #{id}
                </if>
                <if test="uid != null and uid != ''">
                    AND uid = #{uid}
                </if>
                <if test="line != null and line != ''">
                    AND line LIKE 
                        <if test="dbName == 'oracle'">'%'||#{line}||'%'</if>
                        <if test="dbName == 'mssql'">#{line}+'%'</if>
                        <if test="dbName == 'mysql'">concat('%',#{line},'%')</if>
                </if>
                <if test="num != null and num != ''">
                    AND num = #{num}
                </if>
                <if test="result != null and result != ''">
                    AND result = #{result}
                </if>
                <if test="data != null and data != '' and countReserve5 != null and countReserve5 != ''">
                    AND data BETWEEN #{data} AND #{countReserve5}
                </if>
            </where>
        </update>
    
    点赞 评论

相关推荐