فهرست منبع

code gen add kotlin support

= 7 سال پیش
والد
کامیت
c7408d5719

+ 2 - 1
src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -205,6 +205,7 @@ public class AutoGenerator {
             ctx.put("logicDeleteFieldName", config.getStrategyConfig().getLogicDeleteFieldName());
             ctx.put("versionFieldName", config.getStrategyConfig().getVersionFieldName());
             ctx.put("activeRecord", config.getGlobalConfig().isActiveRecord());
+            ctx.put("kotlin", config.getGlobalConfig().isKotlin());
             ctx.put("date", date);
             ctx.put("table", tableInfo);
             ctx.put("enableCache", config.getGlobalConfig().isEnableCache());
@@ -285,7 +286,7 @@ public class AutoGenerator {
 
             // 根据override标识来判断是否需要创建文件
             if (isCreate(entityFile)) {
-                vmToFile(context, template.getEntity(), entityFile);
+                vmToFile(context, template.getEntity(config.getGlobalConfig().isKotlin()), entityFile);
             }
             if (isCreate(mapperFile)) {
                 vmToFile(context, template.getMapper(), mapperFile);

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

@@ -49,7 +49,8 @@ public class ConstVal {
     public static final String JAVA_SUFFIX = ".java";
     public static final String XML_SUFFIX = ".xml";
 
-    public static final String TEMPLATE_ENTITY = "/templates/entity.java.vm";
+    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_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";

+ 13 - 0
src/main/java/com/baomidou/mybatisplus/generator/config/GlobalConfig.java

@@ -50,6 +50,11 @@ public class GlobalConfig {
      */
     private String author;
 
+    /**
+     * 开启 Kotlin 模式
+     */
+    private boolean kotlin = false;
+
     /**
      * 开启 ActiveRecord 模式
      */
@@ -118,6 +123,14 @@ public class GlobalConfig {
         return this;
     }
 
+    public boolean isKotlin() {
+        return kotlin;
+    }
+
+    public void setKotlin(boolean kotlin) {
+        this.kotlin = kotlin;
+    }
+
     public boolean isActiveRecord() {
         return activeRecord;
     }

+ 3 - 3
src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java

@@ -25,7 +25,7 @@ package com.baomidou.mybatisplus.generator.config;
  */
 public class TemplateConfig {
 
-    private String entity = ConstVal.TEMPLATE_ENTITY;
+    private String entity = ConstVal.TEMPLATE_ENTITY_JAVA;
 
     private String service = ConstVal.TEMPLATE_SERVICE;
 
@@ -37,8 +37,8 @@ public class TemplateConfig {
 
     private String controller = ConstVal.TEMPLATE_CONTROLLER;
 
-    public String getEntity() {
-        return entity;
+    public String getEntity(boolean kotlin) {
+        return kotlin ? ConstVal.TEMPLATE_ENTITY_KT : entity;
     }
 
     public TemplateConfig setEntity(String entity) {

+ 1 - 1
src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -235,7 +235,7 @@ public class ConfigBuilder {
 
         // 生成路径信息
         pathInfo = new HashMap<>();
-        if (StringUtils.isNotEmpty(template.getEntity())) {
+        if (StringUtils.isNotEmpty(template.getEntity(getGlobalConfig().isKotlin()))) {
             pathInfo.put(ConstVal.ENTITY_PATH, joinPath(outputDir, packageInfo.get(ConstVal.ENTITY)));
         }
         if (StringUtils.isNotEmpty(template.getMapper())) {

+ 5 - 1
src/main/resources/templates/controller.java.vm

@@ -26,10 +26,14 @@ import ${superControllerClassPackage};
 @Controller
 #end
 @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
+#if(${kotlin})
+class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}#end
+#else
 #if(${superControllerClass})
 public class ${table.controllerName} extends ${superControllerClass} {
 #else
 public class ${table.controllerName} {
 #end
-	
+
 }
+#end

+ 96 - 0
src/main/resources/templates/entity.kt.vm

@@ -0,0 +1,96 @@
+package ${package.Entity};
+
+#foreach($pkg in ${table.importPackages})
+import ${pkg};
+#end
+
+/**
+ * <p>
+ * $!{table.comment}
+ * </p>
+ *
+ * @author ${author}
+ * @since ${date}
+ */
+#if(${table.convert})
+@TableName("${table.name}")
+#end
+#if(${superEntityClass})
+    class ${entity} : ${superEntityClass}#if(${activeRecord})<${entity}>#end()
+#elseif(${activeRecord})
+    class ${entity} : Model<${entity}>()
+#else
+    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})
+    @TableField("${field.name}")
+    #end
+## 乐观锁注解
+    #if(${versionFieldName}==${field.name})
+    @Version
+    #end
+## 逻辑删除注解
+    #if(${logicDeleteFieldName}==${field.name})
+    @TableLogic
+    #end
+private ${field.propertyType} ${field.propertyName};
+#end
+## ----------  END 字段循环遍历  ----------
+
+
+#if(${entityColumnConstant})
+#foreach($field in ${table.fields})
+
+    val ${field.name.toUpperCase()} : String = "${field.name}";
+
+#end
+#end
+#if(${activeRecord})
+    override fun pkVal(): Serializable {
+#if(${keyPropertyName})
+        return ${keyPropertyName}!!
+#else
+        return id!!
+#end
+    }
+
+#end
+    @Override
+    public String toString() {
+        return "${entity}{" +
+#foreach($field in ${table.fields})
+#if($!{velocityCount}==1)
+                        "${field.propertyName}=" + ${field.propertyName} +
+#else
+                ", ${field.propertyName}=" + ${field.propertyName} +
+#end
+#end
+        "}";
+    }
+}

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

@@ -11,6 +11,10 @@ import ${superMapperClassPackage};
  * @author ${author}
  * @since ${date}
  */
+#if(${kotlin})
+interface ${table.mapperName} extends ${superMapperClass}<${entity}>
+#else
 public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
 
-}
+}
+#end

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

@@ -11,6 +11,10 @@ import ${superServiceClassPackage};
  * @author ${author}
  * @since ${date}
  */
+#if(${kotlin})
+interface ${table.serviceName} extends ${superServiceClass}<${entity}>
+#else
 public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
-	
+
 }
+#end

+ 7 - 1
src/main/resources/templates/serviceImpl.java.vm

@@ -14,7 +14,13 @@ import org.springframework.stereotype.Service;
  * @author ${author}
  * @since ${date}
  */
+#if(${kotlin})
 @Service
+open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
+
+}
+#else
 public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
-	
+
 }
+#end