假设我们的数据结构是下面这样的
type Goods struct {
Id int64 `json:"id"`
GoodsId int64 `json:"goods_id"`
GoodsName string `json:"goods_name"`
Price int64 `json:"price"`
CreateTime time.Time `json:"create_time"`
UpdateTime time.Time `json:"create_time"`
IsDeleted int `json:"-"`
}
一、新增数据
engine := db.GetEngine()
has, err := engine.Exist(&Goods{
GoodsId: "11011",
})
if has {
return ierror.NewError(201, "该条记录已存在")
}
var goods Goods
goods = Goods{
GoodsId: goodsId,
Price: price,
CreateTime: time.Now(),
UpdateTime: time.Now(),
IsDeleted: 0,
}
_, err = engine.Insert(&goods)
二、修改数据
if !has {
return ierror.NewError(201, "该条记录不存在")
}
举例:update goods表 set price=168 where goods_id = 11011
var goods Goods
goods = Goods{
Price: "168",
UpdateTime: time.Now(),
}
_, err = engine.Where(builder.Eq{
"goods_id": "11011",
}).Update(&goods)
三、删除数据