자라는 개발자/시행착오들
on duplicate key 를 써서 없으면 insert 있으면 update 하기
자란다
2020. 12. 2. 17:04
728x90
반응형
on duplicate key 를 써서 없으면 insert 있으면 update 하기
코딩을 하다보니 DB에 있으면 update 없으면 insert를 해야 할일이 생겼다.
on duplicate key 를 모를때는 select count() .... 해와서 0이 아닐때 작업을 했는데,
이 방법을 알고 나니 신세계였다.
insert 문 + on duplicate key + update문 으로 보면된다.
DAO에서 넘겨줄때나 xml 파일에서 넘길때 insert도 되고 update 도 되서 뭐로할지 고민했는데, update 하니까 잘된다.
<update id="---------------" parameterType="map">
/* --------- */
insert into ---------( AA,BB,CC)
values(#{param1},#{param2},#{param3})
on duplicate key update CC = #{param3}
</update>
728x90
반응형