Comments (2)
Service层封装即可。
from mybatis-spring.
mysql 存在更新 不存在插入 2010-06-12 18:17:02
分类: Mysql/postgreSQL
看程序竟然发现Mysql有这个功能!
今天写程序,新发现……………………,相当不错^_^,省略了很多功夫,每天1G多的日志!!
MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDATE语法,使得原本需要执行3条SQL语句(SELECT,INSERT,UPDATE),缩减为1条语句即可完成。
INSERT ... ON DUPLICATE KEY UPDATE,当插入的记录会引发主键冲突或者违反唯一约束时,则使用UPDATE更新旧的记录,否则插入新记录。
例如ipstats表结构如下:
CREATE TABLE ipstats (
ip VARCHAR(15) NOT NULL UNIQUE,
clicks SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0'
);
原本需要执行3条SQL语句,如下:
IF (SELECT * FROM ipstats WHERE ip='192.168.0.1') {
UPDATE ipstats SET clicks=clicks+1 WHERE ip='192.168.0.1';
} else {
INSERT INTO ipstats (ip, clicks) VALUES ('192.168.0.1', 1);
}
而现在只需下面1条SQL语句即可完成:
INSERT INTO ipstats VALUES('192.168.0.1', 1) ON DUPLICATE KEY UPDATE clicks=clicks+1;
注意,要使用这条语句,前提条件是这个表必须有一个唯一索引或主键。
from mybatis-spring.
Related Issues (20)
- 类转化异常 HOT 1
- 新增后 返回主键 要怎么配置呢 HOT 1
- 可以配置多数据源吗? HOT 4
- 查询时字段大小写问题 HOT 4
- 请问下,我怎么运行tomcat报错.. HOT 1
- toString HOT 1
- 使用mapper的insert报错 HOT 5
- pagehelper + tk.mapper HOT 1
- mybatis+spring整合,由于版本的问题会报错 HOT 3
- springBoot1.5.9+pagehelper5.0每页显示数量居然等于总记录数?! HOT 1
- 数据源为postgresql, oracle, mariadb时, helperDialect分别填什么? HOT 1
- 在我本地运行的时候发现当前版本的两个问题 HOT 1
- 想请教一个generator的问题,望解答
- selectByExampleAndRowBounds方法bug
- 《MyBatis从入门到精通》(刘增辉)一书中,第159页有错误。
- Example 是不是只支持嵌套1层SQL HOT 1
- 你好,请问支持spring3.0.5和MyBatis3.0.5吗? HOT 2
- deleteByExample,updateByExample的坑 HOT 2
- error creating bean with name 'sqlsessionfactory' defined in class path resource
- 同学,您这个项目引入了23个开源组件,存在69个漏洞,辛苦升级一下
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mybatis-spring.