在实际项目开发过程中,经常会遇到某种业务,根据某个字段判断数据是否存在,若不存在则进行insert操作,若存在则进行根据这个字段update操作:

<insert id="insertProject" parameterType="com.lenovo.mcmp.rm.etl.model.param.resourceConfig.CreateResourceConfigParam">
INSERT INTO project(project_code,project_name,start_time,age)
VALUES(#{project_code},#{project_name},'2018-01-10 22:00:00',24)
ON conflict(project_code) 
DO UPDATE SET project_name= #{project_name}, start_time='2018-02-22 12:00:00'
</insert>

也可以根据多个字段判断是否已存在该条数据:

<insert id="insertProject" parameterType="com.lenovo.mcmp.rm.etl.model.param.resourceConfig.CreateResourceConfigParam">
INSERT INTO project(project_code,project_name,start_time,age)
VALUES(#{project_code},#{project_name},'2018-01-10 22:00:00',24)
ON conflict(project_code,age) 
DO UPDATE SET project_name= #{project_name}, start_time='2018-02-22 12:00:00'
</insert>

更多推荐