2 回答
TA贡献1807条经验 获得超9个赞
雷姆普拉斯与"github.com/go-sql-driver/mysql""gorm.io/driver/mysql"
import (
"encoding/json"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"net/http"
)
var DB *gorm.DB
var err error
const DNS = "root:654321cg@tcp(127.0.0.1:3306)/resourcesdb?charset=utf8&parseTime=True&loc=Local"
...
func InitialMigration() {
DB, err = gorm.Open(mysql.Open(DNS), &gorm.Config{})
if err != nil {
panic("Cannot connect to DB")
}
DB.AutoMigrate(&Client{})
}
func createClient(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var client Client
json.NewDecoder(r.Body).Decode(&client)
DB.Create(&client)
json.NewEncoder(w).Encode(client)
}
- 2 回答
- 0 关注
- 107 浏览
添加回答
举报