目录案例查询班级get请求查询学生get请求案例 package main import ( _ "GitHub.com/Go-sql-driver/Mysql" "gi
package main
import (
_ "GitHub.com/Go-sql-driver/Mysql"
"github.com/jinzhu/gORM"
"github.com/gin-gonic/gin"
)
//班级-学生:一对多
type Class struct {
gorm.Model
ClassName string
Students []Student //班级有多个学生
}
//学生-学生卡:一对一
type Student struct {
gorm.Model
StudentName string
ClassID uint //学生属于一个班级
IDCard IDCard // 一个学生只有一个学生卡
Teachers []Teacher `gorm:"many2many:Student_Teacher;"` //一个学生有多个老师
}
type IDCard struct {
gorm.Model
StudentID uint //一张卡只属于一个学生
Money int //卡里余额
}
//老师-学生:多对多
type Teacher struct {
gorm.Model
TeacherName string
Students []Student `gorm:"many2many:Student_Teacher;"` //老师教多个学生
}
func main() {
db, _ := gorm.Open("mysql", "root:admin123@/beego_test?charset=utf8&parseTime=True&loc=Local")
db.AutoMigrate(&Class{},&Student{},&IDCard{},Teacher{})
defer db.Close()
// 测试数据
//i := IDCard{
// Money:99,
//}
//s := Student{
// StudentName:"chary",
// IDCard : i,
//}
//c := Class{
// ClassName:"超神学院",
// Students: []Student{s},
//}
//t := Teacher{
// TeacherName:"雅典啦老师",
// Students: []Student{s},
//}
//
//_ = db.Create(&c).Error
//_ = db.Create(&t).Error
r:=gin.Default()
r.POST("/student", func(c *gin.Context) {
var student Student
_ = c.BindJSON(&student)
db.Create(&student)
})
r.GET("/student/:ID", func(c *gin.Context) {
id := c.Param("ID")
var student Student
_ = c.BindjsON(&student)
db.Preload("Teachers").Preload("IDCard").First(&student,"id=?",id)
c.JSON(200,gin.H{
"msg":student,
})
})
r.GET("/class/:ID", func(c *gin.Context) {
id := c.Param("ID")
var class Class
_ = c.BindJSON(&class)
db.Preload("Students").Preload("Students.Teachers").First(&class,"id=?",id)
c.JSON(200,gin.H{
"msg":class,
})
})
_ = r.Run(":8080")
}
以上就是golang gorm实现get请求查询案例测试的详细内容,更多关于golang gorm测试get请求查询的资料请关注编程网其它相关文章!
--结束END--
本文标题: golang gorm实现get请求查询案例测试
本文链接: https://lsjlt.com/news/146199.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0