Establish Data Table
Execute the following sql statement in your database to create a data table named users to store user information.
CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password CHAR(32) NOT NULL,
email VARCHAR(100),
created_at DATETIME,
updated_at DATETIME
);
| Field Name | Type | Description |
|---|---|---|
id | INT UNSIGNED | Primary key, auto-increment, uniquely identifies the user |
username | VARCHAR(50) | Username, cannot be null |
password | CHAR(32) | User password hash value, fixed length of 32 characters, cannot be null |
email | VARCHAR(100) | User's email address, can be null |
created_at | DATETIME | Creation time |
updated_at | DATETIME | Record last update time |
Generate Data Model
Modify the tool configuration file:
hack/config.yaml
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/star"
Do not confuse
hack/config.yamlwithmanifest/config/config.yaml; the former is a configuration file for development tools, while the latter is for business configuration.
Execute the GF-CLI command to generate the data model:
$ gf gen dao
Upon successful execution, four files will be generated in the model and dao layers:
internal/model/do/users.go
internal/model/entity/users.go
internal/dao/internal/users.go
internal/dao/users.go
The model layer is used by GoFrame to manage data structures, corresponding one-to-one with the data table, and should not be modified by users. model/do/users.go is used as a data writing object, adopting a generic design for easy data storage; model/entity/users.go is used as a data reading object, with types consistent with the data table.
The dao layer manages data access objects, with GoFrame ORM using it to perform CRUD operations on the data table. dao/internal/users.go holds internal object implementations, should not be modified by users, and is not exposed. dao/users.go instantiates and exposes data access objects, where users can add custom methods.