温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

golang数据库的基本操作方法

发布时间:2021-06-23 10:47:41 来源:亿速云 阅读:451 作者:chen 栏目:大数据

这篇文章主要介绍“golang数据库的基本操作方法”,在日常操作中,相信很多人在golang数据库的基本操作方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”golang数据库的基本操作方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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()
    }
}()

到此,关于“golang数据库的基本操作方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI