1.测试表准备
-- id非自增
CREATE TABLE `test_idms` (
`id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB
;
-- id自增
CREATE TABLE `test_idms_sub` (
`id` int NOT NULL AUTO_INCREMENT,
`sub_name` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
;
2.数据插入操作
四种自增主键单条插入方式
//1.1 方法一
body = {
"sub_name":"1.1测试"
}
var id = db.table("test_idms_sub").insert(body);
//这能返回新增的自增id
log.info("id1.1={}",id)
//1.2 方法二
id = db.insert("insert into test_idms_sub(sub_name) values('1.2测试')");
log.info("id1.2={}",id)
//1.3 方法三
body = {
"sub_name":"1.3测试"
}
id = db.table('test_idms_sub').primary('id').save(body);
log.info("id1.3={}",id)
//1.4 方法四
body = {
"sub_name":"1.4测试"
}
id = db.table("test_idms_sub").primary("id").insert(body);
log.info("id1.4={}",id)
//总结:返回值是id
四种非自增主键插入方式
//2.1 方法一
body = {
"name":"2.1测试"
}
id = db.table("test_idms").primary("id",uuid()).insert(body);
log.info("id2.1={}",id)
//2.2 方法二
body = {
"name":"2.2测试"
}
id = db.table("test_idms").primary("id",uuid()).save(body); //save(body,true) 对应的upsert方式
log.info("id2.2={}",id)
//2.3 方法三
id = db.table("test_idms").primary("id",uuid()).insert(body);
log.info("id2.3={}",id)
//2.4 方法四
id = db.insert("insert into test_idms(id,name) values('"+uuid()+"','2.4测试')");
log.info("id2.4={}",id)
//总结:返回值null
批量插入方式
body=[
{
"id":uuid(),
"name":"3.1批量"
},
{
"id":uuid(),
"name":"3.2批量"
}
]
id = db.table("test_idms").batchInsert(body);
//返回的是插入的记录数
log.info("id3={}",id)
return id
标签:body,Magic,name,idms,数据库,Api,test,id,sub
From: https://blog.csdn.net/weixin_43966292/article/details/141180877