12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package database
- import (
- "gorm.io/driver/mysql"
- "gorm.io/gen"
- "gorm.io/gorm"
- "os"
- )
- func generateGormDbModel(dsn string, daoPath string) {
- currentPath, _ := os.Getwd()
- db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
- genConfig := gen.Config{
- OutPath: currentPath + "/dao" + daoPath + "/query",
- ModelPkgPath: currentPath + "/dao" + daoPath + "/model",
- Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface,
-
- FieldNullable: false,
-
-
-
-
-
-
-
-
-
-
-
-
- }
- g := gen.NewGenerator(genConfig)
- g.WithImportPkgPath("github.com/shopspring/decimal")
- g.UseDB(db)
-
-
- dataMap := map[string]func(detailType gorm.ColumnType) (dataType string){
- "tinyint": func(detailType gorm.ColumnType) (dataType string) { return "int64" },
- "smallint": func(detailType gorm.ColumnType) (dataType string) { return "int64" },
- "mediumint": func(detailType gorm.ColumnType) (dataType string) { return "int64" },
- "bigint": func(detailType gorm.ColumnType) (dataType string) { return "int64" },
- "int": func(detailType gorm.ColumnType) (dataType string) { return "int64" },
- "decimal": func(detailType gorm.ColumnType) (dataType string) { return "decimal.Decimal" },
- }
- g.WithDataTypeMap(dataMap)
-
- g.WithOpts(gen.FieldRename("table", "MTable"))
- g.GenerateAllTable()
- g.ApplyBasic(g.GenerateAllTable()...)
- g.Execute()
- }
|