Browse Source

新增mariadbQuery(从mysqlQuery cv 过去的),新增DateType枚举(用于定义生成实体的时间类型)

miemie 7 năm trước cách đây
mục cha
commit
e2109066cb

+ 3 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/GlobalConfig.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.generator.config;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.generator.config.rules.DateType;
 import lombok.Data;
 import lombok.experimental.Accessors;
 
@@ -72,9 +73,9 @@ public class GlobalConfig {
     private boolean baseResultMap = false;
 
     /**
-     * 是否使用 java8 的时间类型
+     * 时间类型对应策略
      */
-    private boolean useJava8Time = true;
+    private DateType dateType = DateType.TIME_PACK;
 
     /**
      * 开启 baseColumnList

+ 25 - 12
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/converts/MySqlTypeConvert.java

@@ -57,20 +57,33 @@ public class MySqlTypeConvert implements ITypeConvert {
         } else if (t.contains("json") || t.contains("enum")) {
             return DbColumnType.STRING;
         } else if (t.contains("date") || t.contains("time") || t.contains("year")) {
-            if (globalConfig.isUseJava8Time()) {
-                switch (t) {
-                    case "date":
-                        return DbColumnType.LOCAL_DATE;
-                    case "time":
-                        return DbColumnType.LOCAL_TIME;
-                    default:
-                        return DbColumnType.LOCAL_DATE_TIME;
-                }
-            } else {
-                return DbColumnType.DATE;
+            switch (globalConfig.getDateType()) {
+                case ONLY_DATE:
+                    return DbColumnType.DATE;
+                case SQL_PACK:
+                    switch (t) {
+                        case "date":
+                            return DbColumnType.DATE_SQL;
+                        case "time":
+                            return DbColumnType.TIME;
+                        case "year"://todo 这个year可能有bug
+                            return DbColumnType.DATE_SQL;
+                        default:
+                            return DbColumnType.TIMESTAMP;
+                    }
+                case TIME_PACK:
+                    switch (t) {
+                        case "date":
+                            return DbColumnType.LOCAL_DATE;
+                        case "time":
+                            return DbColumnType.LOCAL_TIME;
+                        case "year":
+                            return DbColumnType.YEAR;
+                        default:
+                            return DbColumnType.LOCAL_DATE_TIME;
+                    }
             }
         }
         return DbColumnType.STRING;
     }
-
 }

+ 92 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/MariadbQuery.java

@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.querys;
+
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * <p>
+ * MySql 表数据查询
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public class MariadbQuery extends AbstractDbQuery {
+
+
+    @Override
+    public DbType dbType() {
+        return DbType.MARIADB;
+    }
+
+
+    @Override
+    public String tablesSql() {
+        return "show table status";
+    }
+
+
+    @Override
+    public String tableFieldsSql() {
+        return "show full fields from `%s`";
+    }
+
+
+    @Override
+    public String tableName() {
+        return "NAME";
+    }
+
+
+    @Override
+    public String tableComment() {
+        return "COMMENT";
+    }
+
+
+    @Override
+    public String fieldName() {
+        return "FIELD";
+    }
+
+
+    @Override
+    public String fieldType() {
+        return "TYPE";
+    }
+
+
+    @Override
+    public String fieldComment() {
+        return "COMMENT";
+    }
+
+
+    @Override
+    public String fieldKey() {
+        return "KEY";
+    }
+
+
+    @Override
+    public boolean isKeyIdentity(ResultSet results) throws SQLException {
+        return "auto_increment".equals(results.getString("Extra"));
+    }
+}

+ 25 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/DateType.java

@@ -0,0 +1,25 @@
+package com.baomidou.mybatisplus.generator.config.rules;
+
+/**
+ * <p>
+ * 数据库时间类型 到 实体类时间类型 对应策略
+ * </p>
+ *
+ * @author miemie
+ * @since 2018/5/22
+ */
+public enum DateType {
+    /**
+     * 只使用 java.util.date 代替
+     */
+    ONLY_DATE,
+    /**
+     * 使用 java.sql 包下的
+     */
+    SQL_PACK,
+    /**
+     * 使用 java.time 包下的
+     * java8 新的时间类型
+     */
+    TIME_PACK
+}

+ 4 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/DbColumnType.java

@@ -46,14 +46,18 @@ public enum DbColumnType {
     CHARACTER("Character", null),
     OBJECT("Object", null),
     DATE("Date", "java.util.Date"),
+    DATE_SQL("Date", "java.sql.Date"),
     TIME("Time", "java.sql.Time"),
     BLOB("Blob", "java.sql.Blob"),
     CLOB("Clob", "java.sql.Clob"),
     TIMESTAMP("Timestamp", "java.sql.Timestamp"),
     BIG_INTEGER("BigInteger", "java.math.BigInteger"),
     BIG_DECIMAL("BigDecimal", "java.math.BigDecimal"),
+
+    // java8 新时间类型
     LOCAL_DATE("LocalDate", "java.time.LocalDate"),
     LOCAL_TIME("LocalTime", "java.time.LocalTime"),
+    YEAR("Year", "java.time.Year"),
     LOCAL_DATE_TIME("LocalDateTime", "java.time.LocalDateTime");
 
     /**