乘风原创程序

  • Gorm入门使用
  • 2021/7/2 17:40:58
  • gorm

    gorm crud 数据库的增删改查

    go get -u github.com/jinzhu/gorm
    go get -u github.com/jinzhu/gorm/dialects/mysql
    
    

    go常规查询操作

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"time"
    
    	"github.com/jinzhu/gorm"
    	_ "github.com/jinzhu/gorm/dialects/mysql"
    )
    
    var db *gorm.db
    
    // test 表结构
    type test struct {
    	id       int       `json:"id" db:"id" gorm:"id"`
    	username string    `json:"username" db:"username" gorm:"username"`
    	password string    `json:"password" db:"password" gorm:"password"`
    	datetime time.time `json:"datetime" db:"datetime" gorm:"datetime"`
    	city     string    `json:"city" db:"city" gorm:"city"`
    	country  string    `json:"country" db:"country" gorm:"country"`
    	sex      string    `json:"sex" db:"sex" gorm:"sex"`
    	age      string    `json:"age" db:"age" gorm:"age"`
    }
    
    // tablename 数据库名称规范
    func (t test) tablename() string {
    	return "test"
    }
    
    // init xx
    func init() (err error) {
    	db, err = gorm.open("mysql", "root:1qa2ws3ed@/gouse?charset=utf8&parsetime=true&loc=local")
    
    	if err != nil {
    		fmt.println("连接诶数据库失败")
    		return
    	}
    
    	return err
    }
    
    // selectdata 查询单条语句
    func selectdata() (err error) {
    	var info test
    
    	// first根据主键查询单条语句+++++++++++++
    	//_ = db.first(&info)
    	////fmt.println(info.username, info.password, info.age, info.datetime)
    	//marshal, err := json.marshal(info)
    	//if err != nil {
    	//	fmt.println("反序列化失败")
    	//	return
    	//
    	//}
    	//fmt.println(string(marshal))
    
    	// 随机获取条数据++++++++++++++++
    	//db.take(&info)
    	//takedata, err := json.marshal(info)
    	//fmt.println(string(takedata))
    
    	//// 根据主键查询最后一条记录+++++++++++++
    	//db.last(&info)
    	//lastdata, err := json.marshal(info)
    	//fmt.println(string(lastdata))
    
    	//// 获取所有记录+++++++++++++++
    	//var infolist []test
    	//
    	//db.find(&info)
    	//finddata, err := json.marshal(infolist)
    	//fmt.println(string(finddata))
    
    	// first 查询主键为10000的数据(仅当主键为int时)
    	db.first(&info, 10000)
    	marshal, err := json.marshal(info)
    	fmt.println(string(marshal))
    	return err
    }
    
    // wheredata where语句查询
    func wheredata() (err error) {
    	// where 条件查询sex 为女的主键第一个
    	//var info test
    	//db.where("sex = ?", "女").first(&info)
    	//marshal, err := json.marshal(info)
    	//fmt.println(string(marshal))
    
    	// 查询所有sex为男的用户 并统计计数
    	var infolist []test
    	//db.where("sex = ?", "男").find(&infolist)
    	//fmt.println(len(infolist))
    
    	db.where("id in (?)", []int{1000, 2000}).find(&infolist)
    	marshal, err := json.marshal(infolist)
    	fmt.println(string(marshal))
    
    	return err
    }
    
    // main 主函数
    func main() {
    	_ = init()
    	//err := selectdata()
    
    	err := wheredata()
    	if err != nil {
    		fmt.println("")
    		return
    	}
    }
    
    
    	// like 查询
    	db.where("username like ?", "%abc%").find(&infolist)
    	likedata, err := json.marshal(infolist)
    	fmt.println(string(likedata))
    	return err    
    
    

    注: ?其实就是占位符

    • go结构体转换成map

    go map 查询

    	db.where(map[string]interface{}{"id": 1000}).find(&info)
    	mapdata, err := json.marshal(info)
    
    	fmt.println(string(mapdata))
    
    	return err
    
    

    go not查询

    // notselect  not查询用法
    func notselect() (err error) {
    	var infolist []test
    
    	db.not("id = ?", "1").first(&infolist)
    	notlikedata, err := json.marshal(infolist)
    	fmt.println(string(notlikedata))
    
    	db.not("id in (?)", []int{1, 2, 3, 4, 5}).first(&infolist)
    	notindata, err := json.marshal(infolist)
    	fmt.println(string(notindata))
    	return err
    
    }
    
    

    go or 查询

    // orselect or查询
    func orselect() (err error) {
    	var infolist []test
    	db.where("id = ?", "1").or("id = ?", "2").find(&infolist)
    	ordata, err := json.marshal(infolist)
    	fmt.println(string(ordata))
    
    	return err
    }
    

    go 内联查询 等同于where条件

    // 内联条件
    func neilian() (err error) {
    	var info test
    	db.find(&info, "id = ?", "1")
    	marshal, err := json.marshal(info)
    	fmt.println(string(marshal))
    	return err
    }
    

    go firstorcreate

    获取匹配的第一条记录,否则根据给定的条件创建一个新的记录(仅支持struct or map)

    // firstorcreate 查询条件后的第一条语句 如果为空 就创建一条记录
    func firstorcreate() (err error) {
    	var info test
    	db.where("id = ?", "1000").firstorcreate(&info)
    	marshal, err := json.marshal(info)
    	fmt.println(string(marshal))
    	return err
    }