Search in sources :

Example 16 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-Oracle by yangzongzhuan.

the class GenTableServiceImpl method importGenTable.

/**
 * 导入表结构
 *
 * @param tableList 导入表列表
 */
@Override
@Transactional
public void importGenTable(List<GenTable> tableList) {
    String operName = SecurityUtils.getUsername();
    try {
        for (GenTable table : tableList) {
            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) {
        throw new ServiceException("导入失败:" + e.getMessage());
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-Oracle by yangzongzhuan.

the class GenTableServiceImpl method generatorCode.

/**
 * 查询表信息并生成代码
 */
private void generatorCode(String tableName, ZipOutputStream zip) {
    // 查询表信息
    GenTable table = genTableMapper.selectGenTableByName(tableName);
    // 获取菜单id序列,用于生成菜单sql语句
    long menuId = genTableMapper.selectMenuId();
    table.setMenuId(menuId);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    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.flush();
            zip.closeEntry();
        } catch (IOException e) {
            log.error("渲染模板失败,表名:" + table.getTableName(), e);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) GenTable(com.ruoyi.project.tool.gen.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 18 with GenTable

use of com.ruoyi.project.tool.gen.domain.GenTable in project RuoYi-Vue-Oracle by yangzongzhuan.

the class VelocityUtils method getImportList.

/**
 * 根据列类型获取导入包
 *
 * @param genTable 业务表对象
 * @return 返回需要导入的包列表
 */
public static HashSet<String> getImportList(GenTable genTable) {
    List<GenTableColumn> columns = genTable.getColumns();
    GenTable subGenTable = genTable.getSubTable();
    HashSet<String> importList = new HashSet<String>();
    if (StringUtils.isNotNull(subGenTable)) {
        importList.add("java.util.List");
    }
    for (GenTableColumn column : columns) {
        if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) {
            importList.add("java.util.Date");
            importList.add("com.fasterxml.jackson.annotation.JsonFormat");
        } else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) {
            importList.add("java.math.BigDecimal");
        }
    }
    return importList;
}
Also used : GenTable(com.ruoyi.project.tool.gen.domain.GenTable) GenTableColumn(com.ruoyi.project.tool.gen.domain.GenTableColumn) HashSet(java.util.HashSet)

Aggregations

GenTable (com.ruoyi.project.tool.gen.domain.GenTable)18 GenTableColumn (com.ruoyi.project.tool.gen.domain.GenTableColumn)8 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)8 Template (org.apache.velocity.Template)8 VelocityContext (org.apache.velocity.VelocityContext)8 ServiceException (com.ruoyi.common.exception.ServiceException)6 File (java.io.File)4 LinkedHashMap (java.util.LinkedHashMap)4 ZipEntry (java.util.zip.ZipEntry)4 Transactional (org.springframework.transaction.annotation.Transactional)4 JSON (com.alibaba.fastjson.JSON)2 JSONObject (com.alibaba.fastjson.JSONObject)2 Constants (com.ruoyi.common.constant.Constants)2 GenConstants (com.ruoyi.common.constant.GenConstants)2 CharsetKit (com.ruoyi.common.core.text.CharsetKit)2 SecurityUtils (com.ruoyi.common.utils.SecurityUtils)2 StringUtils (com.ruoyi.common.utils.StringUtils)2 GenTableColumnMapper (com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper)2 GenTableMapper (com.ruoyi.project.tool.gen.mapper.GenTableMapper)2