zoukankan      html  css  js  c++  java
  • gorm关联关系

    package main
    
    import (
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"github.com/jinzhu/gorm"
    	_ "github.com/jinzhu/gorm/dialects/mysql"
    	"net/http"
    )
    
    type Class struct {
    	// 班级,一个学生有一个班级,一个班级有多个学生
    	gorm.Model
    	ClassName string    `json:"class_name"`
    	Students  []Student `json:"students"` // 一个班级有多个学生,一对多关系
    }
    
    type Student struct {
    	// 学生
    	gorm.Model
    	StudentName string `json:"student_name"`
    	Class       Class  `json:"class"`
    	ClassID     uint   `json:"class_id"` // 一个学生有一个班级,对应一个班级有多个学生,学生通过ClassID来知道是属于哪个班级的
    	IDCard      IDCard `json:"id_card"`  // 一个学生对应一个身份证,一对一关系
    	// 一个学生有多个老师, 与老师建立多对多的关系
    	Teachers []Teacher `gorm:"many2many:student_teacher" json:"teachers"` // 会产生一个关联表
    }
    
    type IDCard struct {
    	// 身份证
    	gorm.Model
    	Num       int  `json:"num"`
    	StudentID uint `json:"student_id"`
    }
    
    type Teacher struct {
    	// 老师
    	gorm.Model
    	TeacherName string `json:"teacher_name"`
    	// 一个老师有多个学生,与学生建立多对多的关系
    	Students []Student `gorm:"many2many:student_teacher" json:"students"` // 会产生一个关联表
    }
    
    func main() {
    	db, err := gorm.Open("mysql", "root:leon110...@tcp(10.227.10.172:3306)/cache?charset=utf8&parseTime=True")
    	if err != nil {
    		panic(err.Error())
    	}
    	defer db.Close()
    	//card := IDCard{
    	//	Num: 12345,
    	//}
    	//s := Student{
    	//	StudentName: "alex",
    	//	IDCard:      card,
    	//	Teachers: []Teacher{t},
    	//}
    	//
    	//t := Teacher{
    	//	TeacherName: "qm",
    	//	Students: []Student{s},
    	//}
    	//C := Class{
    	//	ClassName: "xueqianban",
    	//	Students:  []Student{s}, // 给班级里面添加学生
    	//}
    
    	db.AutoMigrate(&IDCard{}, &Teacher{}, &Class{}, &Student{})
    	// err = db.Create(&card).Error
    	//db.Create(&C)
    	//db.Create(&t)
    	r := gin.Default()
    	r.POST("/student", func(context *gin.Context) {
    		var student Student
    		_ = context.BindJSON(&student)
    		db.Create(&student)
    		context.JSON(http.StatusOK, "ok")
    	})
    	r.GET("/student/:ID", func(context *gin.Context) {
    		id := context.Param("ID")
    		fmt.Printf("id=%s
    ", id)
    		var student Student
    		_ = context.BindJSON(&student)
    		// 预加载
    		db.Preload("Teachers").Preload("IDCard").Preload("Class").First(&student, "id = ?", id)
    		context.JSON(http.StatusOK, gin.H{
    			"data": student,
    		})
    	})
    
    	r.GET("/class/:ID", func(context *gin.Context) {
    		id := context.Param("ID")
    		var c Class
    		// 嵌套预加载
    		db.Preload("Students").Preload("Students.IDCard").Preload("Students.Teachers").First(&c, "id=?", id)
    		context.JSON(http.StatusOK, gin.H{
    			"data": c,
    		})
    	})
    	r.Run(":8888")
    }
    
    
    
  • 相关阅读:
    大整数取模
    HDU 4527 小明系列故事——玩转十滴水
    HDU 3293
    Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    Windows anaconda 运行yolov3
    AttributeError: module 'tensorflow.python.training.checkpointable' has no attribute 'CheckpointableBase'
    40行代码的人脸识别实践——RuntimeError: Error while calling cudnnCreate
    tensorboard 监控指标可视化
    tensorflow实战Google深度学习框架 第292页的程序 完整版 以及计算图可视化
    tensroboard 结合命名控件,同一命名空间的节点会折叠成一个节点
  • 原文地址:https://www.cnblogs.com/forsaken627/p/13604565.html
Copyright © 2011-2022 走看看