beego框架中模型的创建及orm数据库操作并实现注册_登录_查询列表功能

管理员 发布于 7个月前   244

学习go语言beego框架,话不多说,接着上一篇博文往下看

实现功能:model添加,注册/登录/查询列表等功能


1.mode创建 我这里以users表为例

在models目录下创建user.go文件作为user表的模型:

package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
//全局连接实例
var (
db orm.Ormer
)
//由于model这个名字叫 Users 那么操作的表其实 users
type Users struct {
Id         int64
Name       string
Email      string
Password   string
Created_at string
}
//实例
func init() {
orm.Debug = true // 是否开启调试模式 调试模式下会打印出sql语句
orm.RegisterDataBase("default", "mysql", "root:Cbb_0721!@#$@tcp(192.168.1.140:3306)/laravel?charset=utf8", 30)
orm.RegisterModel(new(Users))
db = orm.NewOrm()
}
//方法 添加
func AddUser(users *Users) (int64, error) {
id, err := db.Insert(users)
return id, err
}


2.注册、登录、查询列表路由

在routers目录的router.go文件添加

package routers
import (
"gblog/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/user", &controllers.UserController{}, "get:Get")
beego.Router("/reg", &controllers.UserController{}, "get:Reg")
beego.Router("/register", &controllers.UserController{}, "post:Register")
beego.Router("/login", &controllers.UserController{}, "get:Login")
beego.Router("/info", &controllers.UserController{}, "post:Info")
beego.Router("/infolist", &controllers.UserController{}, "get:InfoList")
beego.BConfig.EnableGzip = true
beego.BConfig.RouterCaseSensitive = true
beego.BConfig.MaxMemory = 1 << 26
beego.BConfig.WebConfig.AutoRender = true
beego.BConfig.CopyRequestBody = true
}

3.注册/登录模板的创建,就是html/js/css代码

在view目录下创建register.html、login.html模板文件

register.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
<h1>注册页面</h1>
<form method="post" action="/register">
    用户名:<input type="text" name="name"> <p></p>
    &nbsp &nbsp邮箱:<input type="text" name="email"> <p></p>
    &nbsp &nbsp密码:<input type="password" name="password"><p></p>
    <input type="submit" name="注册" style="margin-left: 190px;">
</form>
</body>
</html>

login.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
</head>
<body>
<h1>登陆界面</h1>
<form method="post" action="/info">
    用户名:<input type="text" name="name"><p></p>
    &nbsp &nbsp密码:<input type="password" name="password"><p></p>
    <input type="submit" value="登陆" style="margin-left: 190px;">
</form>
</body>
</html>

4.注册/登录/查询列表 功能 (注意:功能我就不分开写了,里面都有备注,有其他框架基础的话看一眼就会觉得有点熟悉)

在controllers目录创建user.go文件作为user控制器

package controllers

import (
"gblog/models"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)

// UserController  is a user control
type UserController struct {
beego.Controller
}

//注册页模板
func (c *UserController) Reg() {
c.TplName = "register.html" //只有加了这句,前端才会跳转到注册页面
}

//注册用户  模型添加数据
func (c *UserController) Register() {
//1.拿到数据
name := c.GetString("name")
email := c.GetString("email")
password := c.GetString("password")
//2.对数据进行校验
if name == "" && email == "" && password == "" {
beego.Info("数据不能为空")
c.Redirect("/reg", 302) //重定向函数,如果发生错误页面重新回到注册页,并返回错误码302
return
}
//24小时制
timeObj := time.Now()
var str = timeObj.Format("2006-01-02 15:04:05")
user := models.Users{Name: name, Email: email, Password: password, Created_at: str}
models.AddUser(&user)
c.Ctx.WriteString("注册成功!")
}

//登录页模板
func (c *UserController) Login() {
c.TplName = "login.html"
}

//单条数据查询
func (c *UserController) Info() {
//获取get值
name := c.GetString("name")
password := c.GetString("password")
//2.判断数据是否合法
if name == "" || password == "" {
beego.Info("输入数据不合法")
c.TplName = "login.html"
return
}
//3.查询账号密码是否正确
o := orm.NewOrm()
user := models.Users{}
user.Name = name
user.Password = password
err := o.Read(&user, "Name", "Password")
if err != nil {
beego.Info("查询失败")
c.TplName = "login.html"
return
}
//4.跳转
c.Ctx.WriteString("登陆成功,老铁欢迎您")
c.Data["json"] = user
c.ServeJSON()
c.StopRun()
}
//使用原生sql进行列表数据查询
func (c *UserController) InfoList() {
o := orm.NewOrm()
var maps []orm.Params
sql := `select * from users`
o.Raw(sql).Values(&maps)
c.Data["json"] = maps
c.ServeJSON()
}

5.最后看看效果

注册:

1.png

2.png

3.png

登录:

4.png

5.png

查询列表: (就的数据集 这个就不截图了有点长)

url:http://127.0.0.1:8080/infolist

[
  {
    "created_at": "2020-09-25 03:37:38",
    "current_team_id": null,
    "email": "514224527@qq.com",
    "email_verified_at": null,
    "id": "1",
    "name": "admin",
    "password": "$2y$10$QKXE9t9l65cb3Vztes6S8eh/7uCG4jEigt8/MAKbgHEl8sLrxPfwC",
    "profile_photo_path": "profile-photos/726sxzkRKwn1lszPOApcL4gYrRTFlhC39MEc1Ku6.gif",
    "remember_token": null,
    "subscribe": "1",
    "two_factor_recovery_codes": null,
    "two_factor_secret": null,
    "updated_at": "2020-09-30 02:15:43"
  },
  {
    "created_at": "2020-09-30 06:00:25",
    "current_team_id": null,
    "email": "51422452@qq.com",
    "email_verified_at": null,
    "id": "2",
    "name": "站长小号",
    "password": "$2y$10$jkNk1ZD5THGCP5vmOcWd.e2rVqADywFAOgCgAJRdnBS4TZQC94jBe",
    "profile_photo_path": null,
    "remember_token": null,
    "subscribe": "0",
    "two_factor_recovery_codes": null,
    "two_factor_secret": null,
    "updated_at": "2020-09-30 06:00:25"
  },
  {
    "created_at": "2020-09-30 06:00:25",
    "current_team_id": null,
    "email": "5142245@qq.com",
    "email_verified_at": null,
    "id": "3",
    "name": "站长二号",
    "password": "$2y$10$jkNk1ZD5THGCP5vmOcWd.e2rVqADywFAOgCgAJRdnBS4TZQC94jBe",
    "profile_photo_path": null,
    "remember_token": null,
    "subscribe": "0",
    "two_factor_recovery_codes": null,
    "two_factor_secret": null,
    "updated_at": "2020-09-30 06:00:25"
  },
  {
    "created_at": "2020-12-11 13:52:18",
    "current_team_id": null,
    "email": "test@qq.com",
    "email_verified_at": null,
    "id": "7",
    "name": "测试",
    "password": "123456",
    "profile_photo_path": null,
    "remember_token": null,
    "subscribe": "0",
    "two_factor_recovery_codes": null,
    "two_factor_secret": null,
    "updated_at": null
  }
]


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章归档

  • 2020-12         (6篇)
  • 2021-01         (2篇)

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客