天天看點

Mybatis增強動态sql

動态sql

批量新增與删除

<!--批量插入-->
    <insert id="batchsave">
        INSERT INTO t_product (name, salary, color) values
        <!--
           collection:要麼是array要麼是list
           item: 循環疊代的元素,給每個元素取一個别名叫做i
           separator:分隔符,循環疊代的時候以,進行分割
       -->
        <foreach collection="list" item="i" separator=",">
            (#{i.name},#{i.salary},#{i.color})
        </foreach>
    </insert>
    <!--根據id批量删除-->
    <delete id="batchDeleteById">
        DELETE FROM t_product WHERE id IN
        <foreach collection="array" item="i" separator="," close=")" open="(">
            #{i}
        </foreach>
    </delete>
    <!--批量删除-->
    <delete id="batchDelete">
        DELETE FROM t_product WHERE id IN
        <foreach collection="list" item="i" open="(" close=")" separator=",">
            #{i.id}
        </foreach>
    </delete>
           

繼續閱讀