问题描述
koa+mysql,如何实现批量更新,批量插入,
已实现的批量删除
sql
// 根据id单个删除
let cartDelectById = (id) => {
let sql = `delete from cart where cart_id = ${id};`
return query(sql)
}
// 根据id多个删除
let cartDelectByIds = (ids) => {
let sql = `delete from cart where cart_id in (${ids});`
return query(sql)
}
koa
// 单个删除
const cartDelectById = async(ctx) => {
let id = 1;
await cartModel.cartDelectById([id])
.then(result => {
ctx.body = {
state: 200,
msg: "删除购物车成功",
data: result
}
})
.catch(error => {
console.log(error);
ctx.body = false;
})
}
// 多个删除
const cartDelectByIds = async(ctx) => {
let ids = [1, 2, 3];
await cartModel.cartDelectByIds(ids)
.then(result => {
ctx.body = {
state: 200,
msg: "删除购物车成功",
data: result
}
})
.catch(error => {
console.log(error);
ctx.body = false;
})
}
单条sql成功插入和更新,请问如何实现批量更新,批量插入
// 插入
let cartInsertInto = (values) => {
let sql = `insert into cart values(default,?,?,?,?,?);`
return query(sql, values)
}
// 更新
let cartUpdate = (values) => {
let sql = `update cart set user_id=?,goods_id=?,cart_goods_number=? where cart_id=?;`
return query(sql)
}
插入
const cartInsertInto = async(ctx) => {
console.log(ctx.request.body)
let goodsId = ctx.request.body.goodsId;
let cartGoodsNumber = ctx.request.body.goodsNumber;
let userId = ctx.request.body.userId;
await cartModel.cartInsertInto([userId, goodsId, cartGoodsNumber, new Date(), new Date()])
.then(result => {
ctx.body = {
state: 200,
msg: "插入购物车成功",
data: result
}
})
.catch(error => {
console.log(error);
ctx.body = false;
})
}