打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MyBatis实现SaveOrUpdate

MyBatis实现SaveOrUpdate

这篇文章主要讲如何通过xml方式实现SaveOrUpdate,但是仍然建议在Service中实现。

例子

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

条件限制

根据不同的判断逻辑,会有所不同,就上面这个例子而言,就要求实体类中包含count属性(可以是别的名字)。否则selectKey的结果没法保存,如果入参是个Map类型,就没有这个限制。

说明

从例子来看除了有个限制外,也没别的麻烦。

通过selectKey做第一次查询,然后根据结果进行判断,所以这里的order="BEFORE"是必须的。

也是因为BEFORE,所以没法通过<bind>标签来临时存储中间的值,只能在入参中增加属性来存放。

测试代码

//数据库中已经存在该ID,但是countryname=ChinaCountry country = new Country();country.setId(35);country.setCountryname("中国");country.setCountrycode("CN");//由于存在,这里会updateint result = countryMapper.saveOrUpdate(country);//查询结果,判断是否已经改变Country c2 = countryMapper.selectById(35);assertEquals("中国",c2.getCountryname());//id=300的不存在c2 = countryMapper.selectById(300);assertNull(c2);//将id=300country.setId(300);//由于id=300不存在,这里会Insertresult = countryMapper.saveOrUpdate(country);//查询结果c2 = countryMapper.selectById(300);assertNotNull(c2);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输出日志

DEBUG ==>  Preparing: select count(*) from country where id = ? DEBUG ==> Parameters: 35(Integer)TRACE <==    Columns: C1TRACE <==        Row: 1DEBUG <==      Total: 1DEBUG ==>  Preparing: update country set countryname = ?,countrycode = ? where id = ? DEBUG ==> Parameters: 中国(String), CN(String), 35(Integer)DEBUG <==    Updates: 1DEBUG ==>  Preparing: select * from country where id = ? DEBUG ==> Parameters: 35(Integer)TRACE <==    Columns: ID, COUNTRYNAME, COUNTRYCODETRACE <==        Row: 35, 中国, CNDEBUG <==      Total: 1DEBUG ==>  Preparing: select * from country where id = ? DEBUG ==> Parameters: 300(Integer)DEBUG <==      Total: 0DEBUG ==>  Preparing: select count(*) from country where id = ? DEBUG ==> Parameters: 300(Integer)TRACE <==    Columns: C1TRACE <==        Row: 0DEBUG <==      Total: 1DEBUG ==>  Preparing: insert into country values(?,?,?) DEBUG ==> Parameters: 300(Integer), 中国(String), CN(String)DEBUG <==    Updates: 1DEBUG ==>  Preparing: select * from country where id = ? DEBUG ==> Parameters: 300(Integer)TRACE <==    Columns: ID, COUNTRYNAME, COUNTRYCODETRACE <==        Row: 300, 中国, CNDEBUG <==      Total: 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

最后

这种方式只是利用了selectKey会多执行一次查询来实现的,但是如果你同时还需要通过selectKey获取序列或者自增的id,就会麻烦很多(oracle麻烦,其他支持自增的还是很容易)。

建议在复杂情况下,还是选择在Service中实现更好。

MyBatis工具:www.mybatis.tk

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MyBatis直接执行SQL的工具SqlMapper
mybatis 自动增长id保存后得到id
MySQL如何自动获取主键(MyBatis执行Insert操作返回自增主键)
myBatis3之SQL映射的XML文件(insert,update,delete 元素)
wiki/mapper3/3.Use.md · abel533 / Mapper
MyBatis ResultMap
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服