Mybatis部分写法总结

一.在获取数据的同时更新某个字段的值

   <select id="getChannelUrl" resultMap="BaseResultMap">
        SELECT * FROM channel_url u ,channel c WHERE u.catgory = #{category}
        AND u.channel_id = c.id
        AND c.status=0
        order by u.down_count
        <if test="count != null and count>=0">
        limit #{count}
        </if>
        for UPDATE;
        UPDATE channel_url u
        , (SELECT u.id FROM channel_url u ,channel c WHERE u.category = #{category}
        AND u.channel_id = c.id
        AND c.status=0
        order by u.down_count
        <if test="count != null and count>=0">
            limit #{count}
        </if>) a SET u.down_count = u.down_count+1 where a.id =u.id;
    </select>

二.更新数据时判断数据值按条件进行更新

    <update id="changeCategyByIds">
        update  channel_url
        set category = (CASE category
        WHEN 0 THEN 1
        WHEN 1 THEN 0
        ELSE 0
        END)
        WHERE id IN
        <foreach collection="list" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
    </update>

你可能感兴趣的:(---javaweb)