新建Beego项目,并创建数据库
create database 05blog default character set utf8;
数据库驱动注册等信息
在models下,建立db.go文件
package models import( "blog/05blog/models/class" // 注册模型,需要引入该包 "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) /* 使用orm连接数据库步骤: //告诉orm使用哪一种数据库 1.注册数据库驱动RegisterDriver(driverName, DriverType) 2.注册数据库RegisterDataBase(aliasName, driverName, dataSource, params ...) 3.注册对象模型RegisterModel(models ...) 4.开启同步RunSyncdb(name string, force bool, verbose bool) */ // 在init函数中连接数据库,当导入该包的时候便执行此函数 func init(){ orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase("default", "mysql", "root:123456@tcp(localhost:3306)/05blog?charset=utf8") orm.RegisterModel(new(class.User)) // 注册模型,建立User类型对象,注册模型时,需要引入包 orm.RunSyncdb("default", false, true) }
实现数据库的相关方法的封装
在models文件夹中新建文件夹class,在class文件夹中新建文件user.go
完成对数据库操作的基本封装,分别是读,写,更新操作封装
package class import ( "fmt" "github.com/astaxie/beego/orm" ) // 完成User类型定义 type User struct { Id int `orm:"pk"` // 设置为主键,字段Id, Password首字母必须大写 Password string } func (u *User) ReadDB() (err error) { o := orm.NewOrm() err = o.Read(u) return err } func (u *User) Create() (err error) { o := orm.NewOrm() fmt.Println("Create success!") _, _ = o.Insert(u) return err } func (u *User) Update() (err error) { o := orm.NewOrm() _, err = o.Update(u) return err }
在view文件夹下新建login.html,用于登陆页面展示
<html> <head> <title>04 blog title</title> </head> <body> <h2>Register</h2> <form action="/register" method="post"> id:<input name="userid"/> password:<input name="password" type="password"> <input type="submit"> </form> <h2>Reallogin</h2> <form action="/reallogin" method="post"> id:<input name="userid"/> password:<input name="password" type="password"> <input type="submit"> </form> </body> </html>
注册路由
修改routers文件夹下router.go文件,如下:
package routers import ( "blog/04blog/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/login", &controllers.UserController{}, `get:PageLogin`) beego.Router("/register", &controllers.UserController{}, `post:Register`) beego.Router("/reallogin", &controllers.UserController{}, `post:Reallogin`) }
在controller文件夹下,新建user.go文件,用于实现路由中的方法
package controllers import ( "blog/04blog/models/class" "fmt" "github.com/astaxie/beego" "github.com/astaxie/beego/validation" // 用于校验信息 ) type UserController struct { beego.Controller } func (c *UserController) PageLogin() { c.TplName = "login.html" // 将hello.html页面输出 } func (c *UserController) Register() { id, _ := c.GetInt("userid") // login.html中传过来的数据,这个GetInt返回两个值 password := c.GetString("password") fmt.Println("This is id and password") fmt.Println(id, password) valid := validation.Validation{} valid.Required(id, "id") // 校验是否为空值 valid.Required(password, "password") // valid.MaxSize(id, 20, "id") switch { // 使用switch方式来判断是否出现错误,如果有错,则打印错误并返回 case valid.HasErrors(): fmt.Println(valid.Errors[0].Key + valid.Errors[0].Message) c.TplName = "bad.html" return } u := &class.User{ Id: id, Password: password, } err := u.Create() if err != nil { fmt.Println(err) c.TplName = "bad.html" return } c.TplName = "welcome.html" } func (c *UserController) Reallogin() { id, _ := c.GetInt("userid") password := c.GetString("password") u := &class.User{ Id: id, Password: password, } err := u.ReadDB() if err != nil { fmt.Println(err) c.TplName = "bad.html" return } c.TplName = "welcome.html" }
登陆/注册成功/失败页面
登陆/注册成功页面welcome.html:
welcome
登陆/注册失败页面bad.html:
bad
main.go中启动models的init方法
package main import ( _ "blog/04blog/models" _ "blog/04blog/routers" "github.com/astaxie/beego" ) func main() { beego.Run() }
注册完之后,输入正确的用户名时,无论输入什么密码都能返回welcome :arrow:
这什么破烂的登录啊 每次都需要重新登录吗?!
Hi all, here every person is sharing such knowledge, so it’s nice to read this weblog, and I used
to pay a quick visit this web site daily.
谢谢大佬,写的简单易懂,看了一堆博客,就你这个对新手友好。
这里的登陆查询有错。只是根据主键检索表中是否存在该条记录,并没有对密码进行对比。所以这里的验证理论上是只需要输入正确的账号即可登陆,密码就失效了。
I think everything posted made a great deal of sense.
But, what about this? what if you typed a catchier post title?
I ain’t saying your information isn’t solid, however suppose you added a post
title to possibly get folk’s attention? I mean Beego 用户注册和登陆实现 – Imekaku-Blog is a little
vanilla. You might glance at Yahoo’s home page and note how they create article titles to get viewers interested.
You might add a video or a related picture or two to grab people interested about everything’ve written. In my
opinion, it would make your blog a little livelier.
来个orm的封装,链接一次数据库就行,返回一个orm对象,来共用操作数据库。