select device_id, company_id from watch_company insert into watch_company device_id, company_id, #{deviceId}, #{companyId}, INSERT INTO watch_company (device_id, company_id) SELECT device_id, company_id FROM ( SELECT #{watchCompany.deviceId,jdbcType=BIGINT} AS device_id, #{watchCompany.companyId,jdbcType=BIGINT} AS company_id ) AS new_records WHERE NOT EXISTS ( SELECT 1 FROM watch_company wc WHERE wc.device_id = new_records.device_id AND wc.company_id = new_records.company_id ) delete from watch_company where device_id = #{deviceId} and company_id in #{id}