com.ruoyi.generator.util.GenUtils Java Examples

The following examples show how to use com.ruoyi.generator.util.GenUtils. 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: GenServiceImpl.java    From ruoyiplus with MIT License 6 votes vote down vote up
@Override
public TableInfo getTableInfo(String tableName, String prefix, Map<String, String[]> params) {
    TableInfo table = genMapper.selectTableByName(tableName);
    if (table != null) {
        // 查询列信息
        List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
        String tn = table.getTableName();
        if (StrUtil.isNotEmpty(prefix) && tn.startsWith(prefix)) {
            tn = tn.substring(prefix.length() - 1);
        }
        // 表名转换成Java属性名
        String className = GenUtils.tableToJava(tn);
        table.setClassName(className);
        table.setClassname(StringUtils.uncapitalize(className));
        // 列信息
        table.setColumns(GenUtils.transColums(columns, params));
        // 设置主键
        table.setPrimaryKey(table.getColumnsLast());
    }
    return table;
}
 
Example #2
Source File: GenTableServiceImpl.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 导入表结构
 * 
 * @param tableList 导入表列表
 * @param operName 操作人员
 */
@Override
@Transactional
public void importGenTable(List<GenTable> tableList, String operName)
{
    for (GenTable table : tableList)
    {
        try
        {
            String tableName = table.getTableName();
            GenUtils.initTable(table, operName);
            int row = genTableMapper.insertGenTable(table);
            if (row > 0)
            {
                // 保存列信息
                List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                for (GenTableColumn column : genTableColumns)
                {
                    GenUtils.initColumnField(column, table);
                    genTableColumnMapper.insertGenTableColumn(column);
                }
            }
        }
        catch (Exception 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 ruoyiplus with MIT License 5 votes vote down vote up
@Override
public Map previewCode(
        TableInfo.GenStyle codeStyle, String tableName, Map<String, String[]> params) {
    String prefix = GenUtils.getParam(params, "prefix");
    TableInfo table = getTableInfo(tableName, prefix, params);
    if (codeStyle == TableInfo.GenStyle.Web) {
        return generatorAdvCodeOnlyWeb(
                GenUtils.getParam(params, "tmpl"), table, table.getColumns(), params);
    }
    return null;
}
 
Example #7
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);
        }
    }
}
 
Example #8
Source File: GenServiceImpl.java    From ruoyiplus with MIT License 4 votes vote down vote up
@Override
public byte[] generatorCode(
        TableInfo.GenStyle codeStyle, String tableName, Map<String, String[]> params) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);
    // 查询表信息
    String prefix = GenUtils.getParam(params, "prefix");
    TableInfo table = getTableInfo(tableName, prefix, params);
    // 查询列信息
    List<ColumnInfo> columns = table.getColumns();
    // 列的额外信息处理
    for (ColumnInfo c : columns) {
        String[] ss = params.get(c.getAttrname());
        if (ss != null && ss.length == 1) {
            JSONObject _json = JSONUtil.parseObj(ss[0]);
            c.setWidth(Convert.toInt(_json.get("width"), 150));
            c.setVisible(Convert.toBool(_json.get("visible"), true));
            c.setEditField(Convert.toBool(_json.get("editable"), true));
            c.setSearchField(Convert.toBool(_json.get("searchable"), false));
            List<Verify> verifyList = Lists.newArrayList();
            if (_json.containsKey("verify")) {
                String vvs = Convert.toStr(_json.get("verify"));
                if (vvs.length() > 0) {
                    String[] vvArray = StrUtil.splitToArray(vvs, ',');
                    for (String _v : vvArray) {
                        verifyList.add(new Verify(_v));
                    }
                }
                c.setVerifyList(verifyList);
            } else {
                c.setVerifyList(verifyList);
            }
        }
    }
    // 生成代码
    String tmpl = GenUtils.getParam(params, "tmpl");
    if (StrUtil.isEmpty(tmpl)) {
        generatorCode(table, zip);
    } else {
        generatorAdvCodeOnlyWeb(tmpl, table, columns, params, zip);
    }
    IOUtils.closeQuietly(zip);
    return outputStream.toByteArray();
}