tanghc 3 năm trước cách đây
mục cha
commit
ba82051bf8

+ 10 - 0
changelog.md

@@ -1,5 +1,15 @@
 # changelog
 
+## 1.4.6
+
+- 模板新增时间变量
+
+```
+${context.datetime}:日期时间,年月日时分秒
+${context.date}:日期,年月日
+${context.time}:时间,时分秒
+```
+
 ## 1.4.5
 
 - 可以格式化xml代码(application.properties中设置`gen.format-xml=true`)

+ 12 - 0
front/public/velocity/csharp.json

@@ -61,6 +61,18 @@
         {
           "expression": "${context.pkName}",
           "text": "表主键名"
+        },
+        {
+          "expression": "${context.datetime}",
+          "text": "日期时间,年月日时分秒"
+        },
+        {
+          "expression": "${context.date}",
+          "text": "日期,年月日"
+        },
+        {
+          "expression": "${context.time}",
+          "text": "时间,时分秒"
         }
       ]
     },

+ 12 - 0
front/public/velocity/java.json

@@ -63,6 +63,18 @@
           "text": "Java类名且首字母小写"
         },
         {
+          "expression": "${context.datetime}",
+          "text": "日期时间,年月日时分秒"
+        },
+        {
+          "expression": "${context.date}",
+          "text": "日期,年月日"
+        },
+        {
+          "expression": "${context.time}",
+          "text": "时间,时分秒"
+        },
+        {
           "expression": "${context.pkName}",
           "text": "表主键名"
         },

+ 21 - 0
gen/src/main/java/com/gitee/gen/gen/SQLContext.java

@@ -3,12 +3,21 @@ package com.gitee.gen.gen;
 import com.gitee.gen.util.FieldUtil;
 import org.apache.commons.lang.StringUtils;
 
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
 /**
  * SQL上下文,这里可以取到表,字段信息<br>
  * 最终会把SQL上下文信息放到velocity中
  */
 public class SQLContext {
 
+    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
+
+    private final LocalDateTime localDateTime = LocalDateTime.now();
+
     /**
      * 表结构定义
      */
@@ -36,6 +45,18 @@ public class SQLContext {
         this.javaPkColumn = (JavaColumnDefinition) this.tableDefinition.getPkColumn();
     }
 
+    public String getDatetime() {
+        return localDateTime.format(DATE_TIME_FORMATTER);
+    }
+
+    public String getDate() {
+        return localDateTime.format(DATE_FORMATTER);
+    }
+
+    public String getTime() {
+        return localDateTime.format(TIME_FORMATTER);
+    }
+
     /**
      * 返回Java类名
      *

+ 12 - 0
gen/src/main/resources/public/velocity/csharp.json

@@ -61,6 +61,18 @@
         {
           "expression": "${context.pkName}",
           "text": "表主键名"
+        },
+        {
+          "expression": "${context.datetime}",
+          "text": "日期时间,年月日时分秒"
+        },
+        {
+          "expression": "${context.date}",
+          "text": "日期,年月日"
+        },
+        {
+          "expression": "${context.time}",
+          "text": "时间,时分秒"
         }
       ]
     },

+ 12 - 0
gen/src/main/resources/public/velocity/java.json

@@ -63,6 +63,18 @@
           "text": "Java类名且首字母小写"
         },
         {
+          "expression": "${context.datetime}",
+          "text": "日期时间,年月日时分秒"
+        },
+        {
+          "expression": "${context.date}",
+          "text": "日期,年月日"
+        },
+        {
+          "expression": "${context.time}",
+          "text": "时间,时分秒"
+        },
+        {
           "expression": "${context.pkName}",
           "text": "表主键名"
         },