如果❤️我的文章有帮助,欢迎点赞、关注。这是对我继续技术创作最大的鼓励。更多往期文章在我的个人博客
上一节讲完项目结构 & 生成数据表映射之后,接下来就是项目连接数据库操作数据库的代码封装:
连接数据库配置
数据库配置的主要目的就是为了把 Golang 链接数据库链接
“[User]:[Pwd]@tcp([Host]:[Port])/[DbName]?charset=[DbCharset]”
中的各项参数,变成结构体可标准配置
。比如:
type DbConf struct {
Host string
Port int
User string
Pwd string
DbName string
DbCharset string
}
conf/db.go
package conf
const DriverName = "mysql"
type DbConf struct {
Host string
Port int
User string
Pwd string
DbName string
}
var MasterDbConf DbConf = DbConf{
Host : "127.0.0.1",
Port : 3306,
User : "root",
Pwd : "",
DbName : "superstar",
}
var SlaveDbConf DbConf = DbConf{
Host : "127.0.0.1",
Port : 3306,
User : "root",
Pwd : "",
DbName : "superstar",
}
连接数据库方法封装
datasource/dbhelper.go
package datasource
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"superstar/conf"
"sync"
"xorm.io/xorm"
)
// 数据库单例 dbhelper.go
var (
masterEngine *xorm.Engine
slaveEngine *xorm.Engine
lock sync.Mutex
)
func InstanceMaster() *xorm.Engine {
if masterEngine != nil {
return masterEngine
}
lock.Lock()
defer lock.Unlock()
// 协程时,上面十个一起进来,每个都到 lock.Lock() 加锁挂起。这里避免 第一个加锁然后创建实例后,每个都创建实例
if masterEngine != nil {
return masterEngine
}
c := conf.MasterDbConf
driverSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
c.User, c.Pwd, c.Host, c.Port, c.DbName)
fmt.Println(driverSource)
masterEngine, err := xorm.NewEngine(conf.DriverName, driverSource)
if err != nil {
log.Fatal("dbhelper.DbInstanceMaster err:", err)
return nil
}
// DEBUG 模式,打印全部sql, 帮助对比 orm 与 sql 执行对照关系
masterEngine.ShowSQL(false)
masterEngine.SetTZLocation(conf.SysTimeLocation)
return masterEngine
}
func InstanceSlave() *xorm.Engine {
if slaveEngine != nil {
return slaveEngine
}
lock.Lock()
defer lock.Unlock()
// 协程时,上面十个一起进来,每个都到 lock.Lock() 加锁挂起。这里避免 第一个加锁然后创建实例后,每个都创建实例
if slaveEngine != nil {
return slaveEngine
}
c := conf.MasterDbConf
driverSource := fmt.Sprint("%s:%s@tcp(%s:%d)/%s?charset=utf8",
c.User, c.Pwd, c.Host, c.Port, c.DbName)
slaveEngine, err := xorm.NewEngine(conf.DriverName, driverSource)
if err != nil {
log.Fatal("dbhelper.DbInstanceMaster err:", err)
return nil
}
return slaveEngine
}
数据库操作 dao 层
dao/playerinfo_dao.go
package dao
// play_info 表数据操作层
import (
"log"
"superstar/models"
"xorm.io/xorm"
)
type PlayerinfoDao struct {
engine *xorm.Engine
}
func NewPlayerinfoDao(engine *xorm.Engine) *PlayerinfoDao {
return &PlayerinfoDao{
engine: engine,
}
}
// 获取单个
func (p PlayerinfoDao)Get(id int) *models.PlayerInfo {
data := &models.PlayerInfo{Id:id}
ok, err := p.engine.Get(data)
if ok && err == nil {
return data
} else {
data.Id = 0
return data
}
}
// 获取多个
func (p PlayerinfoDao)GetAll() []models.PlayerInfo {
datalist := []models.PlayerInfo{}
err := p.engine.Desc("id").Find(&datalist)
if err != nil {
log.Println(err)
return nil
} else {
return datalist
}
}
// 关键字查询
func (d PlayerinfoDao) Search(country string) []models.PlayerInfo {
// datalist := make([]models.StarInfo, 0)
datalist := []models.PlayerInfo{}
err := d.engine.Where("country=?", country).Desc("id").Find(&datalist)
if err != nil {
log.Println(err)
return nil
} else {
return datalist
}
}
// 软删除
func (d PlayerinfoDao) DeleteByID(id int) error {
data := &models.PlayerInfo{Id: id, PlayStatus: 1}
_, err := d.engine.ID(id).Update(data)
return err
}
// 修改
func (d PlayerinfoDao) Update(data *models.PlayerInfo, columns []string) error {
_, err := d.engine.MustCols(columns...).ID(data.Id).Update(data)
return err
}
// 新增
func (d PlayerinfoDao) Create(data *models.PlayerInfo) error {
_, err := d.engine.Insert(data)
return err
}