可以脱离beego框架单独安装使用 ORM:
数据库的设置:
将你需要使用的 driver 加入 import 中
orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
orm.RegisterModel(new(User))
o := orm.NewOrm()
user := User{Name: "slene"}
// insert
id, err := o.Insert(&user)
// update
user.Name = "astaxie"
num, err := o.Update(&user)
// read one
u := User{Id: user.Id}
err = o.Read(&u)
// delete
num, err = o.Delete(&u)
AuthUser -> auth_user
Auth_User -> auth__user
DB_AuthUser -> d_b__auth_user
SQL 直接查询
SQL 事务
调试查询日志
例子:
构造查询
QueryBuilder 提供了一个简便,流畅的SQL查询构造器。在不影响代码可读性的前提下用来快速的建立SQL语句。
type QueryBuilder interface {
Select(fields ...string) QueryBuilder
From(tables ...string) QueryBuilder
InnerJoin(table string) QueryBuilder
LeftJoin(table string) QueryBuilder
RightJoin(table string) QueryBuilder
On(cond string) QueryBuilder
Where(cond string) QueryBuilder
And(cond string) QueryBuilder
Or(cond string) QueryBuilder
In(vals ...string) QueryBuilder
OrderBy(fields ...string) QueryBuilder
Asc() QueryBuilder
Desc() QueryBuilder
Limit(limit int) QueryBuilder
Offset(offset int) QueryBuilder
GroupBy(fields ...string) QueryBuilder
Having(cond string) QueryBuilder
Subquery(sub string, alias string) string
String() string
}