Search in sources :

Example 1 with ColumnEntity

use of com.jun.plugin.system.entity.ColumnEntity in project jun_springboot_api_service by wujun728.

the class GenUtils method generatorCode.

/**
 * 生成代码
 */
public static void generatorCode(Map<String, String> table, List<Map<String, String>> columns, ZipOutputStream zip) {
    // 配置信息
    Configuration config = getConfig();
    boolean hasBigDecimal = false;
    // 表信息
    TableEntity tableEntity = new TableEntity();
    tableEntity.setTableName(table.get("tableName"));
    tableEntity.setComments(table.get("tableComment"));
    // 表名转换成Java类名
    String className = tableToJava(tableEntity.getTableName(), config.getStringArray("tablePrefix"));
    tableEntity.setClassName(className);
    tableEntity.setClassname(StringUtils.uncapitalize(className));
    tableEntity.setClassNameLower(className.toLowerCase());
    // 列信息
    List<ColumnEntity> columsList = new ArrayList<>();
    for (Map<String, String> column : columns) {
        ColumnEntity columnEntity = new ColumnEntity();
        columnEntity.setColumnName(column.get("columnName"));
        columnEntity.setDataType(column.get("dataType"));
        columnEntity.setComments(column.get("columnComment"));
        columnEntity.setExtra(column.get("extra"));
        // 列名转换成Java属性名
        String attrName = columnToJava(columnEntity.getColumnName());
        columnEntity.setAttrName(attrName);
        columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
        // 列的数据类型,转换成Java类型
        String attrType = config.getString(columnEntity.getDataType(), "unknowType");
        columnEntity.setAttrType(attrType);
        if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
            hasBigDecimal = true;
        }
        // 是否主键
        if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
            tableEntity.setPk(columnEntity);
        }
        columsList.add(columnEntity);
    }
    tableEntity.setColumns(columsList);
    // 没主键,则第一个字段为主键
    if (tableEntity.getPk() == null) {
        tableEntity.setPk(tableEntity.getColumns().get(0));
    }
    // 设置velocity资源加载器
    Properties prop = new Properties();
    prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Velocity.init(prop);
    String mainPath = config.getString("mainPath");
    mainPath = StringUtils.isBlank(mainPath) ? "com.company" : mainPath;
    // 封装模板数据
    Map<String, Object> map = new HashMap<>(15);
    map.put("tableName", tableEntity.getTableName());
    map.put("comments", tableEntity.getComments());
    map.put("pk", tableEntity.getPk());
    map.put("className", tableEntity.getClassName());
    map.put("classname", tableEntity.getClassname());
    map.put("pathName", tableEntity.getClassname().toLowerCase());
    map.put("columns", tableEntity.getColumns());
    map.put("classNameLower", tableEntity.getClassNameLower());
    map.put("hasBigDecimal", hasBigDecimal);
    map.put("mainPath", mainPath);
    map.put("package", config.getString("package"));
    map.put("author", config.getString("author"));
    map.put("email", config.getString("email"));
    map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
    map.put("identity", IdWorker.getId());
    map.put("addId", IdWorker.getId());
    map.put("updateId", IdWorker.getId());
    map.put("deleteId", IdWorker.getId());
    map.put("selectId", IdWorker.getId());
    VelocityContext context = new VelocityContext(map);
    // 获取模板列表
    List<String> templates = getTemplates();
    for (String template : templates) {
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8");
        tpl.merge(context, sw);
        try {
            // 添加到zip
            zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getClassName(), config.getString("package")))));
            IOUtils.write(sw.toString(), zip, "UTF-8");
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            throw new BusinessException("渲染模板失败,表名:" + tableEntity.getTableName());
        }
    }
}
Also used : ColumnEntity(com.jun.plugin.system.entity.ColumnEntity) Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) VelocityContext(org.apache.velocity.VelocityContext) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Template(org.apache.velocity.Template) BusinessException(com.jun.plugin.system.common.exception.BusinessException) StringWriter(java.io.StringWriter) TableEntity(com.jun.plugin.system.entity.TableEntity)

Aggregations

BusinessException (com.jun.plugin.system.common.exception.BusinessException)1 ColumnEntity (com.jun.plugin.system.entity.ColumnEntity)1 TableEntity (com.jun.plugin.system.entity.TableEntity)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ZipEntry (java.util.zip.ZipEntry)1 Configuration (org.apache.commons.configuration.Configuration)1 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1