This commit is contained in:
tcq 2023-12-14 17:03:47 +08:00
parent a73f5af4b4
commit 39e05c84b0
3 changed files with 117 additions and 0 deletions

14
go.mod Normal file
View File

@ -0,0 +1,14 @@
module mysql_gorm_json
go 1.21.1
require (
gorm.io/driver/mysql v1.5.2
gorm.io/gorm v1.25.5
)
require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
)

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

92
main.go Normal file
View File

@ -0,0 +1,92 @@
package main
import (
"database/sql/driver"
"encoding/json"
"fmt"
"log"
"os"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
func main() {
dsn := "root:123456@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local"
db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{ // table name prefix, table for `User` would be `t_users`
SingularTable: true, // use singular table name, table for `User` would be `user` with this option enabled
},
Logger: logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
Colorful: true,
LogLevel: logger.LogLevel(4), // Log level
},
),
})
//
db.AutoMigrate(&UserEntity{})
//u := UserEntity{
// Name: "张三3",
// Profile: Profile{
// Email: "233@qq.com",
// Mobile: "120",
// },
//}
//db.Create(&u)
var user UserEntity
db.Debug().Where("class->'$.name' like (?)", "%zhang%").First(&user)
fmt.Println("用户", user)
//user.Profile.Status = "请假中"
//user.Class.Number = 1
//user.Class.Name = "zhangsan"
//db.Updates(&user)
}
type Profile struct {
Email string `json:"email"`
Mobile string `json:"mobile"`
Status string `json:"status"`
}
type UserEntity struct {
gorm.Model
Name string `json:"name"`
Profile Profile `json:"profile" gorm:"type:json;comment:'个人信息'"`
Class Class `gorm:"column:class;type:json;comment:class" json:"class"`
}
type Class struct {
Name string `json:"name"`
Number int `json:"number"`
}
// Value 存储数据的时候转换为字符串
func (t Class) Value() (driver.Value, error) {
return json.Marshal(t)
}
// Scan 读取数据的时候转换为json
func (t *Class) Scan(value interface{}) error {
return json.Unmarshal(value.([]byte), &t)
}
// Value 存储数据的时候转换为字符串
func (t Profile) Value() (driver.Value, error) {
return json.Marshal(t)
}
// Scan 读取数据的时候转换为json
func (t *Profile) Scan(value interface{}) error {
return json.Unmarshal(value.([]byte), &t)
}
func (t *UserEntity) TableName() string {
return "user"
}