| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- package models
- import (
- "fmt"
- "git.wanbits.cc/sin/flytalk/config"
- "github.com/jinzhu/gorm"
- "time"
- )
- var DB *gorm.DB
- type Model struct {
- ID uint `gorm:"primary_key" json:"id"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- DeletedAt *time.Time `sql:"index" json:"deleted_at"`
- }
- func init() {
- mysql := config.CreateMysql(config.MysqlConf)
- dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Db)
- var err error
- DB, err = gorm.Open("mysql", dsn)
- if err != nil {
- panic("数据库连接失败!")
- }
- DB.SingularTable(true)
- DB.LogMode(false)
- //DB.SetLogger(tools.Logger())
- DB.DB().SetMaxIdleConns(10)
- DB.DB().SetMaxOpenConns(100)
- InitConfig()
- }
- func Execute(sql string) {
- DB.Exec(sql)
- }
- func CloseDB() {
- defer DB.Close()
- }
|