Skip to content

Commit 9caddef

Browse files
committed
添加 typescript 类型
1 parent d1584ac commit 9caddef

File tree

5 files changed

+63
-58
lines changed

5 files changed

+63
-58
lines changed

databases/20221210.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,9 @@ create table gen_table_column (
456456
column_name varchar(64) default '' comment '字段名称',
457457
column_comment varchar(128) default '' comment '字段描述',
458458
column_type varchar(128) default '' comment '字段类型',
459-
java_type varchar(128) default '' comment 'JAVA类型',
460-
java_field varchar(128) default '' comment 'JAVA属性',
459+
java_type varchar(128) default '' comment 'JAVA 类型',
460+
ts_type varchar(128) default '' comment 'TS 类型',
461+
java_field varchar(128) default '' comment 'JAVA 属性',
461462
is_pk char(1) default '0' comment '是否主键{0=否, 1=是}',
462463
is_increment char(1) default '0' comment '是否自增{0=否, 1=是}',
463464
is_list char(1) default '0' comment '是否列表字段{0=否, 1=是}',

tang-generator/src/main/java/com/tang/generator/entity/GenTableColumn.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class GenTableColumn extends BaseEntity {
1111

1212
@java.io.Serial
13-
private static final long serialVersionUID = 2148893922489984601L;
13+
private static final long serialVersionUID = 3026775149324258963L;
1414

1515
/**
1616
* 编号
@@ -38,12 +38,17 @@ public class GenTableColumn extends BaseEntity {
3838
private String columnType;
3939

4040
/**
41-
* JAVA类型
41+
* JAVA 类型
4242
*/
4343
private String javaType;
4444

4545
/**
46-
* JAVA属性
46+
* TS 类型
47+
*/
48+
private String tsType;
49+
50+
/**
51+
* JAVA 属性
4752
*/
4853
private String javaField;
4954

@@ -151,6 +156,14 @@ public void setJavaType(String javaType) {
151156
this.javaType = javaType;
152157
}
153158

159+
public String getTsType() {
160+
return tsType;
161+
}
162+
163+
public void setTsType(String tsType) {
164+
this.tsType = tsType;
165+
}
166+
154167
public String getJavaField() {
155168
return javaField;
156169
}

tang-generator/src/main/java/com/tang/generator/utils/TableColumnUtils.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private TableColumnUtils() {
5252
*/
5353
public static void initTableColumn(GenTableColumn tableColumn) {
5454
tableColumn.setJavaType(getJavaType(tableColumn.getColumnType()));
55+
tableColumn.setTsType(getTsType(tableColumn.getColumnType()));
5556
tableColumn.setJavaField(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableColumn.getColumnName()));
5657
tableColumn.setIsList("1");
5758
tableColumn.setIsInsert(getIsInsert(tableColumn.getJavaField()));
@@ -81,27 +82,27 @@ private static boolean getIsSuperField(String javaField) {
8182
}
8283

8384
/**
84-
* 获取HTML类型
85+
* 获取 HTML 类型
8586
*
8687
* @param columnName 字段名称
87-
* @return HTML类型
88+
* @return HTML 类型
8889
*/
8990
private static String getHtmlType(String columnName) {
90-
var htmlSelect = "type";
91-
var htmlRadio = "status";
92-
var htmlDate = "time";
91+
var htmlSelect = List.of("type");
92+
var htmlRadio = List.of("status");
93+
var htmlDate = List.of("time", "date");
9394

9495
var htmlType = "input";
95-
// 如果包含type返回select
96-
if (columnName.contains(htmlSelect)) {
97-
htmlType = "select";
98-
}
99-
// 如果包含status返回radio
100-
if (columnName.contains(htmlRadio)) {
96+
// 如果包含 htmlRadio 返回 radio
97+
if (htmlRadio.stream().anyMatch(columnName::contains)) {
10198
htmlType = "radio";
10299
}
103-
// 如果包含time返回date
104-
if (columnName.contains(htmlDate)) {
100+
// 如果包含 htmlSelect 返回 select
101+
if (htmlSelect.stream().anyMatch(columnName::contains)) {
102+
htmlType = "select";
103+
}
104+
// 如果包含 htmlDate 返回 date
105+
if (htmlDate.stream().anyMatch(columnName::contains)) {
105106
htmlType = "date";
106107
}
107108
return htmlType;
@@ -120,18 +121,18 @@ private static String getIsEdit(GenTableColumn tableColumn) {
120121
/**
121122
* 获取是否允许插入
122123
*
123-
* @param javaField java字段
124+
* @param javaField java 字段
124125
* @return 是否允许插入
125126
*/
126127
private static String getIsInsert(String javaField) {
127128
return List.of(NOT_INSERT_FIELD).contains(javaField) ? "0" : "1";
128129
}
129130

130131
/**
131-
* 获取java类型
132+
* 获取 java 类型
132133
*
133134
* @param columnType 字段类型
134-
* @return java类型
135+
* @return java 类型
135136
*/
136137
private static String getJavaType(String columnType) {
137138
if (List.of(STRING_TYPE).stream().anyMatch(columnType::startsWith)) {
@@ -146,4 +147,23 @@ private static String getJavaType(String columnType) {
146147
return "";
147148
}
148149

150+
/**
151+
* 获取 typescript 类型
152+
*
153+
* @param columnType 字段类型
154+
* @return typescript 类型
155+
*/
156+
public static String getTsType(String columnType) {
157+
if (List.of(STRING_TYPE).stream().anyMatch(columnType::startsWith)) {
158+
return "string";
159+
}
160+
if (List.of(NUMBER_TYPE).contains(columnType)) {
161+
return "number";
162+
}
163+
if (List.of(DATE_TYPE).contains(columnType)) {
164+
return "Date";
165+
}
166+
return "";
167+
}
168+
149169
}

tang-generator/src/main/resources/mapper/tool/GenTableColumnMapper.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<result property="columnComment" column="column_comment" />
1010
<result property="columnType" column="column_type" />
1111
<result property="javaType" column="java_type" />
12+
<result property="tsType" column="ts_type" />
1213
<result property="javaField" column="java_field" />
1314
<result property="isPk" column="is_pk" />
1415
<result property="isIncrement" column="is_increment" />
@@ -28,7 +29,7 @@
2829
</resultMap>
2930

3031
<sql id="GenTableColumnVo">
31-
select gtc.column_id, gtc.table_id, gtc.column_name, gtc.column_comment, gtc.column_type, gtc.java_type, gtc.java_field, gtc.is_pk, gtc.is_increment, gtc.is_list, gtc.is_insert, gtc.is_edit, gtc.is_required, gtc.query_type, gtc.html_type, gtc.dict_type, gtc.sort, gtc.create_by, gtc.create_time, gtc.update_by, gtc.update_time, gtc.remark
32+
select gtc.column_id, gtc.table_id, gtc.column_name, gtc.column_comment, gtc.column_type, gtc.java_type, gtc.ts_type, gtc.java_field, gtc.is_pk, gtc.is_increment, gtc.is_list, gtc.is_insert, gtc.is_edit, gtc.is_required, gtc.query_type, gtc.html_type, gtc.dict_type, gtc.sort, gtc.create_by, gtc.create_time, gtc.update_by, gtc.update_time, gtc.remark
3233
from gen_table_column gtc
3334
</sql>
3435

@@ -64,6 +65,7 @@
6465
<if test="columnComment != null and columnComment != ''">column_comment,</if>
6566
<if test="columnType != null and columnType != ''">column_type,</if>
6667
<if test="javaType != null and javaType != ''">java_type,</if>
68+
<if test="tsType != null and tsType != ''">ts_type,</if>
6769
<if test="javaField != null and javaField != ''">java_field,</if>
6870
<if test="isPk != null and isPk != ''">is_pk,</if>
6971
<if test="isIncrement != null and isIncrement != ''">is_increment,</if>
@@ -85,6 +87,7 @@
8587
<if test="columnComment != null and columnComment != ''">#{columnComment},</if>
8688
<if test="columnType != null and columnType != ''">#{columnType},</if>
8789
<if test="javaType != null and javaType != ''">#{javaType},</if>
90+
<if test="tsType != null and tsType != ''">#{tsType},</if>
8891
<if test="javaField != null and javaField != ''">#{javaField},</if>
8992
<if test="isPk != null and isPk != ''">#{isPk},</if>
9093
<if test="isIncrement != null and isIncrement != ''">#{isIncrement},</if>
@@ -110,6 +113,7 @@
110113
<if test="columnComment != null and columnComment != ''">column_comment = #{columnComment},</if>
111114
<if test="columnType != null and columnType != ''">column_type = #{columnType},</if>
112115
<if test="javaType != null and javaType != ''">java_type = #{javaType},</if>
116+
<if test="tsType != null and tsType != ''">ts_type = #{tsType},</if>
113117
<if test="javaField != null and javaField != ''">java_field = #{javaField},</if>
114118
<if test="isPk != null and isPk != ''">is_pk = #{isPk},</if>
115119
<if test="isIncrement != null and isIncrement != ''">is_increment = #{isIncrement},</if>

tang-generator/src/main/resources/vm/vue/types.ts.vm

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
*/
44
export interface $ClassName {
55
#foreach($column in $columnList)
6-
#if ($column.javaType == 'Integer')
7-
$column.javaField: number
8-
#end
9-
#if ($column.javaType == 'Long')
10-
$column.javaField: number
11-
#end
12-
#if ($column.javaType == 'String')
13-
$column.javaField: string
14-
#end
15-
#if ($column.javaType == 'LocalDateTime')
16-
$column.javaField: Date
17-
#end
6+
$column.javaField: $column.tsType
187
#end
198
}
209

@@ -23,18 +12,7 @@ export interface $ClassName {
2312
*/
2413
export interface ${ClassName}Form {
2514
#foreach($column in $columnList)
26-
#if ($column.javaType == 'Integer')
27-
$column.javaField: number
28-
#end
29-
#if ($column.javaType == 'Long')
30-
$column.javaField: number
31-
#end
32-
#if ($column.javaType == 'String')
33-
$column.javaField: string
34-
#end
35-
#if ($column.javaType == 'LocalDateTime')
36-
$column.javaField: Date
37-
#end
15+
$column.javaField: $column.tsType
3816
#end
3917
}
4018

@@ -43,17 +21,6 @@ export interface ${ClassName}Form {
4321
*/
4422
export interface ${ClassName}Query extends PageQuery {
4523
#foreach($column in $columnList)
46-
#if ($column.javaType == 'Integer')
47-
$column.javaField: number
48-
#end
49-
#if ($column.javaType == 'Long')
50-
$column.javaField: number
51-
#end
52-
#if ($column.javaType == 'String')
53-
$column.javaField: string
54-
#end
55-
#if ($column.javaType == 'LocalDateTime')
56-
$column.javaField: Date
57-
#end
24+
$column.javaField: $column.tsType
5825
#end
5926
}

0 commit comments

Comments
 (0)