Oracle对一张表进行插入(Insert)和更新(Update) 使用一条Sql语句(Merge Into)
1.mapper接口代码:
void saveOrUpdateFollow(@Param("uId") Long uId, @Param("byId")Long byId, @Param("status")Integer status,@Param("date") Date date);
2.SQL语句:
<insert id="saveOrUpdateFollow" useGeneratedKeys="false">
merge into CONCERN_SOMETHING T1
using (select #{uId} CONCERN_ID,#{byId} BY_ID from dual) T2
on (T1.CONCERN_ID = T2.CONCERN_ID and T1.BY_ID = T2.BY_ID)
when matched then update set T1.CONCERN_STATE = #{status}, T1.CONCERN_TIME = #{date}
when not matched then insert (ID, CONCERN_ID, BY_ID, CONCERN_STATE, CONCERN_TIME)
values (CONCERN_SOMETHING_ID_SEQ.nextval,#{uId},#{byId},#{status},#{date})
</insert>