mybatis mysql 有則更新,無則插入

2021-09-30 20:12:19 字數 1920 閱讀 5514

建立表

在這裡插入drop table

if exists `virtualmachines`;

create table `virtualmachines` (

name varchar (100) not null comment '虛擬機器名稱',

status varchar (100) default null comment '虛擬機器狀態',

state varchar (100) default null comment '虛擬機器開機狀態',

hostcpu bigint (20) default null comment 'cpu',

hostmemory bigint (20) default null comment '記憶體',

provisionedspace bigint (20) default null comment '提供的空間',

usedspace bigint (20) default null comment '已用空間',

ip bigint (20) default null comment 'ip位址',

isftpinstalled tinyint(4) default '0' comment '是否安裝ftp 0:未安裝 1:已安裝',

primary key (name)

) engine = innodb default charset = utf8 comment='虛擬機器表';**片

insert into virtualmachines

( `name`,`status`,`state`,`hostcpu`,`hostmemory`,`provisionedspace`,`usedspace`,`ip`,`isftpinstalled`

) values

( #,

#,#,

#,#,

#,#,

#,# )

on duplicate key update

status =values(status),

state = values(state),

hostcpu = values(hostcpu),

hostmemory = values(hostmemory),

provisionedspace = values(provisionedspace),

usedspace = values(usedspace),

ip = values(ip),

isftpinstalled = values(isftpinstalled)

insert into virtualmachines

( `name`,`status`,`state`,`hostcpu`,`hostmemory`,`provisionedspace`,`usedspace`,`ip`,`isftpinstalled`

) values

( #,

#,#,

#,#,

#,#,#,#

)on duplicate key update

status =values(status),

state = values(state),

hostcpu = values(hostcpu),

hostmemory = values(hostmemory),

provisionedspace = values(provisionedspace),

usedspace = values(usedspace),

ip = values(ip),

isftpinstalled = values(isftpinstalled)

MySql實現無則插入有則更新的解決方案

某些場景會有這樣的需求 無記錄則插入,有記錄則更新。例如 新增使用者,以身份證號碼作為唯一身份標識,插入時若先查詢是否存在記錄再決定插入還是更新,在高併發情況下必然存在問題。本文提供三種解決方案。無論通過synchronized鎖 reentranlock鎖還是分布式鎖,都可以解決該問題。缺點是,加...

mysql有則更新 批量 mysql 批量更新

private function parseupdate data,field,table sql update set keys array keys current data print r keys die foreach keys as column 如何用一條sql語句實現批量更新?mys...

Spring Bean的注入有更簡單的方式嗎?

需要更多相關資料的小可愛們,請看主頁中的個人簡介哦!通過配置的方式注入,往往太麻煩,實際工作中一般不會採取麻煩的方式,因為時間就是金錢,時間決定你是否加班,那是否有更簡單的方式去注入呢?當然有!註解方式注入 對於bean的注入,除了使用xml配置以外,註解的配置簡化開發的速度,使程式看上去更加簡潔。...