Przeglądaj źródła

fixed 代码生成功能优化

fushengqian 1 rok temu
rodzic
commit
43d2bdfd3c

+ 4 - 1
fuint-application/src/main/java/com/fuint/common/service/impl/GenCodeServiceImpl.java

@@ -11,6 +11,7 @@ import com.fuint.framework.exception.BusinessCheckException;
 import com.fuint.framework.exception.BusinessRuntimeException;
 import com.fuint.framework.pagination.PaginationRequest;
 import com.fuint.framework.pagination.PaginationResponse;
+import com.fuint.repository.bean.ColumnBean;
 import com.fuint.repository.mapper.TGenCodeMapper;
 import com.fuint.repository.model.TGenCode;
 import com.github.pagehelper.Page;
@@ -136,8 +137,10 @@ public class GenCodeServiceImpl implements GenCodeService {
             throw new BusinessRuntimeException("渲染模板失败,该表不存在.");
         }
 
+        List<ColumnBean> columns = tGenCodeMapper.getTableColumnList(table.getTablePrefix() + table.getTableName());
+
         VelocityInitializer.initVelocity();
-        VelocityContext context = VelocityUtils.prepareContext(table);
+        VelocityContext context = VelocityUtils.prepareContext(table, columns);
 
         // 获取模板列表
         List<String> templates = VelocityUtils.getTemplateList();

+ 27 - 0
fuint-application/src/main/java/com/fuint/common/util/CommonUtil.java

@@ -35,6 +35,33 @@ public class CommonUtil {
         return new String(ch);
     }
 
+    /**
+     * 功能:将输入字符串的首字母改成驼峰格式
+     *
+     * @param str
+     * @return
+     */
+    public static String toCamelCase(String str) {
+        if (str == null) {
+            return null;
+        }
+        str = str.toLowerCase();
+        StringBuilder sb = new StringBuilder();
+        boolean upperCase = false;
+        for (int i = 0; i < str.length(); i++) {
+            char c = str.charAt(i);
+            if (c == '_') {
+                upperCase = true;
+            } else if (upperCase) {
+                sb.append(Character.toUpperCase(c));
+                upperCase = false;
+            } else {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
     /**
      * 判断是否UTF-8编码
      *

+ 18 - 90
fuint-application/src/main/java/com/fuint/common/util/VelocityUtils.java

@@ -1,14 +1,12 @@
 package com.fuint.common.util;
 
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
+import com.fuint.repository.bean.ColumnBean;
 import org.apache.commons.lang3.StringUtils;
 import com.fuint.repository.model.TGenCode;
 import com.fuint.utils.StringUtil;
 import org.apache.velocity.VelocityContext;
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONObject;
 
 /**
  * 模板处理工具类
@@ -24,16 +22,14 @@ public class VelocityUtils {
     /** mybatis空间路径 */
     private static final String MYBATIS_PATH = "main/resources/mapper";
 
-    /** 默认上级菜单,系统工具 */
-    private static final String DEFAULT_PARENT_MENU_ID = "3";
-
     /**
      * 设置模板变量信息
      *
      * @return 模板列表
      */
-    public static VelocityContext prepareContext(TGenCode genTable) {
+    public static VelocityContext prepareContext(TGenCode genTable, List<ColumnBean> columns) {
         VelocityContext velocityContext = new VelocityContext();
+        velocityContext.put("tablePrefix", genTable.getTablePrefix());
         velocityContext.put("tableName", genTable.getTableName());
         velocityContext.put("moduleName", genTable.getModuleName());
         String modelName = CommonUtil.firstLetterToUpperCase(genTable.getTablePrefix()).replaceAll("_", "") + CommonUtil.firstLetterToUpperCase(genTable.getTableName());
@@ -41,20 +37,24 @@ public class VelocityUtils {
         velocityContext.put("basePackage", getPackagePrefix(genTable.getPackageName()));
         velocityContext.put("packageName", genTable.getPackageName());
         velocityContext.put("pkColumn", genTable.getPkName());
-        velocityContext.put("importList", getImportList(genTable));
+        velocityContext.put("author", genTable.getAuthor());
         velocityContext.put("table", genTable);
-        velocityContext.put("columns", new ArrayList<>());
-        setMenuVelocityContext(velocityContext, genTable);
+        if (columns != null && columns.size() > 0) {
+            for (ColumnBean columnBean : columns) {
+                 columnBean.setField(CommonUtil.toCamelCase(columnBean.getField()));
+                 if (columnBean.getType().equals("char") || columnBean.getType().equals("varchar") || columnBean.getType().equals("text")) {
+                     columnBean.setType("String");
+                 } else if(columnBean.getType().equals("int") || columnBean.getType().equals("tinyint")) {
+                     columnBean.setType("Integer");
+                 } else if(columnBean.getType().equals("datetime")) {
+                     columnBean.setType("Date");
+                }
+            }
+        }
+        velocityContext.put("columns", columns);
         return velocityContext;
     }
 
-    public static void setMenuVelocityContext(VelocityContext context, TGenCode genTable) {
-        String options = (genTable.getTableName() == null) ? genTable.getTableName() : null;
-        JSONObject paramsObj = JSON.parseObject(options);
-        String parentMenuId = getParentMenuId(paramsObj);
-        context.put("parentMenuId", parentMenuId);
-    }
-
     /**
      * 获取模板信息
      *
@@ -117,6 +117,7 @@ public class VelocityUtils {
         } else if (template.contains("index-tree.vue.vm")) {
             fileName = StringUtil.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
         }
+
         return fileName;
     }
 
@@ -130,77 +131,4 @@ public class VelocityUtils {
         int lastIndex = packageName.lastIndexOf(".");
         return StringUtils.substring(packageName, 0, lastIndex);
     }
-
-    /**
-     * 根据列类型获取导入包
-     * 
-     * @param genTable 业务表对象
-     * @return 返回需要导入的包列表
-     */
-    public static HashSet<String> getImportList(TGenCode genTable) {
-        HashSet<String> importList = new HashSet<>();
-        return importList;
-    }
-
-    /**
-     * 获取权限前缀
-     *
-     * @param moduleName 模块名称
-     * @param businessName 业务名称
-     * @return 返回权限前缀
-     */
-    public static String getPermissionPrefix(String moduleName, String businessName) {
-        return StringUtil.format("{}:{}", moduleName, businessName);
-    }
-
-    /**
-     * 获取上级菜单ID字段
-     *
-     * @param paramsObj 生成其他选项
-     * @return 上级菜单ID字段
-     */
-    public static String getParentMenuId(JSONObject paramsObj) {
-        return DEFAULT_PARENT_MENU_ID;
-    }
-
-    /**
-     * 获取树编码
-     *
-     * @param paramsObj 生成其他选项
-     * @return 树编码
-     */
-    public static String getTreecode(JSONObject paramsObj) {
-        return StringUtils.EMPTY;
-    }
-
-    /**
-     * 获取树父编码
-     *
-     * @param paramsObj 生成其他选项
-     * @return 树父编码
-     */
-    public static String getTreeParentCode(JSONObject paramsObj) {
-        return StringUtils.EMPTY;
-    }
-
-    /**
-     * 获取树名称
-     *
-     * @param paramsObj 生成其他选项
-     * @return 树名称
-     */
-    public static String getTreeName(JSONObject paramsObj) {
-        return StringUtils.EMPTY;
-    }
-
-    /**
-     * 获取需要在哪一列上面显示展开按钮
-     *
-     * @param genTable 业务表对象
-     * @return 展开按钮列序号
-     */
-    public static int getExpandColumn(TGenCode genTable) {
-        int num = 0;
-        return num;
-    }
 }

+ 1 - 4
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendGenCodeController.java

@@ -104,8 +104,6 @@ public class BackendGenCodeController extends BaseController {
         if (tGenCode == null) {
             return getFailureResult(201);
         }
-
-        tGenCode.setOperator(accountInfo.getAccountName());
         tGenCode.setId(id);
         tGenCode.setStatus(status);
         genCodeService.updateGenCode(tGenCode);
@@ -134,7 +132,6 @@ public class BackendGenCodeController extends BaseController {
         }
 
         TGenCode tGenCode = new TGenCode();
-        tGenCode.setOperator(accountInfo.getAccountName());
         tGenCode.setStatus(status);
         if (StringUtil.isNotEmpty(id)) {
             tGenCode.setId(Integer.parseInt(id));
@@ -187,7 +184,7 @@ public class BackendGenCodeController extends BaseController {
             // empty
         }
 
-        genCodeService.generatorCode("luck");
+        genCodeService.generatorCode("banner");
         return getSuccessResult(true);
     }
 }

+ 23 - 77
fuint-application/src/main/resources/vm/java/model.java.vm

@@ -1,34 +1,33 @@
-package ${packageName}.domain;
-
-#foreach ($import in $importList)
-import ${import};
-#end
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-import com.fuint.common.annotation.Excel;
-#if($table.crud || $table.sub)
-import com.fuint.common.core.domain.BaseEntity;
-#elseif($table.tree)
-import com.fuint.common.core.domain.TreeEntity;
-#end
+package com.fuint.repository.model;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.util.Date;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
 
 /**
- * ${functionName}对象 ${tableName}
+ * ${moduleName}实体
  * 
- * @author ${author}
- * @date ${datetime}
+ * @Created by ${author}
+ * CopyRight https://www.fuint.cn
  */
-#if($table.crud || $table.sub)
-#set($Entity="BaseEntity")
-#elseif($table.tree)
-#set($Entity="TreeEntity")
-#end
+@Getter
+@Setter
+@TableName("${tablePrefix}${tableName}")
+@ApiModel(value = "${tableName}表对象", description = "${tableName}表对象")
 public class ${modelName} implements Serializable {
     private static final long serialVersionUID = 1L;
 
 #foreach ($column in $columns)
-#if(!$table.isSuperColumn($column.javaField))
-    /** $column.columnComment */
+    @ApiModelProperty("$column.comment")
+    #if($column.fild == 'id')
+    @TableId(value = "ID", type = IdType.AUTO)
+    #end
 #if($column.list)
 #set($parentheseIndex=$column.columnComment.indexOf("("))
 #if($parentheseIndex != -1)
@@ -45,60 +44,7 @@ public class ${modelName} implements Serializable {
     @Excel(name = "${comment}")
 #end
 #end
-    private $column.javaType $column.javaField;
-
-#end
-#end
-#if($table.sub)
-    /** $table.subTable.functionName信息 */
-    private List<${subClassName}> ${subclassName}List;
-
-#end
-#foreach ($column in $columns)
-#if(!$table.isSuperColumn($column.javaField))
-#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))
-#set($AttrName=$column.javaField)
-#else
-#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
-#end
-    public void set${AttrName}($column.javaType $column.javaField) 
-    {
-        this.$column.javaField = $column.javaField;
-    }
-
-    public $column.javaType get${AttrName}() 
-    {
-        return $column.javaField;
-    }
-#end
-#end
-
-#if($table.sub)
-    public List<${subClassName}> get${subClassName}List()
-    {
-        return ${subclassName}List;
-    }
-
-    public void set${subClassName}List(List<${subClassName}> ${subclassName}List)
-    {
-        this.${subclassName}List = ${subclassName}List;
-    }
+    private $column.type $column.field;
 
 #end
-    @Override
-    public String toString() {
-        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
-#foreach ($column in $columns)
-#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))
-#set($AttrName=$column.javaField)
-#else
-#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
-#end
-            .append("${column.javaField}", get${AttrName}())
-#end
-#if($table.sub)
-            .append("${subclassName}List", get${subClassName}List())
-#end
-            .toString();
-    }
 }

+ 30 - 0
fuint-repository/src/main/java/com/fuint/repository/bean/ColumnBean.java

@@ -0,0 +1,30 @@
+package com.fuint.repository.bean;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+import java.io.Serializable;
+
+/**
+ * 表结构字段实体
+ *
+ * Created by FSQ
+ * CopyRight https://www.fuint.cn
+ */
+@Getter
+@Setter
+public class ColumnBean implements Serializable {
+
+    @ApiModelProperty("字段名称")
+    private String field;
+
+    @ApiModelProperty("类型")
+    private String type;
+
+    @ApiModelProperty("是否为空")
+    private String isNull;
+
+    @ApiModelProperty("备注信息")
+    private String comment;
+
+}

+ 4 - 0
fuint-repository/src/main/java/com/fuint/repository/mapper/TGenCodeMapper.java

@@ -1,8 +1,10 @@
 package com.fuint.repository.mapper;
 
+import com.fuint.repository.bean.ColumnBean;
 import com.fuint.repository.model.TGenCode;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
+import java.util.List;
 
 /**
  * 代码生成 Mapper 接口
@@ -14,4 +16,6 @@ public interface TGenCodeMapper extends BaseMapper<TGenCode> {
 
     TGenCode findGenCodeByTableName(@Param("tableName") String tableName);
 
+    List<ColumnBean> getTableColumnList(@Param("tableName") String tableName);
+
 }

+ 2 - 2
fuint-repository/src/main/java/com/fuint/repository/model/TGenCode.java

@@ -58,8 +58,8 @@ public class TGenCode implements Serializable {
     @ApiModelProperty("更新时间")
     private Date updateTime;
 
-    @ApiModelProperty("最后操作人")
-    private String operator;
+    @ApiModelProperty("作者")
+    private String author;
 
     @ApiModelProperty("状态 0 无效 1 有效")
     private String status;

+ 5 - 0
fuint-repository/src/main/resources/mapper/TGenCodeMapper.xml

@@ -4,4 +4,9 @@
     <select id="findGenCodeByTableName" resultType="com.fuint.repository.model.TGenCode">
         select * from t_gen_code t where t.table_name = #{tableName}
     </select>
+
+    <select id="getTableColumnList" resultType="com.fuint.repository.bean.ColumnBean">
+        SELECT COLUMN_NAME AS FIELD,DATA_TYPE AS TYPE,IS_NULLABLE AS IS_NULL,COLUMN_COMMENT AS COMMENT FROM information_schema.columns
+        WHERE TABLE_SCHEMA = (SELECT DATABASE()) AND TABLE_NAME = #{tableName}
+    </select>
 </mapper>