> For the complete documentation index, see [llms.txt](https://goooqo.docs.doyto.win/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://goooqo.docs.doyto.win/zh/entity-mapping/entity-object.md).

# 实体对象

## 示例

```go
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语句构建提供表名和列名。

实体对象需要实现以下接口：

```go
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`对应的增删查改语句为：

```sql
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 = ?;
```
