mysql insert插入時與update修改時的條件判斷

最近做級聯關係:首先是insert時,有可能同時插入一二三級表,也有可能一二級不變,只插入一二級下面的第三級表。所以DAO層一起執行三條sql語句,自動事務。dual是臨時表,不用創建,直接寫語句就可以了

 <insert id="insertFirstRentalInThrid">
        INSERT INTO firstrental(
        <include refid="FirstRental_column_sql"/>
        )
        SELECT
        #{fid },
        #{fname}
        FROM dual
        WHERE not exists (select * from firstrental
        where fid = #{fid});
    </insert>

<insert id="insertSecondRentalInThrid">
        INSERT INTO secondrental(
        <include refid="SecondRental_column_sql"/>
        )
        SELECT
        #{sid },
        #{fid },
        #{sname },
        #{title },
        #{content },
        #{date },
        #{tel },
        #{address }
        FROM dual
        WHERE not exists (select * from secondrental
        where sid = #{sid});
    </insert>

接下來是update,也是一個道理,但是update直接用if就可以了

 <update id="updateSecondRentalInThird" parameterType="com.rental.entity.SecondRental">
        UPDATE secondrental
        <set>
            <if test="fid                 !=null">fid = #{ fid },</if>
            <if test="sname                  !=null">sname = #{ sname },</if>
            <if test="title                  !=null">title = #{ title },</if>
            <if test="content                 !=null">content = #{ content },</if>
            <if test="date                  !=null">date = #{ date },</if>
            <if test="tel             !=null">tel = #{ tel },</if>
            <if test="address             !=null">address = #{ address }</if>
        </set>
        WHERE
        sid=#{sid}
    </update>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章