12345678910111213141516171819202122232425262728293031323334353637 |
- package orm
- import (
- "fmt"
- _ "github.com/denisenkom/go-mssqldb"
- "github.com/jmoiron/sqlx"
- "log"
- "lttc-go-adm-znaf/pkg/setting"
- )
- var (
- DB *sqlx.DB
- )
- // 初始化数据库链接
- func Setup() {
- var err error
- // 拼接数据库链接字符串
- dsn := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s",
- setting.DatabaseSetting.Server,
- setting.DatabaseSetting.UserId,
- setting.DatabaseSetting.Password,
- setting.DatabaseSetting.Port,
- setting.DatabaseSetting.DataBase)
- // 链接数据库
- DB, err = sqlx.Connect("sqlserver", dsn)
- if err != nil {
- log.Fatalf("orm.setup err: %v", err)
- }
- }
- // 关闭数据库链接
- func Close() {
- _ = DB.Close()
- }
|