gorm的使用
type User struct {
gorm.Model
Name string
Age int
Sex bool
}
func main() {
db, err := gorm.Open("mysql", "root:password@/dbname?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic("failed to connect database")
}
//自动创建或更新数据库表结构
db.AutoMigrate(&User{})
defer db.Close()
ginServer := gin.Default()
ginServer.POST("/user", func(c *gin.Context) {
var user User
_ = c.BindJSON(&user)
db.Create(&user)
})
ginServer.GET("/user/:Name", func(c *gin.Context) {
name := c.Param("Name")
var user User
db.First(&user, "name = ?", name)
c.JSON(http.StatusOK, user)
})
ginServer.Run(":8080")
}
post
get