com.ruoyi.generator.util.VelocityInitializer Java Examples

The following examples show how to use com.ruoyi.generator.util.VelocityInitializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenTableServiceImpl.java    From supplierShop with MIT License 6 votes vote down vote up
/**
 * 预览代码
 * 
 * @param tableId 表编号
 * @return 预览数据列表
 */
public Map<String, String> previewCode(Long tableId)
{
    Map<String, String> dataMap = new LinkedHashMap<>();
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableById(tableId);
    // 查询列信息
    List<GenTableColumn> columns = table.getColumns();
    setPkColumn(table, columns);
    VelocityInitializer.initVelocity();

    VelocityContext context = VelocityUtils.prepareContext(table);

    // 获取模板列表
    List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
    for (String template : templates)
    {
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, Constants.UTF8);
        tpl.merge(context, sw);
        dataMap.put(template, sw.toString());
    }
    return dataMap;
}
 
Example #2
Source File: GenTableServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 查询表信息并生成代码
 */
private void generatorCode(String tableName, ZipOutputStream zip)
{
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    // 查询列信息
    List<GenTableColumn> columns = table.getColumns();
    setPkColumn(table, columns);

    VelocityInitializer.initVelocity();

    VelocityContext context = VelocityUtils.prepareContext(table);

    // 获取模板列表
    List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
    for (String template : templates)
    {
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, Constants.UTF8);
        tpl.merge(context, sw);
        try
        {
            // 添加到zip
            zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
            IOUtils.write(sw.toString(), zip, Constants.UTF8);
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        }
        catch (IOException e)
        {
            log.error("渲染模板失败,表名:" + table.getTableName(), e);
        }
    }
}
 
Example #3
Source File: GenServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 生成代码
 */
private void generatorCode(TableInfo table, ZipOutputStream zip) {
    // 设置主键
    table.setPrimaryKey(table.getColumnsLast());
    VelocityInitializer.initVelocity();
    String packageName = Global.getPackageName();
    String moduleName = GenUtils.getModuleName(packageName);

    VelocityContext context = GenUtils.getVelocityContext(table);

    // 获取模板列表
    List<String> templates = GenUtils.getTemplates();
    for (String template : templates) {
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, Constants.UTF8);
        tpl.merge(context, sw);
        try {
            // 添加到zip
            zip.putNextEntry(new ZipEntry(GenUtils.getFileName(template, table, moduleName)));
            IOUtils.write(sw.toString(), zip, Constants.UTF8);
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            log.error("渲染模板失败,表名:" + table.getTableName(), e);
        }
    }
}
 
Example #4
Source File: GenServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
private Map generatorAdvCodeOnlyWeb(
        String tmplName, TableInfo table, List<ColumnInfo> columns, Map<String, String[]> params) {
    Map retMap = new HashMap();
    // 设置主键
    table.setPrimaryKey(table.getColumnsLast());
    // 设置表格的访问地址
    table.setUri(Convert.toStr(GenUtils.getParam(params, "uri"), "system/unknown"));
    VelocityInitializer.initVelocity();

    String packageName = GenUtils.getParam(params, "package");
    String moduleName = GenUtils.getParam(params, "module");
    table.setModule(moduleName);

    String genJava = Convert.toStr(GenUtils.getParam(params, "gen-java"), "0");
    String genHtml = Convert.toStr(GenUtils.getParam(params, "gen-html"), "0");
    TableInfo.GenStyle genStyle = TableInfo.GenStyle.All;
    if (genJava.equals("1") && genHtml.equals("0")) {
        genStyle = TableInfo.GenStyle.Java;
    } else if (genHtml.equals("1") && genJava.equals("0")) {
        genStyle = TableInfo.GenStyle.Web;
    }
    VelocityContext context = GenUtils.getVelocityContext(table, packageName);

    // 获取模板列表
    List<String> templates = GenUtils.getTemplatesWeb(genStyle, tmplName);
    return putIntoMap(retMap, context, templates);
}
 
Example #5
Source File: GenServiceImpl.java    From ruoyiplus with MIT License 5 votes vote down vote up
@Override
public Map generatorProjectCode(String projectName, String version, String basePackage) {
    Map retMap = new HashMap();
    VelocityInitializer.initVelocity();
    VelocityContext context = GenUtils.getVelocityContext(projectName, basePackage, version);
    // 获取模板列表
    List<String> templates = GenUtils.getTemplatesWeb(TableInfo.GenStyle.Project, null);
    return putIntoMap(retMap, context, templates);
}
 
Example #6
Source File: GenServiceImpl.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 根据表信息生成代码
 * @param zip 生成后的压缩包
 * @param tableName 表名
 */
private void generatorCode(ZipOutputStream zip, String tableName) {
    // 查询表信息
    TableInfo table = genMapper.selectTableByName(tableName);
    // 查询列信息
    List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
    // 表名转换成Java属性名
    String className = GenUtils.tableToJava(table.getTableName());
    table.setClassName(className);
    table.setClassname(StrUtil.lowerFirst(className));
    // 列信息
    table.setColumns(GenUtils.transColums(columns));
    // 设置主键
    table.setPrimaryKey(table.getColumnsLast());

    VelocityInitializer.initVelocity();

    String packageName = Global.getPackageName();
    String moduleName = GenUtils.getModuleName(packageName);

    VelocityContext context = GenUtils.getVelocityContext(table);

    // 获取模板列表
    List<String> templates = GenUtils.getTemplates();
    for (String template : templates) {
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, CharsetKit.UTF8);
        tpl.merge(context, sw);
        try {
            // 添加到zip
            zip.putNextEntry(new ZipEntry(Objects.requireNonNull(GenUtils.getFileName(template, table, moduleName))));
            IOUtils.write(sw.toString(), zip, CharsetKit.UTF8);
            IOUtils.closeQuietly(sw);
            zip.closeEntry();
        } catch (IOException e) {
            log.error("渲染模板失败,表名:" + table.getTableName(), e);
        }
    }
}