掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
?dao?命令是?CLI?中最頻繁使用、也是框架設(shè)計(jì)的工程規(guī)范能否準(zhǔn)確落地的關(guān)鍵命令。該命令用于生成?dao?數(shù)據(jù)訪問(wèn)對(duì)象、?do?數(shù)據(jù)轉(zhuǎn)化模型及?entity?實(shí)例數(shù)據(jù)模型GO代碼文件。由于該命令的參數(shù)、選項(xiàng)較多,我們推薦使用配置文件來(lái)管理生成規(guī)則。

創(chuàng)新互聯(lián)專注于秦都網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供秦都營(yíng)銷型網(wǎng)站建設(shè),秦都網(wǎng)站制作、秦都網(wǎng)頁(yè)設(shè)計(jì)、秦都網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造秦都網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供秦都網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
大部分場(chǎng)景下,進(jìn)入項(xiàng)目根目錄執(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"
| 名稱 | 必須 | 默認(rèn)值 | 含義 | 示例 |
|---|---|---|---|---|
?gfcli.gen.dao? |
是 | ?dao?代碼生成配置項(xiàng),可以有多個(gè)配置項(xiàng)構(gòu)成數(shù)組,支持多個(gè)數(shù)據(jù)庫(kù)生成。不同的數(shù)據(jù)庫(kù)可以設(shè)置不同的生成規(guī)則,例如可以生成到不同的位置或者文件。 |
- | |
?link? |
是 | 分為兩部分,第一部分表示你連接的數(shù)據(jù)庫(kù)類型?mysql?, ?postgresql?等, 第二部分就是連接數(shù)據(jù)庫(kù)的dsn信息。 |
- | |
?path? |
?internal? |
生成?dao?和?model?文件的存儲(chǔ)目錄地址。 |
?./app? |
|
?group? |
?default? |
在數(shù)據(jù)庫(kù)配置中的數(shù)據(jù)庫(kù)分組名稱。只能配置一個(gè)名稱。數(shù)據(jù)庫(kù)在配置文件中的分組名稱往往確定之后便不再修改。 | ?defalut? ? order? ? user? |
|
?prefix? |
生成數(shù)據(jù)庫(kù)對(duì)象及文件的前綴,以便區(qū)分不同數(shù)據(jù)庫(kù)或者不同數(shù)據(jù)庫(kù)中的相同表名,防止數(shù)據(jù)表同名覆蓋。 | ?order_? ? user_? |
||
?removePrefix? |
刪除數(shù)據(jù)表的指定前綴名稱。多個(gè)前綴以?,?號(hào)分隔。 |
?gf_? |
||
?tables? |
指定當(dāng)前數(shù)據(jù)庫(kù)中需要執(zhí)行代碼生成的數(shù)據(jù)表。如果為空,表示數(shù)據(jù)庫(kù)的所有表都會(huì)生成。 | ?user?, ?user_detail? |
||
?tablesEx? |
?Tables Excluding?,指定當(dāng)前數(shù)據(jù)庫(kù)中需要排除代碼生成的數(shù)據(jù)表。 |
?product?, ?order? |
||
?jsonCase? |
?CamelLower? |
指定?model?中生成的數(shù)據(jù)實(shí)體對(duì)象中?json?標(biāo)簽名稱規(guī)則,參數(shù)不區(qū)分大小寫。參數(shù)可選為:?Camel?、?CamelLower?、?Snake?、?SnakeScreaming?、?SnakeFirstUpper?、?Kebab?、?KebabScreaming?。 |
?Snake? |
|
?stdTime? |
?false? |
當(dāng)數(shù)據(jù)表字段類型為時(shí)間類型時(shí),代碼生成的屬性類型使用標(biāo)準(zhǔn)庫(kù)的?time.Time?而不是框架的?*gtime.Time?類型。 |
?true? |
|
?gJsonSupport? |
?false? |
當(dāng)數(shù)據(jù)表字段類型為?JSON?類型時(shí),代碼生成的屬性類型使用?*gjson.Json?類型。 |
?true? |
|
?overwriteDao? |
?false? |
每次生成?dao?代碼時(shí)是否重新生成覆蓋?dao/internal?目錄外層的文件。注意?dao/internal?目錄外層的文件可能由開發(fā)者自定義擴(kuò)展了功能,覆蓋可能會(huì)產(chǎn)生風(fēng)險(xiǎn)。 |
?true? |
|
?importPrefix? |
通過(guò)?go.mod?自動(dòng)檢測(cè) |
用于指定生成Go文件的import路徑前綴。特別是針對(duì)于不是在項(xiàng)目根目錄下使用?gen dao?命令,或者想要將代碼文件生成到自定義的其他目錄,這個(gè)時(shí)候配置該參數(shù)十分必要。 |
?github.com/gogf/gf? |
|
?descriptionTag? |
?false? |
用于指定是否為數(shù)據(jù)模型結(jié)構(gòu)體屬性增加?desription?的標(biāo)簽,內(nèi)容為對(duì)應(yīng)的數(shù)據(jù)表字段注釋。 |
?true? |
|
?noModelComment? |
?false? |
用于指定是否關(guān)閉數(shù)據(jù)模型結(jié)構(gòu)體屬性的注釋自動(dòng)生成,內(nèi)容為數(shù)據(jù)表對(duì)應(yīng)字段的注釋。 | ?true? |
倉(cāng)庫(kù)地址:https://github.com/gogf/focus-single
1、以下3個(gè)目錄的文件由?dao?命令生成:
| 路徑 | 說(shuō)明 | 詳細(xì)介紹 |
?/internal/model/entity? |
數(shù)據(jù)模型 | 數(shù)據(jù)模型由工具維護(hù),用戶不能修改。 工具每次生成代碼文件將會(huì)覆蓋該目錄。 |
?/internal/service/internal/do? |
數(shù)據(jù)轉(zhuǎn)換模型 | 數(shù)據(jù)轉(zhuǎn)換模型用于業(yè)務(wù)模型到數(shù)據(jù)模型的轉(zhuǎn)換,由工具維護(hù),用戶不能修改。 工具每次生成代碼文件將會(huì)覆蓋該目錄。 |
?/internal/service/internal/dao? |
數(shù)據(jù)操作對(duì)象 | 通過(guò)對(duì)象方式訪問(wèn)底層數(shù)據(jù)源,底層基于?ORM?組件實(shí)現(xiàn)。往往需要結(jié)合?entity?和?do?通用使用。該目錄下的文件開發(fā)者可擴(kuò)展修改,但是往往沒(méi)這種必要。 |
2、?model?中的模型分為兩類:數(shù)據(jù)模型和業(yè)務(wù)模型。
數(shù)據(jù)模型:通過(guò)?CLI?工具自動(dòng)生成 ?model/entity? 目錄文件,數(shù)據(jù)庫(kù)的數(shù)據(jù)表都會(huì)生成到該目錄下,這個(gè)目錄下的文件對(duì)應(yīng)的模型為數(shù)據(jù)模型。數(shù)據(jù)模型即與數(shù)據(jù)表一一對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu),開發(fā)者往往不需要去修改并且也不應(yīng)該去修改,數(shù)據(jù)模型只有在數(shù)據(jù)表結(jié)構(gòu)變更時(shí)通過(guò)?CLI?工具自動(dòng)更新。數(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)行命名,一個(gè)數(shù)據(jù)表一個(gè)文件及其一個(gè)對(duì)應(yīng)的?DAO?對(duì)象。操作數(shù)據(jù)表即是通過(guò)?DAO?對(duì)象以及相關(guān)操作方法實(shí)現(xiàn)。?dao?操作采用規(guī)范化設(shè)計(jì),必須傳遞?ctx?參數(shù),并在生成的代碼中必須通過(guò)?Ctx?或者?Transaction?方法創(chuàng)建對(duì)象來(lái)鏈?zhǔn)讲僮鲾?shù)據(jù)表。
4、同時(shí),?dao?采用了工程化規(guī)范設(shè)計(jì)??梢钥吹?,?dao/do?是?service?層內(nèi)部模塊,也就是說(shuō),只有?service?層才能訪問(wèn)?dao?數(shù)據(jù)操作對(duì)象并與底層數(shù)據(jù)庫(kù)交互。這樣通過(guò)工具落地規(guī)范的方式避免了項(xiàng)目中隨處調(diào)用?dao?操作數(shù)據(jù)庫(kù)的情況,規(guī)范了請(qǐng)求調(diào)用鏈。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流