iris-xorm开始
2020-02-14 本文已影响0人
EasyNetCN
xorm(https://xorm.io/)是golang的开源ORM框架,支持以下数据库驱动:
- Mysql: github.com/go-sql-driver/mysql
- MyMysql: github.com/ziutek/mymysql/godrv
- Postgres: github.com/lib/pq
- Tidb: github.com/pingcap/tidb
- SQLite: github.com/mattn/go-sqlite3
- MsSql: github.com/denisenkom/go-mssqldb
- Oracle: github.com/mattn/go-oci8 (experiment)
xorm的源代码地址:https://gitea.com/xorm/xorm
xorm的文档:http://gobook.io/read/gitea.com/xorm/manual-en-US
下面的示例,实现了xorm的初始化,同步表结构
xorm列定义:http://gobook.io/read/gitea.com/xorm/manual-en-US/chapter-02/4.columns.html
基于Mysql驱动的数据库连接字符串:
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
package main
import (
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
)
type App struct {
Id int64 `xorm:"autoincr pk notnull 'id' comment('ID')"`
Code string `xorm:"varchar(200) notnull 'code' default('') index(code) comment('编码')"`
Name string `xorm:"varchar(200) notnull 'name' default('') index(name) comment('名称')"`
Status int `xorm:"int notnull 'status' default(1) comment('状态')"`
DelStatus int `xorm:"int notnull 'del_status' default(0) comment('删除状态')"`
CreateTime time.Time `xorm:"datetime notnull 'create_time' comment('创建时间')"`
UpdateTime time.Time `xorm:"datetime notnull 'update_time' comment('更新时间')"`
}
func main() {
engine, err := xorm.NewEngine("mysql", "root:password@tcp(localhost:3306)/easynet_cn_app?charset=utf8")
if err != nil {
fmt.Println(err)
return
}
engine.ShowSQL(true)
err = engine.Sync2(new(App))
if err != nil {
fmt.Println(err)
return
}
}