Selaa lähdekoodia

Merge branch 'master' of http://192.168.1.222:3000/hnsh-smart-steward/smart-steward-api

闪电 4 viikkoa sitten
vanhempi
commit
14dca417ff

+ 26 - 0
smart-steward-api/src/main/java/com/smartSteward/web/controller/ControllerException.java

@@ -0,0 +1,26 @@
1
+package com.smartSteward.web.controller;
2
+
3
+import com.smartSteward.common.core.AjaxResult;
4
+import org.springframework.web.bind.MethodArgumentNotValidException;
5
+import org.springframework.web.bind.annotation.ControllerAdvice;
6
+import org.springframework.web.bind.annotation.ExceptionHandler;
7
+import org.springframework.web.bind.annotation.ResponseBody;
8
+
9
+import java.util.Objects;
10
+
11
+/**
12
+ * 全局异常处理类
13
+ * @author smartSteward
14
+ */
15
+@ControllerAdvice
16
+public class ControllerException {
17
+
18
+    private final static String EXCEPTION_MSG_KEY = "Exception message : ";
19
+
20
+    @ResponseBody
21
+    @ExceptionHandler(MethodArgumentNotValidException.class)
22
+    public Object handleValidException(MethodArgumentNotValidException e) {
23
+        //将错误信息返回给前台
24
+        return AjaxResult.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
25
+    }
26
+}

+ 93 - 7
smart-steward-api/src/main/resources/vm/java/database.java.vm

@@ -1,4 +1,4 @@
1
-package ${packageName}.entity.database.${folder};;
1
+package ${packageName}.entity.database.${folder};
2 2
 
3 3
 #foreach ($import in $importList)
4 4
 import ${import};
@@ -13,6 +13,47 @@ import com.smartSteward.common.core.BaseEntity;
13 13
 #elseif($table.tree)
14 14
 import com.smartSteward.common.core.TreeEntity;
15 15
 #end
16
+## 根据字段类型动态导入校验注解
17
+#set($hasStringField = false)
18
+#set($hasNotBlank = false)
19
+#set($hasNotNull = false)
20
+#set($hasSize = false)
21
+#set($hasDigits = false)
22
+#set($hasMin = false)
23
+#set($hasMax = false)
24
+#foreach ($column in $columns)
25
+#if(!$table.isSuperColumn($column.javaField) && $column.required)
26
+  #if($column.javaType == 'String')
27
+    #set($hasStringField = true)
28
+    #set($hasNotBlank = true)
29
+    #if($column.columnLength && $column.columnLength > 0)
30
+      #set($hasSize = true)
31
+    #end
32
+  #elseif($column.javaType.contains("Enum") || $column.javaType == 'Date' ||
33
+          $column.javaType == 'Integer' || $column.javaType == 'Long' ||
34
+          $column.javaType == 'BigDecimal' || $column.javaType == 'Double' ||
35
+          $column.javaType == 'Float' || $column.javaType == 'Boolean')
36
+    #set($hasNotNull = true)
37
+  #end
38
+#end
39
+#end
40
+
41
+## 导入校验注解
42
+#if($hasNotBlank || $hasNotNull)
43
+import javax.validation.constraints.*;
44
+#end
45
+#if($hasSize)
46
+import javax.validation.constraints.Size;
47
+#end
48
+#if($hasDigits)
49
+import javax.validation.constraints.Digits;
50
+#end
51
+#if($hasMin)
52
+import javax.validation.constraints.Min;
53
+#end
54
+#if($hasMax)
55
+import javax.validation.constraints.Max;
56
+#end
16 57
 
17 58
 /**
18 59
  * ${functionName}对象 ${tableName}
@@ -33,14 +74,14 @@ public class ${ClassName} extends ${Entity}
33 74
 
34 75
 #foreach ($column in $columns)
35 76
 #if(!$table.isSuperColumn($column.javaField))
36
-    /** $column.columnComment */
37
-#if($column.list)
38
-#set($parentheseIndex=$column.columnComment.indexOf("("))
77
+#set($comment = $column.columnComment)
78
+#set($parentheseIndex = $column.columnComment.indexOf("("))
39 79
 #if($parentheseIndex != -1)
40
-#set($comment=$column.columnComment.substring(0, $parentheseIndex))
41
-#else
42
-#set($comment=$column.columnComment)
80
+#set($comment = $column.columnComment.substring(0, $parentheseIndex))
43 81
 #end
82
+
83
+    /** $column.columnComment */
84
+#if($column.list)
44 85
 #if($parentheseIndex != -1)
45 86
     @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
46 87
 #elseif($column.javaType == 'Date')
@@ -55,6 +96,50 @@ public class ${ClassName} extends ${Entity}
55 96
 #else
56 97
     @ApiModelProperty("${comment}")
57 98
 #end
99
+
100
+## 添加校验注解逻辑
101
+## 如果是自增主键,不需要加@NotNull
102
+#if($column.isPk == '1' && $column.increment)
103
+    ## 自增主键,不添加校验注解
104
+#elseif($column.required)
105
+    ## 必填字段
106
+    #if($column.javaType == 'String')
107
+        #if($column.columnLength && $column.columnLength > 0)
108
+    @NotBlank(message = "${comment}不能为空")
109
+    @Size(max = ${column.columnLength}, message = "${comment}长度不能超过${column.columnLength}个字符")
110
+        #else
111
+    @NotBlank(message = "${comment}不能为空")
112
+        #end
113
+    #elseif($column.javaType.contains("Enum"))
114
+    @NotNull(message = "${comment}不能为空")
115
+    #elseif($column.javaType == 'Integer' || $column.javaType == 'int')
116
+    @NotNull(message = "${comment}不能为空")
117
+        #if($column.columnLength && $column.columnLength > 0)
118
+    @Min(value = 0, message = "${comment}最小值为0")
119
+    @Max(value = ${column.columnLength}, message = "${comment}最大值为${column.columnLength}")
120
+        #end
121
+    #elseif($column.javaType == 'Long' || $column.javaType == 'long')
122
+    @NotNull(message = "${comment}不能为空")
123
+    #elseif($column.javaType == 'BigDecimal')
124
+    @NotNull(message = "${comment}不能为空")
125
+        #if($column.columnPrecision)
126
+            #set($integerPart = $column.columnPrecision - $column.columnScale)
127
+    @Digits(integer = ${integerPart}, fraction = ${column.columnScale}, message = "${comment}整数部分最多${integerPart}位,小数部分最多${column.columnScale}位")
128
+        #end
129
+    #elseif($column.javaType == 'Date')
130
+    @NotNull(message = "${comment}不能为空")
131
+    #elseif($column.javaType == 'Boolean' || $column.javaType == 'boolean')
132
+        ## 布尔类型不需要@NotNull
133
+    #elseif(!$column.javaType.matches("^(byte|short|int|long|float|double|boolean|char|Byte|Short|Integer|Long|Float|Double|Boolean|Character)$"))
134
+        ## 其他引用类型
135
+    @NotNull(message = "${comment}不能为空")
136
+    #end
137
+#else
138
+    ## 非必填字段
139
+    #if($column.javaType == 'String' && $column.columnLength && $column.columnLength > 0)
140
+    @Size(max = ${column.columnLength}, message = "${comment}长度不能超过${column.columnLength}个字符")
141
+    #end
142
+#end
58 143
     private $column.javaType $column.javaField;
59 144
 
60 145
 #end
@@ -62,6 +147,7 @@ public class ${ClassName} extends ${Entity}
62 147
 #if($table.sub)
63 148
     /** $table.subTable.functionName信息 */
64 149
     @ApiModelProperty("$table.subTable.functionName信息")
150
+    @Valid  ## 嵌套校验
65 151
     private List<${subClassName}> ${subclassName}List;
66 152
 
67 153
 #end