golang web框架 beego 学习 (四) 连接mysql
1 DB参数配置在app.conf
appname = gowebProject
httpport =
runmode = dev [db]
host= localhost
port=
databaseName = test
userName= root
password= root
2 模型定义在Models.go中
package models import (
"fmt"
"time" "github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
) type Store struct {
Id int64
Title string
Created time.Time `orm:"index"`
Views int64 `orm:"index"`
TopicTime time.Time `orm:"index"`
TopicCount int64
TopicLastUserId int64
} type Customer struct {
Id int64
Uid int64
Title string
Content string `orm:"size(5000)"`
Attachment string
Created time.Time `orm:"index"`
Updated time.Time `orm:"index"`
Views int64 `orm:"index"`
Author string
ReplyTime time.Time `orm:"index"`
ReplyCount int64
ReplyLastUserId int64
} func RegisterDB() {
//注册 model
orm.RegisterModel(new(Store), new(Customer))
//注册驱动
orm.RegisterDriver("mysql", orm.DRMySQL)
//注册默认数据库
host := beego.AppConfig.String("db::host")
port := beego.AppConfig.String("db::port")
dbname := beego.AppConfig.String("db::databaseName")
user := beego.AppConfig.String("db::userName")
pwd := beego.AppConfig.String("db::password") dbcon := user + ":" + pwd + "@tcp(" + host + ":" + port + ")/" + dbname + "?charset=utf8"
fmt.Print(dbcon)
orm.RegisterDataBase("default", "mysql", dbcon /*"root:root@tcp(localhost:3306)/test?charset=utf8"*/) //密码为空格式
}
3 main
package main import (
_ "gowebProject/routers" "github.com/astaxie/beego" // "fmt"
"gowebProject/models" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql"
) //引入数据模型
func init() {
// 注册数据库
models.RegisterDB()
} func main() {
// 开启 ORM 调试模式
orm.Debug = true
// 自动建表
orm.RunSyncdb("default", false, true)
// 运行时
beego.Run()
}
golang web框架 beego 学习 (四) 连接mysql的更多相关文章
- golang web框架 beego 学习 (一) 环境搭建
下面的命令我都是在$GOPATH的路径下执行的: 1. 首先下载beego框架: go get github.com/astaxie/beego (注意:运行上面命令时没有反应,需要在etc/host ...
- golang web框架 beego 学习 (三) beego获取参数
直接上常用的例子吧: A: 获取URL中的参数 router func init() { beego.Router("/task/?:id/?:name", &co ...
- golang web框架 beego 学习 (二) router and controller
1 Router和Controller的常用配置 beego.Router("/user/admin", &controllers.UserController{}) // ...
- golang web框架 beego 学习 (六) request body和module的映射
router.go package routers import ( "gowebProject/controllers" "github.com/astaxie/bee ...
- golang web框架 beego 学习 (五) 配置文件
app.conf: appname = gowebProject httpport = runmode = dev copyrequestbody = true [db] host= localhos ...
- golang web框架 beego 学习 (七)json转数组
Modules type User struct { Id int64 `json:"id"` Name string `json:"name"` Email ...
- golang web框架 beego
尝试了下,在go环境ready的情况下,花了2分钟完成了beego安装.项目生成和启动,效率还是不错的 1.安装: go get github.com/astaxie/beego go get git ...
- openresty 学习笔记四:连接mysql和进行相关操作
openresty 学习笔记四:连接mysql和进行相关操作 毕竟redis是作为缓存,供程序的快速读写,虽然reidis也可以做持久化保存,但还是需要一个做数据存储的数据库.比如首次查询数据在red ...
- Go语言及Web框架Beego环境无脑搭建
[原]Go语言及Web框架Beego环境无脑搭建 本文涉及软件均以截至到2013年10月12日的最新版本为准 1. 相关软件准备: 1) go1.2rc1.windows-386.msi,对应32位w ...
随机推荐
- anu - reactIE
import { options } from "./util"; import { Children } from "./Children"; import ...
- phpstorm 破解方法
前提概要:我收到博友:3102009292 的消息,发给我另一个破解PHPstorm的方法,我亲测有效,方便快捷,在和博友商量后,决定加入我的本文章中,供更多的开发者参考,话不多说,重点来了. 按照P ...
- php include,require 主要是向网页中引入文件
- 解决Viewpager满屏不能自适应填充内容的三种办法
由于排版问题,本人博客园同名博文地址为:http://www.cnblogs.com/bill-technology/articles/3143667.html 很多Android开发者在使用View ...
- 内网dns劫持
进行内网的dns劫持呢 ---> 我们需要用到ettercap 和ettercap内置的工具dns_spoof 1.我们需要开启ip转发 echo >/proc/sys/net/ipv4/ ...
- ThinkPHP CodeIgniter URL访问举例
ThinkPHP URL访问: http://localhost/think/index.php/Home/login/func/[name/syt/password/123/] ht ...
- Docker教程-01.安装docker-ce-18.06
参考文章:http://www.runoob.com/docker/docker-tutorial.html 1.Docker简介 1)Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 ...
- Linux下常用的ftp操作命令
Linux下常用的ftp操作命令 =========== 完美的分割线 ============= 1.登陆ftp服务器 ftp [IP] [PORT] # 登陆ftp服务器,本机登陆可以不写IP 实 ...
- C语言与VT100控制码编程
C语言与VT100控制码编程 声明: . 如果您打算阅读本文,希望您已经了解过C语言的基本语法,本文不对C语言的基本语法进行说明,因为那些东西几乎唾手可得; . 本文在vim中编辑,请尽量是用vim进 ...
- 51Nod 1090: 3个数和为0
1090 3个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等. ...