Golang操作MySQL json字段优雅写法

背景

如下soc_manpower_shift_forecast_tab的version保存一个json格式的结构体。

sql 复制代码
CREATE TABLE `soc_manpower_shift_forecast_tab` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `station_id` bigint(20) unsigned NOT NULL DEFAULT '0',
 `operator` varchar(128) NOT NULL DEFAULT '',
 `forecast_date` int(10) unsigned NOT NULL DEFAULT '0',
 `solution_type` tinyint(4) unsigned NOT NULL DEFAULT '0',
 `version` text NOT NULL,
 `ctime` int(10) unsigned NOT NULL DEFAULT '0',
 `mtime` int(10) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 unique KEY `uniq_station_id_solution_type_date` (`station_id`,`solution_type`, `forecast_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

一般的写法是golang定义的结构体如下,Version是string类型,使用时json反序列化成结构体,写入数据库时把结构体序列化为string。

go 复制代码
type SocManpowerShiftForecast struct {
	ID           int64        `gorm:"column:id;primaryKey" json:"id"`
	StationID    int64        `gorm:"column:station_id" json:"station_id"`
	Operator     string       `gorm:"column:operator" json:"operator"`
	ForecastDate int64        `gorm:"column:forecast_date" json:"forecast_date"` // 使用更友好的时间类型
	SolutionType int64        `gorm:"column:solution_type" json:"solution_type"`
	Version      string       `gorm:"column:version" json:"version"` // JSON 文本
	Ctime        int64        `gorm:"column:ctime" json:"ctime"`
	Mtime        int64        `gorm:"column:mtime" json:"mtime"`
}

优雅写法

go 复制代码
type VersionInfo struct {
	ShiftList []float64 `json:"shift_list"`
}

func (info *VersionInfo) Scan(value interface{}) error {
	bs, ok := value.([]byte)
	if !ok {
		return errors.New("value is not []byte")
	}
	return json.Unmarshal(bs, info)
}

func (info *VersionInfo) Value() (driver.Value, error) {
	return json.Marshal(info)
}

Version结构体实现Scan和Value方法

go 复制代码
type SocManpowerShiftForecast struct {
	ID           int64        `gorm:"column:id;primaryKey" json:"id"`
	StationID    int64        `gorm:"column:station_id" json:"station_id"`
	Operator     string       `gorm:"column:operator" json:"operator"`
	ForecastDate int64        `gorm:"column:forecast_date" json:"forecast_date"` // 使用更友好的时间类型
	SolutionType int64        `gorm:"column:solution_type" json:"solution_type"`
	Version      *VersionInfo `gorm:"column:version" json:"version"` // JSON 文本
	Ctime        int64        `gorm:"column:ctime" json:"ctime"`
	Mtime        int64        `gorm:"column:mtime" json:"mtime"`
}

数据库表结构体直接使用指针结构体即可,完整测试代码如下:

go 复制代码
package mysql

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"example.com/m/test/testutil"
	"example.com/m/util/stringutil"
	"fmt"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

const SocManpowerShiftForecastTabName = "soc_manpower_shift_forecast_tab"

type SocManpowerShiftForecast struct {
	ID           int64        `gorm:"column:id;primaryKey" json:"id"`
	StationID    int64        `gorm:"column:station_id" json:"station_id"`
	Operator     string       `gorm:"column:operator" json:"operator"`
	ForecastDate int64        `gorm:"column:forecast_date" json:"forecast_date"` // 使用更友好的时间类型
	SolutionType int64        `gorm:"column:solution_type" json:"solution_type"`
	Version      *VersionInfo `gorm:"column:version" json:"version"` // JSON 文本
	Ctime        int64        `gorm:"column:ctime" json:"ctime"`
	Mtime        int64        `gorm:"column:mtime" json:"mtime"`
}

type VersionInfo struct {
	ShiftList []float64 `json:"shift_list"`
}

func (info *VersionInfo) Scan(value interface{}) error {
	bs, ok := value.([]byte)
	if !ok {
		return errors.New("value is not []byte")
	}
	return json.Unmarshal(bs, info)
}

func (info *VersionInfo) Value() (driver.Value, error) {
	return json.Marshal(info)
}

func Test_ScanValue(t *testing.T) {
	Convey("ScanValue", t, func() {
		Convey("ScanValue test1", func() {
			ctx := testutil.NewContext(testutil.NewContextRequest{})
			temp := &SocManpowerShiftForecast{
				ID:           0,
				StationID:    102,
				Operator:     "kf",
				ForecastDate: 1751299200,
				SolutionType: 1,
				Version: &VersionInfo{
					ShiftList: []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0},
				},
				Ctime: 1751299200,
				Mtime: 1751299200,
			}
			db := testutil.GetDBCommon(ctx)
			err := db.Table(SocManpowerShiftForecastTabName).Create(temp).Error
			So(err, ShouldEqual, nil)
			var res []*SocManpowerShiftForecast
			err = db.Table(SocManpowerShiftForecastTabName).Find(&res).Error
			So(err, ShouldEqual, nil)
			fmt.Println(stringutil.Object2String(res))
		})
	})
}

日志如下:

无论是写入还是读取都没有问题

powershell 复制代码
2025/07/12 22:21:23 /Users/workspace/go-utils/test/mysql/mysql_test.go:60
[5.581ms] [rows:1] INSERT INTO `soc_manpower_shift_forecast_tab` (`station_id`,`operator`,`forecast_date`,`solution_type`,`version`,`ctime`,`mtime`) VALUES (102,'kf',1751299200,1,'{"shift_list":[1,2,3,4,5,6]}',1751299200,1751299200)
.
相关推荐
QX_hao8 小时前
【Go】--数据类型
开发语言·后端·golang
h7997108 小时前
redis lua脚本(go)调用教程以及debug调试
redis·golang·lua
趣味编程1118 小时前
go的学习2---》并发编程
学习·golang·perl
disanleya8 小时前
MySQL数据库安装后,如何设置自动化备份策略?
mysql
奥尔特星云大使9 小时前
mysql高可用架构之MHA部署(二)VIP漂移(保姆级)
android·mysql·架构·mha·ip漂移
-Xie-9 小时前
Mysql杂志(三十一)——Join连接算法与子查询、排序优化
数据库·mysql
程序新视界10 小时前
在连表查询场景下,MySQL隐式转换存在的坑
数据库·mysql·dba
咋吃都不胖lyh10 小时前
MySQL 与Power BI 的作用,以及在数据分析中扮演的角色
mysql·数据分析·powerbi
瓯雅爱分享17 小时前
Java+Vue构建的采购招投标一体化管理系统,集成招标计划、投标审核、在线竞价、中标公示及合同跟踪功能,附完整源码,助力企业实现采购全流程自动化与规范化
java·mysql·vue·软件工程·源代码管理
咋吃都不胖lyh20 小时前
SQL-多对多关系
android·mysql·数据分析