Sfoglia il codice sorgente

Merge pull request #705 from yandixuan/3.0

代码生成器增加Beetl模板
qmdx 6 anni fa
parent
commit
69af41fe2d

+ 1 - 0
build.gradle

@@ -60,6 +60,7 @@ ext {
         //code generator
         "velocity"                          : "org.apache.velocity:velocity-engine-core:2.0",
         "freemarker"                        : "org.freemarker:freemarker:2.3.9",
+        "beetl"                             : "com.ibeetl:beetl:2.9.6",
 
         // spring boot
         "springboot-autoconfigure"          : "org.springframework.boot:spring-boot-autoconfigure:${springBootVersion}",

+ 1 - 0
mybatis-plus-generator/build.gradle

@@ -3,6 +3,7 @@ dependencies {
 
     compile rootProject.ext.dependencies["velocity"], optional
     compile rootProject.ext.dependencies["freemarker"], optional
+    compile rootProject.ext.dependencies["beetl"], optional
 
     testCompile rootProject.ext.dependencies["sqlserver"]
     testCompile rootProject.ext.dependencies["postgresql"]

+ 53 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/BeetlTemplateEngine.java

@@ -0,0 +1,53 @@
+package com.baomidou.mybatisplus.generator.engine;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Map;
+
+import org.beetl.core.Configuration;
+import org.beetl.core.GroupTemplate;
+import org.beetl.core.Template;
+import org.beetl.core.resource.ClasspathResourceLoader;
+
+import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
+
+/**
+ * <p>
+ * Beetl 模板引擎实现文件输出
+ * </p>
+ *
+ * @author yandixuan
+ * @since 2018-12-16
+ */
+public class BeetlTemplateEngine extends AbstractTemplateEngine {
+
+    private GroupTemplate groupTemplate;
+
+    @Override
+    public AbstractTemplateEngine init(ConfigBuilder configBuilder) {
+        super.init(configBuilder);
+        try {
+            Configuration cfg = Configuration.defaultConfiguration();
+            groupTemplate = new GroupTemplate(new ClasspathResourceLoader("/"), cfg);
+        } catch (IOException e) {
+            logger.error(e.getMessage(), e);
+        }
+        return this;
+    }
+
+    @Override
+    public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception {
+        Template template = groupTemplate.getTemplate(templatePath);
+        FileOutputStream fileOutputStream = new FileOutputStream(new File(outputFile));
+        template.binding(objectMap);
+        template.renderTo(fileOutputStream);
+        fileOutputStream.close();
+        logger.debug("模板:" + templatePath + ";  文件:" + outputFile);
+    }
+
+    @Override
+    public String templateFilePath(String filePath) {
+        return filePath + ".btl";
+    }
+}

+ 39 - 0
mybatis-plus-generator/src/main/resources/templates/controller.java.btl

@@ -0,0 +1,39 @@
+package ${package.Controller};
+
+
+import org.springframework.web.bind.annotation.RequestMapping;
+
+<% if(restControllerStyle){ %>
+import org.springframework.web.bind.annotation.RestController;
+<% }else{ %>
+import org.springframework.stereotype.Controller;
+<% } %>
+<% if(isNotEmpty(superControllerClassPackage)){ %>
+import ${superControllerClassPackage};
+<% } %>
+
+/**
+ * <p>
+ * ${table.comment!} 前端控制器
+ * </p>
+ *
+ * @author ${author}
+ * @since ${date}
+ */
+<% if(restControllerStyle){ %>
+@RestController
+<% }else{ %>
+@Controller
+<% } %>
+@RequestMapping("<% if(isNotEmpty(package.ModuleName)){ %>/package.ModuleName<% } %><% if(isNotEmpty(controllerMappingHyphenStyle)){ %>${controllerMappingHyphen}<% }else{ %>${table.entityPath}<% } %>")
+<% if(kotlin){ %>
+class ${table.controllerName}<% if(isNotEmpty(superControllerClass)){ %> : ${superControllerClass}()<% } %>
+<% }else{ %>
+    <% if(isNotEmpty(superControllerClass)){ %>
+public class ${table.controllerName} extends ${superControllerClass} {
+    <% }else{ %>
+public class ${table.controllerName} {
+    <% } %>
+
+}
+<% } %>

+ 160 - 0
mybatis-plus-generator/src/main/resources/templates/entity.java.btl

@@ -0,0 +1,160 @@
+package ${package.Entity};
+<% for(pkg in table.importPackages){ %>
+import ${pkg};
+<% } %>
+<% if(swagger2){ %>
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+<% } %>
+<% if(entityLombokModel){ %>
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+<% } %>
+/**
+ * <p>
+ * ${table.comment!}
+ * </p>
+ *
+ * @author ${author}
+ * @since ${date}
+ */
+<% if(entityLombokModel){ %>
+@Data
+    <% if(isNotEmpty(superEntityClass)){ %>
+@EqualsAndHashCode(callSuper = true)
+    <% }else{ %>
+@EqualsAndHashCode(callSuper = false)
+    <% } %>
+@Accessors(chain = true)
+<% } %>
+<% if(table.convert){ %>
+@TableName("${table.name}")
+<% } %>
+<% if(swagger2){ %>
+@ApiModel(value="${entity}对象", description="${table.comment!''}")
+<% } %>
+<% if(isNotEmpty(superEntityClass)){ %>
+public class ${entity} extends ${superEntityClass}<% if(activeRecord){ %><${entity}><%}%>{
+<% }else if(activeRecord){ %>
+public class ${entity} extends Model<${entity}> {
+<% }else{ %>
+public class ${entity} implements Serializable {
+<% } %>
+
+    private static final long serialVersionUID = 1L;
+<% /** -----------BEGIN 字段循环遍历----------- **/ %>
+<% for(field in table.fields){ %>
+    <%
+    if(field.keyFlag){
+        var keyPropertyName = field.propertyName;
+    }
+    %>
+
+    <% if(isNotEmpty(field.comment)){ %>
+        <% if(swagger2){ %>
+    @ApiModelProperty(value = "${field.comment}")
+        <% }else{ %>
+    /**
+     * ${field.comment}
+     */
+        <% } %>
+    <% } %>
+    <% if(field.keyFlag){ %>
+    <%
+    /*主键*/
+    %>
+        <% if(field.keyIdentityFlag){ %>
+    @TableId(value = "${field.name}", type = IdType.AUTO)
+        <% }else if(isNotEmpty(idType)){ %>
+    @TableId(value = "${field.name}", type = IdType.${idType})
+        <% }else if(field.convert){ %>
+    @TableId("${field.name}")
+         <% } %>
+    <%
+    /*普通字段*/
+    %>
+    <% }else if(isNotEmpty(field.fill)){ %>
+        <% if(field.convert){ %>
+    @TableField(value = "${field.name}", fill = FieldFill.${field.fill})
+        <% }else{ %>
+    @TableField(fill = FieldFill.${field.fill})
+        <% } %>
+    <% }else if(field.convert){ %>
+    @TableField("${field.name}")
+    <% } %>
+    <%
+    /*乐观锁注解*/
+    %>
+    <% if(versionFieldName!'' == field.name){ %>
+    @Version
+    <% } %>
+    <%
+    /*逻辑删除注解*/
+    %>
+    <% if(logicDeleteFieldName!'' == field.name){ %>
+    @Version
+    <% } %>
+    private ${field.propertyType} ${field.propertyName};
+<% } %>
+<% /** -----------END 字段循环遍历----------- **/ %>
+
+<% if(!entityLombokModel){ %>
+    <% for(field in table.fields){ %>
+        <%
+        var getprefix ='';
+        if(field.propertyType=='boolean'){
+            getprefix='is';
+        }else{
+            getprefix='get';
+        }
+        %>
+    public ${field.propertyType} ${getprefix}${field.capitalName}() {
+        return ${field.propertyName};
+    }
+
+        <% if(entityBuilderModel){ %>
+    public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
+        <% }else{ %>
+    public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
+        <% } %>
+        this.${field.propertyName} = ${field.propertyName};
+        <% if(entityBuilderModel){ %>
+        return this;
+        <% } %>
+    }
+
+    <% } %>
+<% } %>
+<% if(entityColumnConstant){ %>
+   <% for(field in table.fields){ %>
+    public static final String ${strutil.toUpperCase(field.name)} = "${field.name}";
+
+   <% } %>
+<% } %>
+<% if(activeRecord){ %>
+    @Override
+    protected Serializable pkVal() {
+    <% if(isNotEmpty(keyPropertyName)){ %>
+        return this.${keyPropertyName};
+    <% }else{ %>
+        return null;
+    <% } %>
+    }
+
+<% } %>
+<% if(!entityLombokModel){ %>
+    @Override
+    public String toString() {
+        return "${entity}{" +
+    <% for(field in table.fields){ %>
+       <% if(fieldLP.index==0){ %>
+        "${field.propertyName}=" + ${field.propertyName} +
+       <% }else{ %>
+        ", ${field.propertyName}=" + ${field.propertyName} +
+       <% } %>
+    <% } %>
+        "}";
+    }
+<% } %>
+}

+ 20 - 0
mybatis-plus-generator/src/main/resources/templates/mapper.java.btl

@@ -0,0 +1,20 @@
+package ${package.Mapper};
+
+import ${package.Entity}.${entity};
+import ${superMapperClassPackage};
+
+/**
+ * <p>
+ * ${table.comment!} Mapper 接口
+ * </p>
+ *
+ * @author ${author}
+ * @since ${date}
+ */
+<% if(kotlin){ %>
+interface ${table.mapperName} : ${superMapperClass}<${entity}>
+<% }else{ %>
+public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
+
+}
+<% } %>

+ 41 - 0
mybatis-plus-generator/src/main/resources/templates/mapper.xml.btl

@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="${package.Mapper}.${table.mapperName}">
+
+<% if(enableCache){ %>
+    <!-- 开启二级缓存 -->
+    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
+
+<% } %>
+<% if(baseResultMap){ %>
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
+<% for(field in table.fields){ %>
+   <% /** 生成主键排在第一位 **/ %>
+   <% if(field.keyFlag){ %>
+        <id column="${field.name}" property="${field.propertyName}" />
+   <% } %>
+<% } %>
+<% for(field in table.commonFields){ %>
+    <% /** 生成公共字段 **/ %>
+    <result column="${field.name}" property="${field.propertyName}" />
+<% } %>
+<% for(field in table.fields){ %>
+   <% /** 生成普通字段 **/ %>
+   <% if(!field.keyFlag){ %>
+        <result column="${field.name}" property="${field.propertyName}" />
+   <% } %>
+<% } %>
+    </resultMap>
+<% } %>
+<% if(baseColumnList){ %>
+    <!-- 通用查询结果列 -->
+    <sql id="Base_Column_List">
+<% for(field in table.commonFields){ %>
+        ${field.name},
+<% } %>
+        ${table.fieldNames}
+    </sql>
+
+<% } %>
+</mapper>

+ 20 - 0
mybatis-plus-generator/src/main/resources/templates/service.java.btl

@@ -0,0 +1,20 @@
+package ${package.Service};
+
+import ${package.Entity}.${entity};
+import ${superServiceClassPackage};
+
+/**
+ * <p>
+ * ${table.comment!} 服务类
+ * </p>
+ *
+ * @author ${author}
+ * @since ${date}
+ */
+<% if(kotlin){ %>
+interface ${table.serviceName} : ${superServiceClass}<${entity}>
+<% }else{ %>
+public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
+
+}
+<% } %>

+ 26 - 0
mybatis-plus-generator/src/main/resources/templates/serviceImpl.java.btl

@@ -0,0 +1,26 @@
+package ${package.ServiceImpl};
+
+import ${package.Entity}.${entity};
+import ${package.Mapper}.${table.mapperName};
+import ${package.Service}.${table.serviceName};
+import ${superServiceImplClassPackage};
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ * ${table.comment!} 服务实现类
+ * </p>
+ *
+ * @author ${author}
+ * @since ${date}
+ */
+@Service
+<% if(kotlin){ %>
+open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
+
+}
+<% }else{ %>
+public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
+
+}
+<% } %>