创建连接引擎
package dao
import (
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
)
var (
Driver = "mysql"
DsName = "root:******@tcp(192.168.3.118:3306)/beego1?charset=utf8"
DB *xorm.Engine // 创建xorm引擎
DBErr error
)
func init() {
DB, DBErr = xorm.NewEngine(Driver, DsName)
if DBErr != nil {
log.Fatal(DBErr)
}
}
创建数据表model
package model
import "time"
const (
SEX_WOMEN = "W"
SEX_MAN = "M"
SEX_UNKNOW = "U"
)
type User struct {
Id int64 `xorm:"pk autoincr bigint(64)" form:"id" json:"id"`
Mobile string `xorm:"varchar(20)" form:"mobile" json:"mobile"`
Password string `xorm:"varchar(40)" form:"password" json:"-"`
Avatar string `xorm:"varchar(150)" form:"avatar" json:"avatar"`
Sex string `xorm:"varchar(2)" form:"sex" json:"sex"`
Nickname string `xorm:"varchar(20)" form:"nickname" json:"nickname"`
Salt string `xorm:"varchar(10)" form:"salt" json:"-"` // - 这个Field将不进行字段映射
Online int `xorm:"int(10)" form:"online" json:"online"` //是否在线
Token string `xorm:"varchar(40)" form:"token" json:"token"`
Memo string `xorm:"varchar(140)" form:"memo" json:"memo"`
Createat time.Time `xorm:"datetime" form:"createat" json:"createat"`
}
const (
CONCAT_CATE_USER = 0x01
CONCAT_CATE_COMUNITY = 0x02
)
//好友和群都存在这个表里面
//可根据具体业务做拆分
type Contact struct {
Id int64 `xorm:"pk autoincr bigint(20)" form:"id" json:"id"`
//谁的10000
Ownerid int64 `xorm:"bigint(20)" form:"ownerid" json:"ownerid"` // 记录是谁的
//对端,10001
Dstid int64 `xorm:"bigint(20)" form:"dstid" json:"dstid"` // 对端信息
//
Cate int `xorm:"int(11)" form:"cate" json:"cate"` // 什么类型
Memo string `xorm:"varchar(120)" form:"memo" json:"memo"` // 备注
//
Createat time.Time `xorm:"datetime" form:"createat" json:"createat"` // 创建时间
}
type Fim struct {
Id int64 `xorm:"pk autoincr bigint(20) index" from:"id" json:"id"`
Name string `xorm:"varchar(120)" form:"name" json:"name"`
Create time.Time `xorm:"datetime" form:"create" json:"create"`
}
数据迁移
func init() {
DB, DBErr = xorm.NewEngine(Driver, DsName)
if DBErr != nil {
log.Fatal(DBErr)
}
//DB.ShowSQL(true)
DB.SetMaxOpenConns(2)
// ----20210331----> 进行model 迁移
_ = DB.Sync2(
new(model.User),
new(model.Community),
new(model.Contact),
new(model.Fim),
)
log.Println("init database success")
}