Model Commands
goctl model is one of the components in the tool module under go-zero. It currently supports the recognition of mysql ddl for model layer code generation. It can be selectively generated with or without redis cache through the command line or idea plug-in (supported soon) The code logic.
#
Quick startGenerated by ddl
CURD code can be quickly generated after executing the above command.
Generated by datasource
usermodel_gen.go
usermodel.go
#
Usage#
Generation rulesDefault rule
By default, users will create createTime and updateTime fields (ignoring case and underscore naming style) when creating a table, and the default values are both
CURRENT_TIMESTAMP
, and updateTime supportsON UPDATE CURRENT_TIMESTAMP
. For these two fields,insert
, It will be removed whenupdate
is not in the assignment scope. Of course, if you don't need these two fields, it does not matter.ddl
datasource
Generate code only basic CURD structure.
#
CacheFor the cache, I chose to list it in the form of question and answer. I think this can more clearly describe the function of the cache in the model.
What information will the cache?
For the primary key field cache, the entire structure information will be cached, while for the single index field (except full-text index), the primary key field value will be cached.
Does the data update (
update
) operation clear the cache?Yes, but only clear the information in the primary key cache, why? I won't go into details here.
Why not generate
updateByXxx
anddeleteByXxx
codes based on single index fields?There is no problem in theory, but we believe that the data operations of the model layer are based on the entire structure, including queries. I do not recommend querying only certain fields (no objection), otherwise our cache will be meaningless.
Why not support the code generation layer of
findPageLimit
andfindAll
?At present, I think that in addition to the basic CURD, the other codes are all business-type codes. I think it is better for developers to write according to business needs.
#
Type conversion rulesmysql dataType | golang dataType | golang dataType(if null&&default null) |
---|---|---|
bool | int64 | sql.NullInt64 |
boolean | int64 | sql.NullInt64 |
tinyint | int64 | sql.NullInt64 |
smallint | int64 | sql.NullInt64 |
mediumint | int64 | sql.NullInt64 |
int | int64 | sql.NullInt64 |
integer | int64 | sql.NullInt64 |
bigint | int64 | sql.NullInt64 |
float | float64 | sql.NullFloat64 |
double | float64 | sql.NullFloat64 |
decimal | float64 | sql.NullFloat64 |
date | time.Time | sql.NullTime |
datetime | time.Time | sql.NullTime |
timestamp | time.Time | sql.NullTime |
time | string | sql.NullString |
year | time.Time | sql.NullInt64 |
char | string | sql.NullString |
varchar | string | sql.NullString |
binary | string | sql.NullString |
varbinary | string | sql.NullString |
tinytext | string | sql.NullString |
text | string | sql.NullString |
mediumtext | string | sql.NullString |
longtext | string | sql.NullString |
enum | string | sql.NullString |
set | string | sql.NullString |
json | string | sql.NullString |