当前位置:网站首页>golang : MVC之models
golang : MVC之models
2022-07-25 21:44:00 【IT工作者】
背景:
MVC是系统开发常用的框架,Models 模型层用来处理数据 ,本篇使用beego框架,演示 models层的用法
代码:
在models 路径下面创建一个go文件 ,用来处理数据库链接,CRUD等相关操作
package models
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
var (
db orm.Orme
)
//UserInfo is defined a struct of use
type UserInfo struct {
ID int64
Username string
Password string
}
// Init connect database
func init() {
orm.Debug = true //打开调试模式
orm.RegisterDataBase("default", "mysql", "root:[email protected](127.0.0.1:3306)/test?charset=utf8", 30)
orm.RegisterModel(new(UserInfo))
db = orm.NewOrm()
}
//Adduser to add a use
func Adduser(userinfo *UserInfo) (int64, error) {
id, err := db.Insert(userinfo)
return id, er
}这里需要注意 包里面的init方法 比较特殊 ,函数名字要小写
再实现一个controlle
package controllers
import (
"WEB/models"
"strconv"
"github.com/astaxie/beego"
)
//ModuleController is a Controller to handel struct of models
type ModuleController struct {
beego.Controlle
}
//Get is a function
func (c *ModuleController) Get() {
//c.Ctx.WriteString("test moudle")
user := models.UserInfo{Username: "wangwu", Password: "56789"}
id, _ := models.Adduser(&user)
c.Ctx.WriteString(strconv.FormatInt(id, 10))
}主要操作是
1.创建用户
2.执行添加用户操作
运行
bee run
浏览器可以查看到输出的用户id
http://127.0.0.1:8080/modulecontrol
输出 3
查看数据库
wangwu这个用户已经添加了进来
边栏推荐
- Talk about what's going on with C # backstage GC?
- 【Flink】FLink RocksDBListState 报错 You cannot add null to a ListState
- [redis underlying parsing] linked list type
- pyqt5使用pyqtgraph绘制多个Y值散点图
- Performance debugging -- chrome performance
- 8000 word super detailed custom structure type
- 若依如何解决导出使用下载插件出现异常?
- 5、 Pinda general permission system__ PD tools XXS (anti cross site script attack)
- [leetcode ladder] linked list · 021 merge two ordered linked lists
- 【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)
猜你喜欢

Origen foundation officially launched $ogy stacking, leading a new round of ecological benefits

IJCAI2022开会了! 微软等《领域泛化Domain Generalization》教程

人脸与关键点检测:YOLO5Face实战

如何用 Redis 实现分布式锁的?

【饭谈】如何设计好一款测试平台?

8000 word super detailed custom structure type

工作面试总遇秒杀? 看了京东 T8 大咖私藏的秒杀系统笔记, 已献出膝盖

C#Socket

Stm3 (cubeide) lighting experiment

Sentinel vs Hystrix 限流对比,到底怎么选?
随机推荐
The noise reduction effect is increased by more than 6 times! With the microphone inside the headset, this wireless noise reduction headset is unusual!
[redis underlying parsing] string type
【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
[leetcode ladder] linked list · 021 merge two ordered linked lists
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决
C#程序设计的6大原则
Does the open source agreement have legal effect?
Why do independent sellers like to do e-mail marketing? The original conversion rate can be improved so much!
Bitcoin.com:usdd represents a truly decentralized stable currency
Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees
My heart's broken! After being cheated by 30000, a 16-year-old girl was unconvinced and cheated by 50000
C#Socket
Research on the scheme of MySQL advanced (VIII) sorting problem
YUV420 YUV420sp 图像格式「建议收藏」
Pyqt5 use pyqtgraph to draw multiple y-value scatter plots
【Redis底层解析】链表类型
Autojs learning - file depth search
Mysql8.0 MHA to achieve high availability "MHA"
[Flink] flick rocksdbliststate reports an error you cannot add null to a liststate
Create files, file permissions, ownership, and sticky bits