当前位置:网站首页>The practice of beego framework developed by goweb: Section 4 database configuration and connection
The practice of beego framework developed by goweb: Section 4 database configuration and connection
2022-07-28 17:20:00 【qfliweimin】
In the previous section, I summarized beego frame , This lesson continues to learn database configuration and ORM Introduction to mapping relationship
mysql Database installation
mysql Official download site :https://dev.mysql.com/downloads/ We use 5.7 edition , Download link :https://dev.mysql.com/downloads/mysql/5.7.html#downloads

Select download installation file , And install locally . During installation , If the initial password appears , Remember the password , Use when logging in for the first time .
【 Be careful 】windows Installation instructions :https://jingyan.baidu.com/article/09ea3ededa53b7c0aede39b5.html
After installation , take mysql Of bin Directory path add configuration to environment variable , In order to be able to use login in the terminal command line mysql.
Log in to the terminal mysql The order of :
mysql -root root -pEnter the password set during installation or yourself to log in successfully . As shown below :

As shown in the above figure, it indicates that the login is successful . In order to use your own password , You can set a password you are familiar with , Make it easy to remember .
mysql Database common commands
mysql Some commands of the database use :
- view the database :
show databases; - Use a database :
use databaseName; - Show a list of database tables :
show tables; - mysql Some other commands in :

above mysql Data operations are in the form of command line terminals , In order to facilitate our daily operation , We can use graphical interface tools , In this case we are using navicat Tools . Now let's see how to install .
Navicat install
navicat Tool download address :https://www.navicat.com/en/download/navicat-for-mysql
In the link above , Choose your own system version , Then download the installation file , Installation , Always choose the next step by default , Finally installed successfully .
After installation , stay mysql Create a new database in the database , For example, the name is :myblog; After creating the database , Let's take a look at beego How to use in a project mysql database .
Database driven
We told you before , Database programming belongs to beego Medium Models layer , Also known as ORM modular .
stay beego in , At present, three database drivers are supported , Namely :
- MySQL:http://github.com/go-sql-driver/mysql
- PostgreSQL:http://github.com/lib/pq
- Sqlite3:http://github.com/mattn/go-sqlite3
beego Medium ORM Several features :
- 1) Support Go All types of language storage
- 2)CRUD It's easy to operate
- 3) Automatically Join Association table
- 4) Allow direct use of SQL Inquire about
beego Project use mysql
- 1、 Import the corresponding database driver such as mysql:
import _ "http://github.com/go-sql-driver/mysql"

- 2、 Registration drive , Connect to database Register the database driver with the following two sentences , And the operation of connecting to the database :
orm.RegisterDriver("mysql",orm.DRMySQL)
orm.RegisterDataBase(aliasName,driverName,dbConn)
The detailed code is as follows :

- 3、 Create the database and execute the program

The connection code is as follows :
package models
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"BlogProject/MysqlDemo/util"
// Bear in mind : Import driver package
_ "github.com/go-sql-driver/mysql"
)
func init() {
driverName := beego.AppConfig.String("driverName")
// Register database driver
orm.RegisterDriver(driverName, orm.DRMySQL)
// Database connection
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(" Error connecting to database ")
return
}
util.LogInfo(" Successfully connected to database ")
}

边栏推荐
- 2022牛客多校第二场CDE
- Unity3d simple implementation of water surface shader
- : No such file or directory
- 带参数的微信小程序二维码生成
- Android Development - set cache
- System clock failure of database fault tolerance
- Read excel xlsx format file in unity
- 线性代数及矩阵论(十)
- List of supplementary questions
- Unity shader realizes water wave effect with noise texture
猜你喜欢
![[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)

Re10: are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr

Goweb开发之Beego框架实战:第四节 数据库配置及连接

飞马D200S无人机与机载激光雷达在大比例尺DEM建设中的应用

valarray数值库学习

MySQL数据库增删改查(基础操作命令详解)

Analysis of browser decoding process

Janus series article 3 API usage guide videoroom creating a new video room

Technology sharing | how to recover the erroneously deleted table and the data in the table?

Unity shader uses rendered texture to achieve glass effect
随机推荐
软考回顾总结
Differences between CNSA and CASC and CASIC
Steps to configure V530 switch
Using MVC in the UI of unity
在android开发过程中遇到.sqlite文件处理
火了 2 年的服务网格究竟给微服务带来了什么?(转载)
: No such file or directory
零基础利用Unity3D开发AR应用并远程下载3D模型
Unity shader procedural texture
2021年4月份自考
Global mobile communication base station market in 2019: Ericsson, Huawei and Nokia ranked in the top three
Games101-assignment05 ray tracing - rays intersect triangles
[deep learning]: the second day of pytorch introduction to project practice: realize linear regression from zero (including detailed code)
Vscode界面介绍
Function接口之andThen
Analysis of kubernetes service principle
Facet experience -- the development side of dragon game client
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
Janus series article 3 API usage guide videoroom creating a new video room
使用阿里云免费的SSL证书