新聞中心
?dao?命令是?CLI?中最頻繁使用、也是框架設(shè)計的工程規(guī)范能否準(zhǔn)確落地的關(guān)鍵命令。該命令用于生成?dao?數(shù)據(jù)訪問對象、?do?數(shù)據(jù)轉(zhuǎn)化模型及?entity?實例數(shù)據(jù)模型GO代碼文件。由于該命令的參數(shù)、選項較多,我們推薦使用配置文件來管理生成規(guī)則。

創(chuàng)新互聯(lián)專注于秦都網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供秦都營銷型網(wǎng)站建設(shè),秦都網(wǎng)站制作、秦都網(wǎng)頁設(shè)計、秦都網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造秦都網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供秦都網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
一、使用方式
大部分場景下,進(jìn)入項目根目錄執(zhí)行 ?gf gen dao? 即可。以下為命令行幫助信息。
$ gf gen dao -h
USAGE
gf gen dao [OPTION]
OPTION
-p, --path directory path for generated files
-l, --link database configuration, the same as the ORM configuration of GoFrame
-t, --tables generate models only for given tables, multiple table names separated with ','
-e, --tablesEx generate models excluding given tables, multiple table names separated with ','
-g, --group specifying the configuration group name of database for generated ORM instance,
it's not necessary and the default value is "default"
-f, --prefix add prefix for all table of specified link/database tables
-r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
-j, --jsonCase generated json tag case for model struct, cases are as follows:
| Case | Example |
|---------------- |--------------------|
| Camel | AnyKindOfString |
| CamelLower | anyKindOfString | default
| Snake | any_kind_of_string |
| SnakeScreaming | ANY_KIND_OF_STRING |
| SnakeFirstUpper | rgb_code_md5 |
| Kebab | any-kind-of-string |
| KebabScreaming | ANY-KIND-OF-STRING |
-i, --importPrefix custom import prefix for generated go files
-s, --stdTime use time.Time from stdlib instead of gtime.Time for generated time/date fields of tables
-n, --gJsonSupport use gJsonSupport to use *gjson.Json instead of string for generated json fields of tables
-o, --overwriteDao overwrite all dao files both inside/outside internal folder
-d, --descriptionTag add comment to description tag for each field
-k, --noJsonTag commandGenDaoBriefNoJsonTag
-m, --noModelComment no model comment will be added for each field
-h, --help more information about this command
EXAMPLE
gf gen dao
gf gen dao -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
gf gen dao -p ./model -c config.yaml -g user-center -t user,user_detail,user_login
gf gen dao -r user_
CONFIGURATION SUPPORT
Options are also supported by configuration file.
It's suggested using configuration file instead of command line arguments making producing.
The configuration node name is "gfcli.gen.dao", which also supports multiple databases, for example(config.yaml):
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "order,products"
jsonCase: "CamelLower"
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
path: "./my-app"
prefix: "primary_"
tables: "user, userDetail"
二、配置示例
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "order,products"
jsonCase: "CamelLower"
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
path: "./my-app"
prefix: "primary_"
tables: "user, userDetail"
三、參數(shù)說明
| 名稱 | 必須 | 默認(rèn)值 | 含義 | 示例 |
|---|---|---|---|---|
?gfcli.gen.dao? |
是 | ?dao?代碼生成配置項,可以有多個配置項構(gòu)成數(shù)組,支持多個數(shù)據(jù)庫生成。不同的數(shù)據(jù)庫可以設(shè)置不同的生成規(guī)則,例如可以生成到不同的位置或者文件。 |
- | |
?link? |
是 | 分為兩部分,第一部分表示你連接的數(shù)據(jù)庫類型?mysql?, ?postgresql?等, 第二部分就是連接數(shù)據(jù)庫的dsn信息。 |
- | |
?path? |
?internal? |
生成?dao?和?model?文件的存儲目錄地址。 |
?./app? |
|
?group? |
?default? |
在數(shù)據(jù)庫配置中的數(shù)據(jù)庫分組名稱。只能配置一個名稱。數(shù)據(jù)庫在配置文件中的分組名稱往往確定之后便不再修改。 | ?defalut? ? order? ? user? |
|
?prefix? |
生成數(shù)據(jù)庫對象及文件的前綴,以便區(qū)分不同數(shù)據(jù)庫或者不同數(shù)據(jù)庫中的相同表名,防止數(shù)據(jù)表同名覆蓋。 | ?order_? ? user_? |
||
?removePrefix? |
刪除數(shù)據(jù)表的指定前綴名稱。多個前綴以?,?號分隔。 |
?gf_? |
||
?tables? |
指定當(dāng)前數(shù)據(jù)庫中需要執(zhí)行代碼生成的數(shù)據(jù)表。如果為空,表示數(shù)據(jù)庫的所有表都會生成。 | ?user?, ?user_detail? |
||
?tablesEx? |
?Tables Excluding?,指定當(dāng)前數(shù)據(jù)庫中需要排除代碼生成的數(shù)據(jù)表。 |
?product?, ?order? |
||
?jsonCase? |
?CamelLower? |
指定?model?中生成的數(shù)據(jù)實體對象中?json?標(biāo)簽名稱規(guī)則,參數(shù)不區(qū)分大小寫。參數(shù)可選為:?Camel?、?CamelLower?、?Snake?、?SnakeScreaming?、?SnakeFirstUpper?、?Kebab?、?KebabScreaming?。 |
?Snake? |
|
?stdTime? |
?false? |
當(dāng)數(shù)據(jù)表字段類型為時間類型時,代碼生成的屬性類型使用標(biāo)準(zhǔn)庫的?time.Time?而不是框架的?*gtime.Time?類型。 |
?true? |
|
?gJsonSupport? |
?false? |
當(dāng)數(shù)據(jù)表字段類型為?JSON?類型時,代碼生成的屬性類型使用?*gjson.Json?類型。 |
?true? |
|
?overwriteDao? |
?false? |
每次生成?dao?代碼時是否重新生成覆蓋?dao/internal?目錄外層的文件。注意?dao/internal?目錄外層的文件可能由開發(fā)者自定義擴展了功能,覆蓋可能會產(chǎn)生風(fēng)險。 |
?true? |
|
?importPrefix? |
通過?go.mod?自動檢測 |
用于指定生成Go文件的import路徑前綴。特別是針對于不是在項目根目錄下使用?gen dao?命令,或者想要將代碼文件生成到自定義的其他目錄,這個時候配置該參數(shù)十分必要。 |
?github.com/gogf/gf? |
|
?descriptionTag? |
?false? |
用于指定是否為數(shù)據(jù)模型結(jié)構(gòu)體屬性增加?desription?的標(biāo)簽,內(nèi)容為對應(yīng)的數(shù)據(jù)表字段注釋。 |
?true? |
|
?noModelComment? |
?false? |
用于指定是否關(guān)閉數(shù)據(jù)模型結(jié)構(gòu)體屬性的注釋自動生成,內(nèi)容為數(shù)據(jù)表對應(yīng)字段的注釋。 | ?true? |
四、使用示例
倉庫地址:https://github.com/gogf/focus-single
1、以下3個目錄的文件由?dao?命令生成:
| 路徑 | 說明 | 詳細(xì)介紹 |
?/internal/model/entity? |
數(shù)據(jù)模型 | 數(shù)據(jù)模型由工具維護(hù),用戶不能修改。 工具每次生成代碼文件將會覆蓋該目錄。 |
?/internal/service/internal/do? |
數(shù)據(jù)轉(zhuǎn)換模型 | 數(shù)據(jù)轉(zhuǎn)換模型用于業(yè)務(wù)模型到數(shù)據(jù)模型的轉(zhuǎn)換,由工具維護(hù),用戶不能修改。 工具每次生成代碼文件將會覆蓋該目錄。 |
?/internal/service/internal/dao? |
數(shù)據(jù)操作對象 | 通過對象方式訪問底層數(shù)據(jù)源,底層基于?ORM?組件實現(xiàn)。往往需要結(jié)合?entity?和?do?通用使用。該目錄下的文件開發(fā)者可擴展修改,但是往往沒這種必要。 |
2、?model?中的模型分為兩類:數(shù)據(jù)模型和業(yè)務(wù)模型。
數(shù)據(jù)模型:通過?CLI?工具自動生成 ?model/entity? 目錄文件,數(shù)據(jù)庫的數(shù)據(jù)表都會生成到該目錄下,這個目錄下的文件對應(yīng)的模型為數(shù)據(jù)模型。數(shù)據(jù)模型即與數(shù)據(jù)表一一對應(yīng)的數(shù)據(jù)結(jié)構(gòu),開發(fā)者往往不需要去修改并且也不應(yīng)該去修改,數(shù)據(jù)模型只有在數(shù)據(jù)表結(jié)構(gòu)變更時通過?CLI?工具自動更新。數(shù)據(jù)模型由?CLI?工具生成及統(tǒng)一維護(hù)。
業(yè)務(wù)模型:業(yè)務(wù)模型即是與業(yè)務(wù)相關(guān)的數(shù)據(jù)結(jié)構(gòu),按需定義,例如?service?的輸入輸出數(shù)據(jù)結(jié)構(gòu)定義、內(nèi)部的一些數(shù)據(jù)結(jié)構(gòu)定義等。業(yè)務(wù)模型由開發(fā)者根據(jù)業(yè)務(wù)需要自行定義維護(hù),定義到?model?目錄下。
3、?dao?中的文件按照數(shù)據(jù)表名稱進(jìn)行命名,一個數(shù)據(jù)表一個文件及其一個對應(yīng)的?DAO?對象。操作數(shù)據(jù)表即是通過?DAO?對象以及相關(guān)操作方法實現(xiàn)。?dao?操作采用規(guī)范化設(shè)計,必須傳遞?ctx?參數(shù),并在生成的代碼中必須通過?Ctx?或者?Transaction?方法創(chuàng)建對象來鏈?zhǔn)讲僮鲾?shù)據(jù)表。
4、同時,?dao?采用了工程化規(guī)范設(shè)計??梢钥吹剑?dao/do?是?service?層內(nèi)部模塊,也就是說,只有?service?層才能訪問?dao?數(shù)據(jù)操作對象并與底層數(shù)據(jù)庫交互。這樣通過工具落地規(guī)范的方式避免了項目中隨處調(diào)用?dao?操作數(shù)據(jù)庫的情況,規(guī)范了請求調(diào)用鏈。
當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame開發(fā)工具-數(shù)據(jù)規(guī)范
標(biāo)題URL:http://fisionsoft.com.cn/article/cdhdgeh.html


咨詢
建站咨詢
