当前位置:网站首页>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 ")
}

边栏推荐
- kubernetes service 原理解析
- Facet experience -- the development side of dragon game client
- 线性代数及矩阵论(七)
- Read excel xlsx format file in unity
- PostgreSQL weekly news - July 20, 2022
- Net framework
- Steps to configure V530 switch
- 创建自定义分页控件
- Soft exam review summary
- [deep learning]: the second day of pytorch introduction to project practice: realize linear regression from zero (including detailed code)
猜你喜欢

Net framework

Shopee code League 2022 - qualification round p3.connecting the numbers (segment tree / bipartite graph determination, to be discussed)

wpf命令按钮透明样式

Unity shader uses rendered texture to achieve glass effect

Verilog 每日一题 (VL5 信号发生器)

Goweb开发之Beego框架实战:第三节 程序执行流程分析

Source code of voice live broadcast app

Unity shader procedural texture

Games101 assignment04 job 04

Goweb开发之Beego框架实战:第二节 项目初始化配置
随机推荐
How to use fail2ban to protect WordPress login page
PostgreSQL weekly news - July 20, 2022
[deep learning]: day 6 of pytorch introduction to project practice: multi-layer perceptron (including code)
DGL Chapter 1 (official tutorial) personal notes
零基础利用Unity3D开发AR应用并远程下载3D模型
Code implementation additive attention
侦察机与预警机的区别
WPF command button transparent style
Hikvision responded to the impact of the 'US ban': there are options for the components currently used
System clock failure of database fault tolerance
Android Development - set cache
Ugui learning notes (V) togglegroup makes multiple choice radio boxes
SUSE CEPH rapid deployment – storage6
Add differential pairs and connections in Ad
Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
Goweb开发之Beego框架实战:第五节 项目搭建及注册用户
Easypoi multi sheet export by template
线性代数及矩阵论(八)
创建自定义分页控件
解决SQL Server数据库独占的问题