123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package database
- import (
- "bytes"
- "duoduo/conf"
- "fmt"
- _ "github.com/go-sql-driver/mysql" //加载mysql
- "github.com/jinzhu/gorm"
- "log"
- "strconv"
- )
- var Eloquent *gorm.DB
- var LyMysql *gorm.DB
- var BzMysql *gorm.DB
- var (
- Host string
- Port int
- Name string
- Username string
- Password string
- )
- func Setup() {
- //加载配置
- confIni, errConf := conf.ConnIni()
- if errConf != nil {
- fmt.Println(errConf)
- }
- Host = confIni.MustValue("mysql", "mysql_host")
- Port = confIni.MustInt("mysql", "mysql_port")
- Name = confIni.MustValue("mysql", "mysql_name")
- Username = confIni.MustValue("mysql", "mysql_username")
- Password = confIni.MustValue("mysql", "mysql_password")
- var err error
- conn := GetMysqlConnect()
- log.Println(conn)
- var db Database
- db = new(Mysql)
- Eloquent, err = db.Open("mysql", conn)
- if err != nil {
- log.Fatalf("mysql connect error %v", err)
- } else {
- log.Printf("mysql connect success!")
- }
- if Eloquent.Error != nil {
- log.Fatalf("database error %v", Eloquent.Error)
- }
- Eloquent.LogMode(true)
- //加载领养数据库链接
- Host = confIni.MustValue("ly-mysql", "mysql_host")
- Port = confIni.MustInt("ly-mysql", "mysql_port")
- Name = confIni.MustValue("ly-mysql", "mysql_name")
- Username = confIni.MustValue("ly-mysql", "mysql_username")
- Password = confIni.MustValue("ly-mysql", "mysql_password")
- conn = GetMysqlConnect()
- log.Println(conn)
- //var db Database
- db = new(Mysql)
- LyMysql, err = db.Open("mysql", conn)
- if err != nil {
- log.Fatalf("mysql connect error %v", err)
- } else {
- log.Printf("mysql connect success!")
- }
- if LyMysql.Error != nil {
- log.Fatalf("database error %v", Eloquent.Error)
- }
- LyMysql.LogMode(true)
- //加载壁纸数据库链接
- Host = confIni.MustValue("bz-mysql", "mysql_host")
- Port = confIni.MustInt("bz-mysql", "mysql_port")
- Name = confIni.MustValue("bz-mysql", "mysql_name")
- Username = confIni.MustValue("bz-mysql", "mysql_username")
- Password = confIni.MustValue("bz-mysql", "mysql_password")
- conn = GetMysqlConnect()
- log.Println(conn)
- //var db Database
- db = new(Mysql)
- BzMysql, err = db.Open("mysql", conn)
- if err != nil {
- log.Fatalf("mysql connect error %v", err)
- } else {
- log.Printf("mysql connect success!")
- }
- if BzMysql.Error != nil {
- log.Fatalf("database error %v", Eloquent.Error)
- }
- sql := BzMysql.DB()
- sql.SetMaxOpenConns(100) //设置数据库连接池最大连接数
- sql.SetMaxIdleConns(20)
- BzMysql.LogMode(true)
- }
- func GetMysqlConnect() string {
- var conn bytes.Buffer
- conn.WriteString(Username)
- conn.WriteString(":")
- conn.WriteString(Password)
- conn.WriteString("@tcp(")
- conn.WriteString(Host)
- conn.WriteString(":")
- conn.WriteString(strconv.Itoa(Port))
- conn.WriteString(")")
- conn.WriteString("/")
- conn.WriteString(Name)
- conn.WriteString("?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms")
- return conn.String()
- }
- type Database interface {
- Open(dbType string, conn string) (db *gorm.DB, err error)
- }
- type Mysql struct {
- }
- func (*Mysql) Open(dbType string, conn string) (db *gorm.DB, err error) {
- eloquent, err := gorm.Open(dbType, conn)
- return eloquent, err
- }
|