欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gorm系列-简单入门

程序员文章站 2022-03-26 20:18:18
简介 GORM系列:b站搜七米 文档:https://www.liwenzhou.com/posts/Go/gorm/ 什么是ORM:Object(对象) Relational(关系) Mapping(映射) 对象:程序中的对象/实例 例如Go中的结构体实例 关系:关系数据库 例如MySQL gor ......

简介

gorm系列:b站搜七米

文档:https://www.liwenzhou.com/posts/go/gorm/

什么是orm:object(对象) relational(关系) mapping(映射)

对象:程序中的对象/实例 例如go中的结构体实例

关系:关系数据库 例如mysql

gorm的官方文档:https://gorm.io/zh_cn/docs/

案例

举个不用gorm的例子

type userinfo struct {
	id uint
	name string
	gender string
	hobby string
}

func main()  {
	u1 := userinfo{1, "zisefeizhu", "男","篮球"}
	//将u1数据存入数据库
	insert into userinfo values(1, "紫色飞猪","男","篮球");  //sql语句
}

要求开发不仅会go还要熟练掌握sql语句

使用gorm的例子

type userinfo struct {
	id uint
	name string
	gender string
	hobby string
}

func main()  {
	u1 := userinfo{1, "zisefeizhu", "男","篮球"}
	//将u1数据存入数据库
	orm.create(&u1)   //orm语句不要太简单  
}

gorm系列-简单入门

orm优缺点

优点:提高开发效率

缺点:牺牲执行性能、牺牲灵活性、弱化sql能力

入门

安装gorm

e:\gostudent\gin\lesson19>go get -u github.com/jinzhu/gorm

**安装mysql ** 下载地址:

  1. 解压到指定目录, 例如:e:\mysql

  2. 管理员cmd到bin目录 例如:e:\mysql\mysql-8.0.19-winx64\bin

  3. 执行: mysqld --initialize --console 记下密码:-yml?i2uo7gz(密码随机生成,每次密码不一样,如果没记住,删掉data文件夹,重新执行该命令)

  4. 创建mysql8服务:mysqld --install mysql8

  5. 启动mysql8:net start mysql8

  6. 登陆修改密码

    e:\mysql\mysql-8.0.19-winx64\bin>mysql -uroot -p
    enter password: ************
    welcome to the mysql monitor.  commands end with ; or \g.
    your mysql connection id is 8
    server version: 8.0.19
    
    copyright (c) 2000, 2020, oracle and/or its affiliates. all rights reserved.
    
    oracle is a registered trademark of oracle corporation and/or its
    affiliates. other names may be trademarks of their respective
    owners.
    
    type 'help;' or '\h' for help. type '\c' to clear the current input statement.
    
    mysql> alter user 'root'@'localhost' identified by '123456';
    query ok, 0 rows affected (0.22 sec)
    
    mysql> flush privileges;
    query ok, 0 rows affected (0.06 sec)
    mysql> exit
    bye
    e:\mysql\mysql-8.0.19-winx64\bin>mysql -uroot -p
    enter password: 123456
    
  7. windows环境变量设置
    gorm系列-简单入门

  8. sqlyog连接

    mysql> alter user 'root'@'%' identified with mysql_native_password by '123456';  #修改加密规则
    mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456'; #修改密码加密策略
    query ok, 0 rows affected (0.06 sec)
    
    mysql> flush privileges;  #刷新权限
    query ok, 0 rows affected (0.04 sec)
    

gorm系列-简单入门
gorm基本示例

使用sqlyog professional 64 学习
gorm系列-简单入门

package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

//userinfo -->数据表
type userinfo struct {
	id int
	name string
	gender string
	hobby string
}
func main() {
	//连接mysql数据库
	//方法1
	//connargs := fmt.sprintf("%s:%s@%s(%s:%d)/%s?charset=utf8mb4&parsetime=true&loc=local", "root","123456", "tcp","127.0.0.1", 3306, "db" )
	//db, err := gorm.open("mysql", connargs)
	//方法2    一定要细心啊
	db, err := gorm.open("mysql","root:123456@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parsetime=true&loc=local")
	//panic: dial tcp [::1]:3306: connectex: no connection could be made because the target machine actively refused it. 数据库没打开 被拒绝

	if err != nil{
		panic(err)
	}
	defer db.close()

	//创建表 自动迁移(把结构体和数据表进行对应)
	db.automigrate(&userinfo{})

	//创建数据行
	u1 := userinfo{3, "紫色飞猪","男","直播"}
	db.create(&u1)
}

gorm系列-简单入门
简单的增删改查

package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

//userinfo -->数据表
type userinfo struct {
	id int
	name string
	gender string
	hobby string
}
func main() {
	//连接mysql数据库
	//方法1
	//connargs := fmt.sprintf("%s:%s@%s(%s:%d)/%s?charset=utf8mb4&parsetime=true&loc=local", "root","123456", "tcp","127.0.0.1", 3306, "db" )
	//db, err := gorm.open("mysql", connargs)
	//方法2    一定要细心啊
	db, err := gorm.open("mysql","root:123456@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parsetime=true&loc=local")
	//panic: dial tcp [::1]:3306: connectex: no connection could be made because the target machine actively refused it. 数据库没打开 被拒绝

	if err != nil{
		panic(err)
	}
	defer db.close()

	//创建表 自动迁移(把结构体和数据表进行对应)
	db.automigrate(&userinfo{})

	////创建数据行
	//u1 := userinfo{3, "紫色飞猪","男","直播"}
	//db.create(&u1)

	//查询
	var u userinfo
	db.first(&u)   //查询一行
	fmt.printf("u:%#v\n",u)
	var u1 userinfo
	db.last(&u1)   //查询最后一行
	fmt.printf("u1:%#v\n",u1)
	//更新
	db.model(&u).update("hobby","双色球")
	//删除
	db.delete(&u).delete("")
}