mybatis+oracle insert 判断

业务需求:需要在插入数据库之前判断该数据是否已经存在数据库中,如果存在则进行update操作,如果不存在则进行insert操作
使用语句:merge into ser_layout_ccic_person a
using (select '证件号' certificate_code,
              '身份类型' certificate_type,
              'test' name
         from dual) b
on (a.certificate_code = b.certificate_code and a.certificate_type=b.certificate_type)
when matched then
  update set a.name = b.name
           
when not matched then
  insert
    (a.certificate_code, a.certificate_type, a.name)
  values
    (b.certificate_code, b.certificate_type, b.name)
mybatis配置:<insert id="insertPersonOrUpdate" parameterType="PersonInfo">
merge into <include refid="ac.person"/> a
using (select #{certificateCode} certificate_code from dual) b
on (a.certificate_code = b.certificate_code)
when matched then
  update set a.name = #{name},
             a.sex=#{sex},
             a.birthday=#{birthday},
             a.native_area_code=#{nativeAreaCode},
             a.address=#{address},
             a.mobile=#{mobile},
             a.used_name=#{usedName}
            
when not matched then
  insert
    (a.certificate_code, a.certificate_type, a.name,a.used_name,a.sex,a.birthday,a.native_area_code,a.address,a.mobile,a.type,a.insert_flag)
  values
    (#{certificateCode},#{certificateType},#{name},#{usedName},#{sex},#{birthday},#{nativeAreaCode},#{address},#{mobile},#{type},#{insertFlag})


</insert>

猜你喜欢

转载自wzxqiang900.iteye.com/blog/2307498