@@ -85,12 +85,25 @@ type AutoCodeField struct {
85
85
Clearable bool ` json:"clearable"` // 是否可清空
86
86
Sort bool ` json:"sort"` // 是否支持排序
87
87
PrimaryKey bool ` json:"primaryKey"` // 是否主键
88
- DataSource *DataSource ` json:"dataSource"` // 数据源
88
+ DataSource *DataSource ` json:"dataSource"` // 数据源配置(用于关联其他表)
89
89
CheckDataSource bool ` json:"checkDataSource"` // 是否检查数据源
90
90
FieldIndexType string ` json:"fieldIndexType"` // 索引类型
91
91
}
92
92
```
93
93
94
+ ### 4. DataSource 结构体(关联表配置)
95
+
96
+ ``` go
97
+ type DataSource struct {
98
+ DBName string ` json:"dbName"` // 关联的数据库名称
99
+ Table string ` json:"table"` // 关联的表名
100
+ Label string ` json:"label"` // 用于显示的字段名(如name、title等)
101
+ Value string ` json:"value"` // 用于存储的值字段名(通常是id)
102
+ Association int ` json:"association"` // 关联关系:1=一对一,2=一对多
103
+ HasDeletedAt bool ` json:"hasDeletedAt"` // 关联表是否有软删除字段
104
+ }
105
+ ```
106
+
94
107
## 使用示例
95
108
96
109
### 示例1:创建新包和批量创建多个模块
@@ -151,8 +164,15 @@ type AutoCodeField struct {
151
164
"clearable" : true ,
152
165
"sort" : false ,
153
166
"primaryKey" : false ,
154
- "dataSource" : null ,
155
- "checkDataSource" : false ,
167
+ "dataSource" : {
168
+ "dbName" : " gva" ,
169
+ "table" : " sys_users" ,
170
+ "label" : " username" ,
171
+ "value" : " id" ,
172
+ "association" : 2 ,
173
+ "hasDeletedAt" : true
174
+ },
175
+ "checkDataSource" : true ,
156
176
"fieldIndexType" : " "
157
177
},
158
178
{
@@ -360,6 +380,121 @@ type AutoCodeField struct {
360
380
}
361
381
```
362
382
383
+ ### 示例3:模块关联关系配置详解
384
+
385
+ 以下示例展示了如何配置不同类型的关联关系:
386
+
387
+ ``` json
388
+ {
389
+ "packageName" : " order" ,
390
+ "packageType" : " package" ,
391
+ "needCreatedPackage" : true ,
392
+ "needCreatedModules" : true ,
393
+ "packageInfo" : {
394
+ "desc" : " 订单管理模块" ,
395
+ "label" : " 订单管理" ,
396
+ "template" : " package" ,
397
+ "packageName" : " order"
398
+ },
399
+ "modulesInfo" : [
400
+ {
401
+ "package" : " order" ,
402
+ "tableName" : " orders" ,
403
+ "structName" : " Order" ,
404
+ "packageName" : " order" ,
405
+ "description" : " 订单" ,
406
+ "abbreviation" : " order" ,
407
+ "humpPackageName" : " order" ,
408
+ "gvaModel" : true ,
409
+ "autoMigrate" : true ,
410
+ "autoCreateResource" : true ,
411
+ "autoCreateApiToSql" : true ,
412
+ "autoCreateMenuToSql" : true ,
413
+ "autoCreateBtnAuth" : true ,
414
+ "generateWeb" : true ,
415
+ "generateServer" : true ,
416
+ "fields" : [
417
+ {
418
+ "fieldName" : " UserID" ,
419
+ "fieldDesc" : " 下单用户" ,
420
+ "fieldType" : " uint" ,
421
+ "fieldJson" : " userId" ,
422
+ "columnName" : " user_id" ,
423
+ "fieldSearchType" : " EQ" ,
424
+ "form" : true ,
425
+ "table" : true ,
426
+ "desc" : true ,
427
+ "require" : true ,
428
+ "dataSource" : {
429
+ "dbName" : " gva" ,
430
+ "table" : " sys_users" ,
431
+ "label" : " username" ,
432
+ "value" : " id" ,
433
+ "association" : 2 ,
434
+ "hasDeletedAt" : true
435
+ },
436
+ "checkDataSource" : true
437
+ },
438
+ {
439
+ "fieldName" : " ProductID" ,
440
+ "fieldDesc" : " 商品" ,
441
+ "fieldType" : " uint" ,
442
+ "fieldJson" : " productId" ,
443
+ "columnName" : " product_id" ,
444
+ "fieldSearchType" : " EQ" ,
445
+ "form" : true ,
446
+ "table" : true ,
447
+ "desc" : true ,
448
+ "require" : true ,
449
+ "dataSource" : {
450
+ "dbName" : " gva" ,
451
+ "table" : " products" ,
452
+ "label" : " name" ,
453
+ "value" : " id" ,
454
+ "association" : 2 ,
455
+ "hasDeletedAt" : false
456
+ },
457
+ "checkDataSource" : true
458
+ },
459
+ {
460
+ "fieldName" : " Status" ,
461
+ "fieldDesc" : " 订单状态" ,
462
+ "fieldType" : " int" ,
463
+ "fieldJson" : " status" ,
464
+ "columnName" : " status" ,
465
+ "fieldSearchType" : " EQ" ,
466
+ "form" : true ,
467
+ "table" : true ,
468
+ "desc" : true ,
469
+ "require" : true ,
470
+ "dictType" : " order_status"
471
+ }
472
+ ]
473
+ }
474
+ ]
475
+ }
476
+ ```
477
+
478
+ ## DataSource 配置说明
479
+
480
+ ### 关联关系类型
481
+ - ** association: 1** - 一对一关联(如用户与用户档案)
482
+ - ** association: 2** - 一对多关联(如用户与订单)
483
+
484
+ ### 配置要点
485
+ 1 . ** dbName** : 通常为 "gva"(默认数据库)
486
+ 2 . ** table** : 关联表的实际表名
487
+ 3 . ** label** : 用于前端显示的字段(如用户名、商品名称)
488
+ 4 . ** value** : 用于存储关联ID的字段(通常是 "id")
489
+ 5 . ** hasDeletedAt** : 关联表是否支持软删除
490
+ 6 . ** checkDataSource** : 建议设为true,会验证关联表是否存在
491
+
492
+ ### 常见关联场景
493
+ - 用户关联:` {"table": "sys_users", "label": "username", "value": "id"} `
494
+ - 角色关联:` {"table": "sys_authorities", "label": "authorityName", "value": "authorityId"} `
495
+ - 部门关联:` {"table": "sys_departments", "label": "name", "value": "id"} `
496
+ - 分类关联:` {"table": "categories", "label": "name", "value": "id"} `
497
+
363
498
## 重要注意事项
364
499
365
500
1 . ** PackageType** : 只能是 "plugin" 或 "package"
@@ -369,6 +504,7 @@ type AutoCodeField struct {
369
504
5 . ** 搜索类型** : FieldSearchType支持:EQ, NE, GT, GE, LT, LE, LIKE, BETWEEN等
370
505
6 . ** 索引类型** : FieldIndexType支持:index, unique等
371
506
7 . ** GvaModel** : 设置为true时会自动包含ID、CreatedAt、UpdatedAt、DeletedAt字段
507
+ 8 . ** 关联配置** : 使用dataSource时,确保关联表已存在,建议开启checkDataSource验证
372
508
373
509
## 常见错误避免
374
510
0 commit comments