golang操作數據庫一般使用開源項目gorm,該項目擁有15000多star,功能較全面。
簡單增刪改查
類似於java的hibernate將數據封裝到結構體(java到對象)中進行操作
package models
import (
"fmt"
"github.com/astaxie/beego"
"github.com/jinzhu/gorm"
"riskcontrol/database"
"riskcontrol/utils/snowflake"
"time"
)
const(
score = "user_score"
)
type ScoreModel struct {
BaseModel
UserId string `json:"user_id",gorm:"type:varchar(18);not null;comment:'用戶id';"`
Score int32 `json:"score",gorm:"type:int;null;comment:'分數'"`
BindFacebook bool `json:"bind_facebook",gorm:"type:boolean;not null;comment:'中文名稱'"`
//IsValid bool `json:"-",gorm:"type:boolean;not null;default:1;comment:'是否有效(0:無效,1:有效)'"`
}
func init() {
database.GetDB().AutoMigrate(&ScoreModel{})
}
/**
設置匹配的表名稱
*/
func (ScoreModel) TableName() string {
return score
}
func (userMode *ScoreModel)BeforeCreate (scope *gorm.Scope) error {
id,err := snowflake.NewSnowFlake().Generate()
if err != nil{
beego.Error("snowflake get id error",err)
return err
}
currentTime := time.Now()
scope.SetColumn("Id", id )
scope.SetColumn("CreateTime",currentTime)
scope.SetColumn("UpdateTime",currentTime)
return nil
}
func (userMode *ScoreModel)BeforeUpdate (scope *gorm.Scope) error {
currentTime := time.Now()
scope.SetColumn("UpdateTime",currentTime)
return nil
}
func GetScoreByUserId(userId string) (ScoreModel,error) {
var userScore ScoreModel
err := database.GetDB().Where(&ScoreModel{UserId:userId}).Last(&userScore).Error
return userScore,err
}
func InsertScore(userId string,score int32,bindFacebook bool,appId string) bool {
scoreModel := ScoreModel{UserId:userId,Score:score,BindFacebook:bindFacebook}
scoreModel.AppId = appId
if err := database.GetDB().Create(&scoreModel).Error;err != nil{
beego.Error("AddScore error",err)
return false
}
database.GetDB().NewRecord(scoreModel)
return true
}
func UpdateScore(id int64, addScore int32,bindFacebook bool ) bool {
ScoreModel := ScoreModel{Score:addScore,BindFacebook:bindFacebook}
ScoreModel.ID = id
database.GetDB().Model(&ScoreModel).Update(&ScoreModel)
return false
}
自定義sql
更新
func UpdateScore(id int64, addScore int32,bindFacebook bool ) bool {
sql := fmt.Sprintf("UPDATE user_score SET score = score + %d, bind_facebook = %t,update_time=now() where id = %d",
addScore,bindFacebook,id)
res := database.GetDB().Exec( sql )
num:= res.RowsAffected
if num > 0{
return true
}
return false
}
查詢
func GetScoreHisByUserId(userId string,id string,up bool) ([]dto.ScoreHisDto,error) {
var scoreHis []dto.ScoreHisDto
partitionKey,_ := getPartitionKey()
keyList := []int{partitionKey,partitionKey -1,partitionKey -2}
sqlScript := " SELECT id, create_time ,remark,(score_after-score_before)/100 as add_score FROM score_his WHERE "
if id != "" {
if up == true {
sqlScript += fmt.Sprintf("id > %s and ",id)
}else{
sqlScript += fmt.Sprintf("id < %s and ",id)
}
}
database.GetDB().Raw(sqlScript + " user_id = ? and partition_key in (?) order by id desc limit 20 ",
userId, keyList ).Scan(&scoreHis)
var err error
return scoreHis,err
}
ScoreHisDto的結構體為
type ScoreHisDto struct {
Id string `json:"id"`
Remark string `json:"remark"`
CreateTime time.Time `json:"createTime"`
AddScore float32 `json:"addScore"`
}
注意,這里遇到一個坑,sql查詢出的字段create_time查詢出來后會被自動轉為駝峰式createTime;這里和java mybatis不一樣,mybatis在sql中需要把字段create_time設置別名createTime並映射到dto中到createTime上。
事務
var tx *gorm.DB //開啟事務 tx = database.GetDB().Begin() sql1 := "" tx.Exec( sql1 ) sql2 := "" tx.Exec( sql2 ) //提交事務 tx.Commit()
異常處理
由於go沒有try...catch...finally,在代碼段每一個return前可能都要回滾事務,這樣就很麻煩了。這時候可以使用golang到defer語句,該語句可在方法return前做一些操作,示例如下:
var err error
var tx *gorm.DB
//預定義方法 如果程序異常,則在方法return前回滾事務
defer func() {
if err != nil && tx != nil{
beego.Error(err, "tx rollback...")
tx.Rollback()
}
}()
參考文檔
