🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
构造查询 ~~~ 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 ~~~ ~~~ package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) // struct 字段,首字母必须大写 type Person struct { User_id int `orm:"pk"` Username string Sex string Email string } func init() { maxIdle := 30 maxConn := 30 // 创建连接池 err := orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/test?charset=utf8", maxIdle, maxConn) if err != nil { fmt.Println("connect mysql err : ", err) } // 需要在init中注册定义的model orm.RegisterModel(new(Person)) } func main() { var p0 []Person // 获取 QueryBuilder 对象. 需要指定数据库驱动参数。 // 第二个返回值是错误对象,在这里略过 DB, _ := orm.NewQueryBuilder("mysql") // 构建查询对象 DB.Select("*"). From("person"). Where("user_id > 1"). OrderBy("user_id").Desc(). Limit(10) // 导出 SQL 语句 sql := DB.String() fmt.Println(sql) // 执行 SQL 语句 o := orm.NewOrm() o.Raw(sql).QueryRows(&p0) fmt.Println(p0) } ~~~