beego框架对数据库的链接,如果你只是把数据库连接写到配置文件里,是不起任何人作用的,对于用习惯了laravel框架的我来说是非常坑的一件事。
具体的链接只需要在入口文件添加
package main
import (
_ "achievement/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
func init() {
//设置日志文件
beego.SetLogger("file", `{"filename":"logs/beego.log"}`)
//设置数据库的配制
orm.RegisterDataBase("default","mysql","root:123456@tcp(127.0.0.1:3306)/ssms?charset=utf8")
beego.BConfig.WebConfig.Session.SessionOn = true
}
func main() {
beego.Run();
}
构造器
在beego文档对构造器的描述非常的少只有一个方法的描述,所以很多东西我们不得不一边看源码,一边尝试这些方法的作用。
源码中方法展示
type QueryBuilder interface {
Select(fields ...string) QueryBuilder
ForUpdate() QueryBuilder
From(tables ...string) QueryBuilder
InnerJoin(table string) QueryBuilder
LeftJoin(table string) QueryBuilder
RightJoin(table 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
Update(tables ...string) QueryBuilder
Set(kv ...string) QueryBuilder
Delete(tables ...string) QueryBuilder
InsertInto(table string, fields ...string) QueryBuilder
Values(vals ...string) QueryBuilder
Subquery(sub string, alias string) string
String() string
}
查询语句
func (u *User)LoginJudge(account string,password string) []User {
var users []User
qb,_:=orm.NewQueryBuilder("mysql")
// 构建查询对象
qb.Select("user.account").
From("user").
Where("account = ?").
And("password = ?").And("type != 2")
//返回sql语句
sql := qb.String()
// 执行 SQL 语句
o := orm.NewOrm()
o.Raw(sql,account,password).QueryRows(&users)
return users
}
beego的查询语句可以根据条件拼接
//查询学生的成绩
func (this *Score)SelectAll(page,limit, class int,school string) []ScoreInformation {
var score []ScoreInformation
page = (page-1)*10
qb,_ := orm.NewQueryBuilder("mysql")
qb.Select("student.number","student.name","student.sex","clazz.clazz_name","grade.grade_name","escore.score","course.name as course_name","exam.exam_name","exam.time").
From("student,clazz,grade,escore,course,exam").
Where("student.clazzid = clazz.id").
And("student.gradeid = grade.id").
And("student.id = escore.studentid").
And("escore.courseid = course.id").
And("exam.id = escore.examid")
if class != -1 {
qb.And("clazz.id = "+strconv.Itoa(class))
}
if school != ""{
qb.And("student.number = "+school)
}
qb.Limit(limit).
Offset(page)
sql := qb.String()
fmt.Println(sql)
o := orm.NewOrm()
_,error:=o.Raw(sql).QueryRows(&score)
fmt.Println(error)
fmt.Println(score)
return score
}
插入语句
func (u *User)AddMenager(account,password,name string) error {
qb,_:=orm.NewQueryBuilder("mysql")
// 构建查询对象
qb.InsertInto("user","user.account","user.password","user.name","user.type").
Values("?","?","?","?")
//返回sql语句
sql := qb.String()
fmt.Println(sql)
// 执行 SQL 语句
o := orm.NewOrm()
_,error := o.Raw(sql,account,password,name,3).Exec()
return error
}
删除语句
func (u *User)DeleteMenagerConut(id int) error {
qb,_:=orm.NewQueryBuilder("mysql")
// 构建查询对象
qb.Delete("user").
From("user").
Where("id = ?")
//返回sql语句
sql := qb.String()
fmt.Println(sql)
// 执行 SQL 语句
o := orm.NewOrm()
_,error := o.Raw(sql,id).Exec()
return error
}
在构造其如果有那些方法不知道是什么意思可以将sql := qb.String()用fmt.Println()打印出来来查看具体的sql语句或许就会知道哪里有问题了
近期会将改版本改进一下主要是对方法的封装,以及对一些写的不合理的地方的修改,以及添加一些的新的功能,并且会将注释加的详细一点,希望能给和我一样的小白提供一些便利
完整版
访问方式https://github.com/dajumao/achievement