提交
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package gormx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func NewGorm(prefix, user, password, host, database string, port int) *gorm.DB {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)
|
||||
|
||||
g, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: prefix, // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||
SingularTable: true, // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||
},
|
||||
Logger: NewGormxLogger(context.TODO()),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
Reference in New Issue
Block a user