Home > other >  MySql database table transformation Go Struct (MySql to model)
MySql database table transformation Go Struct (MySql to model)

Time:10-08

# project address
https://github.com/xianghu1314/mtm

# MTM project profileMySql database table transformation Go Struct (MySql to model) (MTM)
A large project, generally adopts the design of the database first method, the tool good realized from the database to Go Struct code conversion, reduced the amount of time manually write Struct,
# to use
# # # # get
Go get github.com/xianghu1314/mtm
# # # # call
` ` ` go
//model transformation
T2s:=MTM. CreateTableToStruct (& amp; MTM. Options {
MySqlUrl: "XXX",
FileName: "Models. Go,"
IfOneFile: true,
PackageName: "Models",
SavePath: "./Models ",
IfToHump: true,
IfJsonTag: true,
IfPluralToSingular: true,
IfCapitalizeFirstLetter: true,
})
Err:=t2s. The Run ()
if err ! Nil={
The Fatal (" model transformation: "+ err. Error ())
}
` ` `
# # # # parameters description
` ` ` go
Type Options struct {
MySqlUrl string/address/database DSN (Data Source Name) : [username [: password] @] [protocol [(address)]]/dbname [? Param1=value1 & amp;... & amp; paramN=valueN]
IfOneFile bool//multiple tables are in the same file true=the same default false
FileName string//FileName when IfOneFile=true effective when the default Models. Go
PackageName the string//custom project package name default Models
SavePath string/default/save folder./Models
IfToHump bool true=//whether the switch the hump is false by default
IfJsonTag bool//contains json tag=true is false by default
IfPluralToSingular bool//whether the plural the singular true=is false by default
IfCapitalizeFirstLetter bool/whether/initials conversion capitals true=is false by default
}
` ` `
# # # # to generate sample
` ` `
The CREATE TABLE ` back_users ` (
` id ` int (11), NOT NULL AUTO_INCREMENT,
` name ` varchar (50), NOT NULL COMMENT 'real name'
` account ` varchar (50), NOT NULL COMMENT 'account',
` PWD ` varchar (50) CHARACTER SET ASCII NOT NULL COMMENT 'password',
The PRIMARY KEY (` id `)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
The CREATE TABLE ` entry_forms ` (
` id ` int (11), NOT NULL AUTO_INCREMENT,
` user_id ` int (11) NOT NULL,
` tournament_id ` int (11) NOT NULL,
` name ` varchar (50), NOT NULL COMMENT 'name',
` id_card ` varchar (19) CHARACTER SET ASCII NOT NULL COMMENT 'id 18 + x',
` height ` double (3, 2) NOT NULL COMMENT 'height, the unit cm,
` weight ` double (3, 2) NOT NULL COMMENT 'weight, the unit kg,
` age ` tinyint (3) NOT NULL,
` blood_type ` varchar (2) NOT NULL COMMENT 'type'
` phone ` char (11), NOT NULL COMMENT 'contact number',
` emergency_phone ` char (11), NOT NULL COMMENT 'emergency contact telephone,
` gender ` enum (' male 'and' getting ') NOT NULL,
` status ` tinyint (4) NOT NULL COMMENT '0, 1 as the default withdrawal, 1 to receive packages, register at 2',
` pay_status ` bit (1) NOT NULL,
` pay_log ` varchar (500) NOT NULL,
` amount ` decimal (65, 0) NOT NULL,
` prepay_id ` varchar (50), NOT NULL COMMENT 'pay with id',
` create_at ` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (` id `),
The KEY ` fk_entryForms_tournaments_1 ` (` tournament_id `),
The KEY ` fk_entry_forms_users_1 ` (` user_id `),
The CONSTRAINT ` fk_entryForms_tournaments_1 ` FOREIGN KEY (` tournament_id `) REFERENCES ` tournaments ` (` id `)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
` ` `
` ` ` go
Package Models

Import (
"Time"
)

Type BackUser struct {
Id int ` json: "Id" `//
The Name string ` json: "Name" `//real Name
The Account string ` json: "Account" `//Account
The Pwd string ` json: "Pwd" `//password
}
Type EntryForm struct {
Id int ` json: "Id" `//
UserId int ` json: "UserId" `//
TournamentId int ` json: "TournamentId `//
"The Name string ` json: "Name" `//Name
IdCard string ` json: "IdCard `//id" 18 + x
Height float64 ` json, "Height" `//Height, the unit cm
Weight float64 ` json: "Weight" `/Weight/body Weight, unit kg
The Age int ` json: "Age" `//
BloodType string ` json: "BloodType `//type"
Phone string ` json: "Phone" `//Phone number
EmergencyPhone string ` json: "EmergencyPhone `//emergency contact phone
Gender string ` json: "Gender" `//
Status int ` json: "Status" `/register/default 0, 1 out of the race, register at 1 to receive package, 2
PayStatus int ` json: "PayStatus `//
"PayLog string ` json: "PayLog `//
"The Amount float64 ` json: "the Amount" `//
PrepayId string ` json: "PrepayId `//and payment id
CreateAt time. Time ` json: "CreateAt `//
"}
` ` `

CodePudding user response:

Only the struct?

Not should be helping to generate a set of crud yao...

In addition the conversion should be made command line tools,

CodePudding user response:

Also probably looked at your code, there is a problem is you didn't do to reserve some word processing

Such as id should be designed into id rather than id
Url should be made a special url instead of the url, or in the ide will prompt warning

You can refer to the

https://github.com/golang/lint/blob/470b6b0bb3005eda157f0275e2e4895055396a81/lint.go

The

commonInitialisms

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related