Selaa lähdekoodia

完善 kotlin 代码生成

hubin 7 vuotta sitten
vanhempi
commit
dc2c603c3a

+ 12 - 5
src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -275,12 +275,12 @@ public class AutoGenerator {
         try {
             TableInfo tableInfo = (TableInfo) context.get("table");
             Map<String, String> pathInfo = config.getPathInfo();
-            String entityFile = String.format((pathInfo.get(ConstVal.ENTITY_PATH) + ConstVal.ENTITY_NAME), entityName);
-            String mapperFile = String.format((pathInfo.get(ConstVal.MAPPER_PATH) + File.separator + tableInfo.getMapperName() + ConstVal.JAVA_SUFFIX), entityName);
+            String entityFile = String.format((pathInfo.get(ConstVal.ENTITY_PATH) + File.separator + "%s" + this.suffixJavaOrKt()), entityName);
+            String mapperFile = String.format((pathInfo.get(ConstVal.MAPPER_PATH) + File.separator + tableInfo.getMapperName() + this.suffixJavaOrKt()), entityName);
             String xmlFile = String.format((pathInfo.get(ConstVal.XML_PATH) + File.separator + tableInfo.getXmlName() + ConstVal.XML_SUFFIX), entityName);
-            String serviceFile = String.format((pathInfo.get(ConstVal.SERIVCE_PATH) + File.separator + tableInfo.getServiceName() + ConstVal.JAVA_SUFFIX), entityName);
-            String implFile = String.format((pathInfo.get(ConstVal.SERVICEIMPL_PATH) + File.separator + tableInfo.getServiceImplName() + ConstVal.JAVA_SUFFIX), entityName);
-            String controllerFile = String.format((pathInfo.get(ConstVal.CONTROLLER_PATH) + File.separator + tableInfo.getControllerName() + ConstVal.JAVA_SUFFIX), entityName);
+            String serviceFile = String.format((pathInfo.get(ConstVal.SERIVCE_PATH) + File.separator + tableInfo.getServiceName() + this.suffixJavaOrKt()), entityName);
+            String implFile = String.format((pathInfo.get(ConstVal.SERVICEIMPL_PATH) + File.separator + tableInfo.getServiceImplName() + this.suffixJavaOrKt()), entityName);
+            String controllerFile = String.format((pathInfo.get(ConstVal.CONTROLLER_PATH) + File.separator + tableInfo.getControllerName() + this.suffixJavaOrKt()), entityName);
 
             TemplateConfig template = config.getTemplate();
 
@@ -323,6 +323,13 @@ public class AutoGenerator {
         }
     }
 
+    /**
+     * 文件后缀
+     */
+    protected String suffixJavaOrKt() {
+        return config.getGlobalConfig().isKotlin() ? ConstVal.KT_SUFFIX : ConstVal.JAVA_SUFFIX;
+    }
+
     /**
      * <p>
      * 将模板转化成为文件

+ 2 - 5
src/main/java/com/baomidou/mybatisplus/generator/config/ConstVal.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.generator.config;
 
-import java.io.File;
 import java.nio.charset.Charset;
 
 /**
@@ -47,19 +46,17 @@ public class ConstVal {
     public static final String UNDERLINE = "_";
 
     public static final String JAVA_SUFFIX = ".java";
+    public static final String KT_SUFFIX = ".kt";
     public static final String XML_SUFFIX = ".xml";
 
     public static final String TEMPLATE_ENTITY_JAVA = "/templates/entity.java.vm";
-    public static final String TEMPLATE_ENTITY_KT = "/templates/entity.java.vm";
+    public static final String TEMPLATE_ENTITY_KT = "/templates/entity.kt.vm";
     public static final String TEMPLATE_MAPPER = "/templates/mapper.java.vm";
     public static final String TEMPLATE_XML = "/templates/mapper.xml.vm";
     public static final String TEMPLATE_SERVICE = "/templates/service.java.vm";
     public static final String TEMPLATE_SERVICEIMPL = "/templates/serviceImpl.java.vm";
     public static final String TEMPLATE_CONTROLLER = "/templates/controller.java.vm";
 
-    public static final String ENTITY_NAME = File.separator + "%s" + JAVA_SUFFIX;
-
-    // 配置使用classloader加载资源
     public static final String VM_LOADPATH_KEY = "file.resource.loader.class";
     public static final String VM_LOADPATH_VALUE = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader";
 

+ 4 - 2
src/main/resources/templates/controller.java.vm

@@ -27,7 +27,8 @@ import ${superControllerClassPackage};
 #end
 @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
 #if(${kotlin})
-class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}#end
+class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
+
 #else
 #if(${superControllerClass})
 public class ${table.controllerName} extends ${superControllerClass} {
@@ -36,4 +37,5 @@ public class ${table.controllerName} {
 #end
 
 }
-#end
+
+#end

+ 37 - 38
src/main/resources/templates/entity.kt.vm

@@ -16,50 +16,50 @@ import ${pkg};
 @TableName("${table.name}")
 #end
 #if(${superEntityClass})
-    class ${entity} : ${superEntityClass}#if(${activeRecord})<${entity}>#end()
+class ${entity} : ${superEntityClass}#if(${activeRecord})<${entity}>#end()() {
 #elseif(${activeRecord})
-    class ${entity} : Model<${entity}>()
+class ${entity} : Model<${entity}>() {
 #else
-    class ${entity} : Serializable {
+class ${entity} : Serializable {
 #end
 
 ## ----------  BEGIN 字段循环遍历  ----------
 #foreach($field in ${table.fields})
-    #if(${field.keyFlag})
-        #set($keyPropertyName=${field.propertyName})
-    #end
-    #if("$!field.comment" != "")
-        /**
-         * ${field.comment}
-         */
-    #end
-    #if(${field.keyFlag})
-    ## 主键
-        #if(${field.keyIdentityFlag})
-        @TableId(value = "${field.name}", type = IdType.AUTO)
-        #elseif(${field.convert})
-        @TableId("${field.name}")
-        #end
-    ## 普通字段
-    #elseif(${field.fill})
-    ## -----   存在字段填充设置   -----
-        #if(${field.convert})
-        @TableField(value = "${field.name}", fill = FieldFill.${field.fill})
-        #else
-        @TableField(fill = FieldFill.${field.fill})
-        #end
-    #elseif(${field.convert})
+#if(${field.keyFlag})
+#set($keyPropertyName=${field.propertyName})
+#end
+#if("$!field.comment" != "")
+    /**
+     * ${field.comment}
+     */
+#end
+#if(${field.keyFlag})
+## 主键
+#if(${field.keyIdentityFlag})
+    @TableId(value = "${field.name}", type = IdType.AUTO)
+#elseif(${field.convert})
+    @TableId("${field.name}")
+#end
+## 普通字段
+#elseif(${field.fill})
+## -----   存在字段填充设置   -----
+#if(${field.convert})
+    @TableField(value = "${field.name}", fill = FieldFill.${field.fill})
+#else
+    @TableField(fill = FieldFill.${field.fill})
+#end
+#elseif(${field.convert})
     @TableField("${field.name}")
-    #end
+#end
 ## 乐观锁注解
-    #if(${versionFieldName}==${field.name})
+#if(${versionFieldName}==${field.name})
     @Version
-    #end
+#end
 ## 逻辑删除注解
-    #if(${logicDeleteFieldName}==${field.name})
+#if(${logicDeleteFieldName}==${field.name})
     @TableLogic
-    #end
-private ${field.propertyType} ${field.propertyName};
+#end
+    var ${field.propertyName}: ${field.propertyType}? = null
 #end
 ## ----------  END 字段循环遍历  ----------
 
@@ -67,7 +67,7 @@ private ${field.propertyType} ${field.propertyName};
 #if(${entityColumnConstant})
 #foreach($field in ${table.fields})
 
-    val ${field.name.toUpperCase()} : String = "${field.name}";
+    val ${field.name.toUpperCase()} : String = "${field.name}"
 
 #end
 #end
@@ -81,16 +81,15 @@ private ${field.propertyType} ${field.propertyName};
     }
 
 #end
-    @Override
-    public String toString() {
+    override fun toString(): String {
         return "${entity}{" +
 #foreach($field in ${table.fields})
 #if($!{velocityCount}==1)
-                        "${field.propertyName}=" + ${field.propertyName} +
+                "${field.propertyName}=" + ${field.propertyName} +
 #else
                 ", ${field.propertyName}=" + ${field.propertyName} +
 #end
 #end
-        "}";
+        "}"
     }
 }

+ 1 - 1
src/main/resources/templates/mapper.java.vm

@@ -12,7 +12,7 @@ import ${superMapperClassPackage};
  * @since ${date}
  */
 #if(${kotlin})
-interface ${table.mapperName} extends ${superMapperClass}<${entity}>
+interface ${table.mapperName} : ${superMapperClass}<${entity}>
 #else
 public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
 

+ 1 - 1
src/main/resources/templates/service.java.vm

@@ -12,7 +12,7 @@ import ${superServiceClassPackage};
  * @since ${date}
  */
 #if(${kotlin})
-interface ${table.serviceName} extends ${superServiceClass}<${entity}>
+interface ${table.serviceName} : ${superServiceClass}<${entity}>
 #else
 public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
 

+ 1 - 0
src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java

@@ -67,6 +67,7 @@ public class MysqlGenerator {
                         .setEnableCache(false)// XML 二级缓存
                         .setBaseResultMap(true)// XML ResultMap
                         .setBaseColumnList(true)// XML columList
+                        //.setKotlin(true) 是否生成 kotlin 代码
                         .setAuthor("Yanghu")
                 // 自定义文件命名,注意 %s 会自动填充表实体属性!
                 // .setMapperName("%sDao")

+ 1 - 0
src/test/java/com/baomidou/mybatisplus/test/generator/PostgreSQLGenerator.java

@@ -39,6 +39,7 @@ public class PostgreSQLGenerator {
         gc.setEnableCache(false);// XML 二级缓存
         gc.setBaseResultMap(true);// XML ResultMap
         gc.setBaseColumnList(false);// XML columList
+        //gc.setKotlin(true) 是否生成 kotlin 代码
         gc.setAuthor("hubin");
 
         // 自定义文件命名,注意 %s 会自动填充表实体属性!

+ 1 - 0
src/test/java/com/baomidou/mybatisplus/test/generator/SQLServerGenerator.java

@@ -39,6 +39,7 @@ public class SQLServerGenerator {
         gc.setEnableCache(false);// XML 二级缓存
         gc.setBaseResultMap(true);// XML ResultMap
         gc.setBaseColumnList(false);// XML columList
+        //gc.setKotlin(true) 是否生成 kotlin 代码
         gc.setAuthor("nieqiurong");
 
         // 自定义文件命名,注意 %s 会自动填充表实体属性!