代码:
package mainimport ( "github.com/beego/beego/v2/client/orm" _ "github.com/go-sql-driver/mysql" "log" "time")/* 标签的应用:1、用 beego orm 显示设置主键2、ID 属性须要设置 int64 或者显示指定 pk3、设置标签名应用:orm。多个标签应用分号分隔。4、设置主键:pk(个别主动设置,主动找第一个 int64 类型设置为主键)5、设置主动增长:auto(个别主动设置,主动找第一个 int64 类型设置为主动增长)6、设置列名:column(name)7、设置字符串类型:默认 varchar(255),设置长度应用 size(length)。其余字符串类型:type(text)8、是否容许为 null:默认不容许。容许为 null,应用:null9、默认值:default()10、正文:description()11、惟一索引:unique12、设置索引:index13、struct 字段的首字母必须大写,否则无奈映射到数据库表(不创立对应的 column)14、auto_now // 每次更新的时候主动设置属性为以后工夫。不体现在 sql 中,是在 orm 代码执行过程中实现15、auto_now_add // 当数据创立时设置为以后工夫。不体现在 sql 中,是在 orm 代码执行过程中实现16、工夫:type(date)17、默认引擎:InnoDB*/type Account struct { ID int64 `orm:"pk;auto;column(id)"` // Id string `orm:"pk"` // 强制设置为主键 // Name string `orm:"size(64);unique"` Name string `orm:"size(64)"` Password string `orm:"size(1024)"` Birthday *time.Time Telephone string Email string Addr string `orm:"default(中国北京)"` Status int8 `orm:"default(1);description(状态)"` RoleId int64 DepartmentId int64 CreatedAt *time.Time `orm:"auto_now_add;"` UpdatedAt *time.Time `orm:"auto_now;"` DeletedAt *time.Time `orm:"null;"` Description string `orm:"type(text)"` Sex bool Height float64 `orm:"digits(10);decimals(2)"` // 总共位数 10 位,小数位数 2 位 Weight float64 //height float64 //weight float64 //A string //B string //B int64}/* 关联表的办法: 1、默认会依据 struct 名称生成对应的表。例如 type Account struct 则生成表 account 。 2、应用该办法后,则依据 return 返回的名称生成或者关联到表。例如 return "act" 首次执行则生成表 act 。后续执行则将变更更新到表 act 。*/func (account *Account) TableName() string { return "act"}// 创立索引func (account *Account) TableIndex() [][]string { return [][]string{ // 给 name 创立索引,并给 telephone 和 email 创立联结索引 {"name"}, {"telephone", "email"}, }}func main() { // 驱动名 driverName := "mysql" // databaseName := "default" dsn := "golang:123456@tcp(192.168.100.20:33060)/cmdb?charset=utf8mb4&parseTime=true" // 注册数据库驱动到 orm // 参数:自定义的数据库类型名,驱动类型(orm 中提供的) orm.RegisterDriver(driverName, orm.DRMySQL) // 参数:beego 必须指定默认的数据库名称,应用的驱动名称(orm 驱动类型名),数据库的配置信息,数据库(连接池),连贯(池)名称 err := orm.RegisterDataBase(databaseName, driverName, dsn) if err != nil { log.Fatal(err) } // 定义构造 // 注册模型 orm.RegisterModel(new(Account)) // 依据定义的构造体生成表(只同步表构造,不同步库。须要提前创立库) // 第二个参数 force:如果为 true,则每次编译时都会从新生成表,即如果表已存在就删除并重建。如果为 false,则只更新列的变更 // 第三个参数 verbose:打印执行的 sql 语句 // orm.RunSyncdb(databaseName,false,true) orm.RunSyncdb(databaseName, true, true)}
sql 执行后果:
drop table `act` DROP TABLE IF EXISTS `act`create table `act` -- -------------------------------------------------- -- Table Structure for `main.Account` -- -------------------------------------------------- CREATE TABLE IF NOT EXISTS `act` ( `id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(64) NOT NULL DEFAULT '' , `password` varchar(1024) NOT NULL DEFAULT '' , `birthday` datetime NOT NULL, `telephone` varchar(255) NOT NULL DEFAULT '' , `email` varchar(255) NOT NULL DEFAULT '' , `addr` varchar(255) NOT NULL DEFAULT '中国北京' , `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态', `role_id` bigint NOT NULL DEFAULT 0 , `department_id` bigint NOT NULL DEFAULT 0 , `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `deleted_at` datetime, `description` longtext NOT NULL, `sex` bool NOT NULL DEFAULT FALSE , `height` numeric(10, 2) NOT NULL DEFAULT 0 , `weight` double precision NOT NULL DEFAULT 0 ) ENGINE=InnoDB; CREATE INDEX `act_name` ON `act` (`name`); CREATE INDEX `act_telephone_email` ON `act` (`telephone`, `email`);Process finished with exit code 0
生成的表:
MariaDB root@localhost:cmdb> desc act;+---------------+---------------+------+-----+----------+----------------+| Field | Type | Null | Key | Default | Extra |+---------------+---------------+------+-----+----------+----------------+| id | bigint(20) | NO | PRI | <null> | auto_increment || name | varchar(64) | NO | MUL | | || password | varchar(1024) | NO | | | || birthday | datetime | NO | | <null> | || telephone | varchar(255) | NO | MUL | | || email | varchar(255) | NO | | | || addr | varchar(255) | NO | | 中国北京 | || status | tinyint(4) | NO | | 1 | || role_id | bigint(20) | NO | | 0 | || department_id | bigint(20) | NO | | 0 | || created_at | datetime | NO | | <null> | || updated_at | datetime | NO | | <null> | || deleted_at | datetime | YES | | <null> | || description | longtext | NO | | <null> | || sex | tinyint(1) | NO | | 0 | || height | decimal(10,2) | NO | | 0.00 | || weight | double | NO | | 0 | |+---------------+---------------+------+-----+----------+----------------+17 rows in setTime: 0.008s