MyBatis实现SaveOrUpdate

2021/01/03补充

有更好的实现,可以看我另一篇https://www.cnblogs.com/ydymz/p/14225759.html

 

例子

<insert id="saveOrUpdate" >
  <selectKey keyProperty="count" resultType="int" order="BEFORE">
    select count(*) from country where id = #{id}
  </selectKey>
  <if test="count > 0">
    update country 
    set countryname = #{countryname},countrycode = #{countrycode} 
    where id = #{id}
  </if>
  <if test="count==0">
    insert into country values(#{id},#{countryname},#{countrycode})
  </if>
</insert>

 

csdn上的代码,唯一必须注意的是 keyProperty="count" ,要在实体类dto里面有

<selectKey keyProperty="count" resultType="int" order="BEFORE">
    select count(*) from country where id = #{id}
  </selectKey>

 

posted @ 2018-12-13 17:51  lgp20151222  阅读(6546)  评论(0编辑  收藏  举报