GoooQo v0.2.3
首页仓库演示
简体中文
简体中文
  • GoooQo介绍
  • 快速上手
  • 接口使用
    • 数据库连接
    • 事务
    • 增删查改接口
  • 实体映射
    • 实体对象
    • 关联实体
  • 查询条件构建
    • 查询对象定义
      • 谓词后缀字段
      • 逻辑后缀字段
      • 子查询字段
      • ER关系字段
      • 自定义字段
    • 分页排序对象
  • 聚合查询
    • 视图对象
    • 聚合查询对象
    • 自然连接
    • 外连接
  • 高级用法
    • 数据库方言
    • 锁
  • 相关资源
    • 文章
      • 从ORM到OQM:一种基于对象的SQL语句构造方案
      • 在GoooQo中如何表示`select * from user where id = ? or name = ? and age = ?`
由 GitBook 提供支持
在本页
  • 示例
  • 定义

这有帮助吗?

在GitHub上编辑
  1. 实体映射

实体对象

示例

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 = ?;
上一页增删查改接口下一页关联实体

最后更新于8个月前

这有帮助吗?