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

边栏推荐
- In 2020q2, shipments in the global tablet market soared by 26.1%: Huawei ranked third and Lenovo increased the most!
- 22年多校第三场(F的证明
- Ugui learning notes (V) togglegroup makes multiple choice radio boxes
- 线性代数及矩阵论(七)
- Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
- Comprehensively design an oppe homepage -- after sales service of the page
- Games101 section 13 ray tracing notes
- Go language slow entry - process control statement
- MySQL数据库增删改查(基础操作命令详解)
- Unity shader uses rendered texture to achieve glass effect
猜你喜欢

UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree

Problem solution of code heartstrings Junior Group (official competition) of Dalian University of Technology (Development Zone campus) in 2021

带参数的微信小程序二维码生成

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

Reasoning Over Semantic-Level Graph for Fact Checking

Application of Pegasus d200s UAV and airborne lidar in large-scale DEM construction

Games101 section 13 ray tracing notes

Round 1A 2022 - Code jam 2022 c.weightlifting (interval DP)

Source code of voice live broadcast app
![[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
随机推荐
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
HTAP comes at a price
Global mobile communication base station market in 2019: Ericsson, Huawei and Nokia ranked in the top three
Function接口之andThen
Modeling Semantics with Gated Graph Neural Networks for KBQA
批量下载文件
Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
Valarray Library Learning
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
C # traversal set
Shopee code League 2022 - qualification round p3.connecting the numbers (segment tree / bipartite graph determination, to be discussed)
线性代数及矩阵论(七)
Unity shader depth of field effect
Create a self-organizing / safe / controllable Lora network! Semtech responded for the first time to the impact of the "new regulations of the Ministry of industry and information technology"
Android Development - set cache
微服务架构-服务注册中心和服务网关(6.8) (转载)
Easypoi --- excel file export
利用SQL Server代理作业对数据库进行定时还原
Make full use of English
Analysis of kubernetes service principle