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>
条件限制
根据不同的判断逻辑,会有所不同,就上面这个例子而言,就要求实体类中包含count属性(可以是别的名字)。否则selectKey的结果没法保存,如果入参是个Map类型,就没有这个限制。
说明
从例子来看除了有个限制外,也没别的麻烦。
通过selectKey做第一次查询,然后根据结果进行判断,所以这里的order="BEFORE"是必须的。
也是因为BEFORE,所以没法通过<bind>标签来临时存储中间的值,只能在入参中增加属性来存放。
测试代码
//数据库中已经存在该ID,但是countryname=China
Country country = new Country();
country.setId(35);
country.setCountryname("中国");
country.setCountrycode("CN");
//由于存在,这里会update
int result = countryMapper.saveOrUpdate(country); //查询结果,判断是否已经改变
Country c2 = countryMapper.selectById(35);
assertEquals("中国",c2.getCountryname()); //id=300的不存在
c2 = countryMapper.selectById(300);
assertNull(c2); //将id=300
country.setId(300);
//由于id=300不存在,这里会Insert
result = countryMapper.saveOrUpdate(country); //查询结果
c2 = countryMapper.selectById(300);
assertNotNull(c2);
输出日志
DEBUG ==> Preparing: select count(*) from country where id = ?
DEBUG ==> Parameters: 35(Integer)
TRACE <== Columns: C1
TRACE <== Row: 1
DEBUG <== Total: 1
DEBUG ==> Preparing: update country set countryname = ?,countrycode = ? where id = ?
DEBUG ==> Parameters: 中国(String), CN(String), 35(Integer)
DEBUG <== Updates: 1
DEBUG ==> Preparing: select * from country where id = ?
DEBUG ==> Parameters: 35(Integer)
TRACE <== Columns: ID, COUNTRYNAME, COUNTRYCODE
TRACE <== Row: 35, 中国, CN
DEBUG <== Total: 1
DEBUG ==> Preparing: select * from country where id = ?
DEBUG ==> Parameters: 300(Integer)
DEBUG <== Total: 0
DEBUG ==> Preparing: select count(*) from country where id = ?
DEBUG ==> Parameters: 300(Integer)
TRACE <== Columns: C1
TRACE <== Row: 0
DEBUG <== Total: 1
DEBUG ==> Preparing: insert into country values(?,?,?)
DEBUG ==> Parameters: 300(Integer), 中国(String), CN(String)
DEBUG <== Updates: 1
DEBUG ==> Preparing: select * from country where id = ?
DEBUG ==> Parameters: 300(Integer)
TRACE <== Columns: ID, COUNTRYNAME, COUNTRYCODE
TRACE <== Row: 300, 中国, CN
DEBUG <== Total: 1
最后
这种方式只是利用了selectKey会多执行一次查询来实现的,但是如果你同时还需要通过selectKey获取序列或者自增的id,就会麻烦很多(Oracle麻烦,其他支持自增的还是很容易)。
建议在复杂情况下,还是选择在Service中实现更好。
MyBatis工具:www.mybatis.tk
http://blog.csdn.net/isea533/article/details/45578415
MyBatis实现SaveOrUpdate的更多相关文章
- MyBatis 配置/注解 SQL CRUD 经典解决方案(2019.08.15持续更新)
本文旨在记录使用各位大神的经典解决方案. 2019.08.14 更新 Mybatis saveOrUpdate SelectKey非主键的使用 MyBatis实现SaveOrUpdate mybati ...
- 集成框架 javaweb开发平台ssmy_m(生成代码) java struts2 mybatis spring maven jquery
网页地址 http://blog.csdn.net/lpy3654321/article/details/31841573 项目设想,在项目开发中,我们的开发者大多数时间都在反复开发 相同的keywo ...
- 从JDBC到hibernate再到mybatis之路
一.传统的JDBC编程 在java开发中,以前都是通过JDBC(Java Data Base Connectivity)与数据库打交道的,至少在ORM(Object Relational Mappin ...
- MySQL保存或更新 saveOrUpdate
1. 引子 在项目开发过程中,有一些数据在写入时候,若已经存在,则覆盖即可.这样可以防止多次重复写入唯一键冲突报错.下面先给出两个MyBatis配置文件中使用saveOrUpdate的示例 <! ...
- jdbc、Mybatis、Hibernate介绍(非原创)
文章大纲 一.jdbc介绍二.Mybatis介绍三.Hibernate介绍四.jdbc.Mybatis.Hibernate比较五.参考文章 一.jdbc介绍 1. jdbc编程步骤 (1)加载数据 ...
- MyBatis从入门到精通(第9章):Spring集成MyBatis(下)
MyBatis从入门到精通(第9章):Spring集成MyBatis(下) springmvc执行流程原理 mybatis-spring 可以帮助我们将MyBatis代码无缝整合到Spring中.使 ...
- Spring Boot demo系列(三):Spring Web+MyBatis Plus
2021.2.24 更新 1 概述 Spring Web+MyBatis Plus的一个Demo,内容和上一篇类似,因此重点放在MyBatis Plus这里. 2 dao层 MyBatis Plus相 ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
随机推荐
- Node.js 学习(二) 创建第一个应用
如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟
题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...
- 链路层三种类型的MAC地址
若需要转载,请注明出处. 我们知道,链路层都是以MAC地址来进行通信双方的地址标识的,如下图:在应用中根据接收方的多寡来进行划分,可分为以下三种: 单播(Unicast) 多播(Multicast) ...
- 【BZOJ】【1597】【USACO 2008 Mar】土地购买
DP/斜率优化 Orz Hzwer…… 想到排序了,但没想到其实可以将序列转化为x递增且y递减的序列……因为x是递增的,若y[i]>y[i-1]那么第i-1个就足够小……以至于可以在搞定第 i ...
- nsight 使用问题
slot 0 offset 0 stride DXGI_FORMAT_r32b32g32_FLOAT 这样一个memory 100.0000, 100.0000,10.0000,1.0000 stri ...
- C#索引器及示例
public class IndexSeletor<T> where T:struct { private List<T> _listObj; public IndexSele ...
- LA 2031
Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his danc ...
- hdu 4725
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- POJ 3281
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8577 Accepted: 3991 Descriptio ...