mybatis更新
- 作者: 奔跑的宅猪
- 来源: 51数据库
- 2020-09-20
// 对象
public class Entity {
private String id = null;
private int value = null;
private Timestamp createTime = null;
// getter and setter ...
}
// 数据表
CREATE TABLE qai_entity (
id CHAR(10) NOT NULL,
value INT NOT NULL DEFAULT 0,
create_time TIMESTAMP NOT NULL,
PRIMARY KEY (id)
)
// MyBatis 配置
mysql没有提供直接的方法来实现批量,但可以使用case when语法来实现这个功能。
update course
set name = case id
when 1 then 'name1'
when 2 then 'name2'
when 3 then 'name3'
end,
title = case id
when 1 then 'new title 1'
when 2 then 'new title 2'
when 3 then 'new title 3'
end
where id in (1,2,3)
public class Entity {
private String id = null;
private int value = null;
private Timestamp createTime = null;
// getter and setter ...
}
// 数据表
CREATE TABLE qai_entity (
id CHAR(10) NOT NULL,
value INT NOT NULL DEFAULT 0,
create_time TIMESTAMP NOT NULL,
PRIMARY KEY (id)
)
// MyBatis 配置
mysql没有提供直接的方法来实现批量,但可以使用case when语法来实现这个功能。
update course
set name = case id
when 1 then 'name1'
when 2 then 'name2'
when 3 then 'name3'
end,
title = case id
when 1 then 'new title 1'
when 2 then 'new title 2'
when 3 then 'new title 3'
end
where id in (1,2,3)
推荐阅读
