当前位置:网站首页>Goweb开发之Beego框架实战:第四节 数据库配置及连接
Goweb开发之Beego框架实战:第四节 数据库配置及连接
2022-07-28 16:20:00 【qfliweimin】
在上节内容中总结了beego框架,本节课继续学习数据库配置和ORM映射关系介绍
mysql数据库安装
mysql官方下载网站:https://dev.mysql.com/downloads/ 我们使用的是5.7版本,下载链接:https://dev.mysql.com/downloads/mysql/5.7.html#downloads

选择下载安装文件,并在本地进行安装。 安装过程中,若出现初始密码,要记住该密码,首次登陆时要使用。
【注意】windows安装说明:https://jingyan.baidu.com/article/09ea3ededa53b7c0aede39b5.html
安装完毕后,将mysql的bin目录路径添加配置到环境变量,以便能够在终端命令行中进行使用登陆mysql。
在终端中登陆mysql的命令:
mysql -root root -p输入安装时或者自己设置的密码即可登陆成功。如下所示:

如上图所示表示登陆成功。为了使用自己的密码,可以自己设置一个自己熟悉的密码,方便自己记忆。
mysql数据库常用命令
mysql数据库的一些命令使用:
- 查看数据库:
show databases; - 使用某个数据库:
use databaseName; - 展示某个数据库表格列表:
show tables; - mysql中的一些其他命令:

以上mysql数据操作都是命令行终端形式,为了方便我们日常操作,我们可以使用图形化界面工具,在这里我们使用的是navicat工具。我们现在来看如何进行安装。
Navicat安装
navicat工具下载地址:https://www.navicat.com/en/download/navicat-for-mysql
在上面的链接中,选择自己的系统版本,然后下载安装文件,进行安装,一直默认选下一步即可,最后安装成功。
安装好了以后,在mysql数据库中创建一个新的数据库,比如名字为:myblog;创建完数据库以后,我们来看一看在beego项目中如何使用mysql数据库。
数据库驱动
我们之前给大家说过,数据库编程属于beego中的Models层,也称ORM模块。
在beego中,目前支持三种数据库驱动,分别是:
- MySQL:http://github.com/go-sql-driver/mysql
- PostgreSQL:http://github.com/lib/pq
- Sqlite3:http://github.com/mattn/go-sqlite3
beego中的ORM所具备的几个特性:
- 1)支持Go语言的所有类型存储
- 2)CRUD操作简单
- 3)自动Join关联表
- 4)允许直接使用SQL查询
beego项目中使用mysql
- 1、导入对应的数据库驱动 比如mysql:
import _ "http://github.com/go-sql-driver/mysql"

- 2、注册驱动,连接数据库 通过如下两句话注册数据库驱动,以及连接数据库的操作:
orm.RegisterDriver("mysql",orm.DRMySQL)
orm.RegisterDataBase(aliasName,driverName,dbConn)
详细代码如下:

- 3、创建数据库并执行程序

连接数据库代码如下:
package models
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"BlogProject/MysqlDemo/util"
//切记:导入驱动包
_ "github.com/go-sql-driver/mysql"
)
func init() {
driverName := beego.AppConfig.String("driverName")
//注册数据库驱动
orm.RegisterDriver(driverName, orm.DRMySQL)
//数据库连接
user := beego.AppConfig.String("mysqluser")
pwd := beego.AppConfig.String("mysqlpwd")
host := beego.AppConfig.String("host")
port := beego.AppConfig.String("port")
dbname := beego.AppConfig.String("dbname")
//dbConn := "root:[email protected](127.0.0.1:3306)/cmsproject?charset=utf8"
dbConn := user + ":" + pwd + "@tcp(" + host + ":" + port + ")/" + dbname + "?charset=utf8"
err := orm.RegisterDataBase("default", driverName, dbConn)
if err != nil {
util.LogError("连接数据库出错")
return
}
util.LogInfo("连接数据库成功")
}

边栏推荐
- [deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
- Make full use of English
- Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
- SUSE Ceph 快速部署 – Storage6
- The 2021 ICPC ASIA Taipei Regional programming contest L. leadfoot (combinatorics /2-adic assignment function +kummer theorem)
- SUSE CEPH add nodes, reduce nodes, delete OSD disks and other operations – storage6
- Games101 assignment04 job 04
- How to use fail2ban to protect WordPress login page
- Question making note 2 (add two numbers)
- DGL Chapter 1 (official tutorial) personal notes
猜你喜欢

Games101-assignment05 ray tracing - rays intersect triangles

Re10:读论文 Are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr

Unity shader uses rendered texture to achieve glass effect

综合设计一个OPPE主页--页面服务部分

Educational codeforces round 126 (rated for Div. 2) f.teleporters (two sets and two points)

Unity3d shader achieves ablation effect
![[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 4 of pytorch introduction to project practice: realize logistic regression from 0 to 1 (with source code)

Analysis of browser decoding process
![[deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)
Read excel xlsx format file in unity
随机推荐
Semtech launched Lora edge, a geolocation solution for the Internet of things, and the first chip lr1110 is now on the market
Jupyter notebook win installation record
Summary of kubenertes 1.16 cluster deployment problems
Semtech推出物联网地理定位解决方案LoRa Edge,首款芯片LR1110现已上市
Rsync 服务部署与参数详解
Question making note 2 (add two numbers)
Some notes on how unity objects move
2022牛客多校第二场CDE
[deep learning]: day 9 of pytorch introduction to project practice: dropout implementation (including source code)
侦察机与预警机的区别
零基础利用Unity3D开发AR应用并远程下载3D模型
Re12: read these3 semantic self segmentation for abstract summary of long legal documents in low
[deep learning]: the second day of pytorch introduction to project practice: realize linear regression from zero (including detailed code)
Codeforces Round #750 (Div. 2) F.Korney Korneevich and XOR (easy&&hard version)(dp)
海康威视回应'美国禁令'影响:目前所使用的元器件都有备选
Function接口之andThen
UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
Codeworks round 801 (Div. 2) and epic Institute of technology round D. tree queries (tree DP)
Technology sharing | how to recover the erroneously deleted table and the data in the table?