import (
. "github.com/doytowin/goooqo"
)
type UserEntity struct {
Int64Id
Name *string `json:"name,omitempty"`
Score *int `json:"score,omitempty"`
Memo *string `json:"memo,omitempty"`
}
func (u UserEntity) GetTableName() string {
return "t_user"
}
定义
实体对象用于为GoooQo中的CRUD语句构建提供表名和列名。
实体对象需要实现以下接口:
package core
type Entity interface {
GetId() any
// SetId set id to self.
// self: the pointer points to the current entity.
// id: type could be int, int64, or string so far.
SetId(self any, id any) error
}
package rdb
import "github.com/doytowin/goooqo/core"
type RdbEntity interface {
core.Entity
GetTableName() string
}
GetId用于构建UPDATE语句。
SetId用于将生成的ID注入到实体。
GetTableName用于提供实体对应的表名。
实体中的每个字段需要与表中的一列相对应。
GoooQo提供了两个Entity的实现以简化实体定义:IntId和Int64Id。
示例中UserEntity对应的增删查改语句为:
SELECT id, name, score, memo FROM t_user;
INSERT INTO t_user (name, score, memo) VALUES (?, ?, ?)
UPDATE t_user SET name = ?, score = ?, memo = ? WHERE id = ?;
DELETE FROM t_user WHERE id = ?;