Search in sources :

Example 1 with TableConf

use of com.free.framework.code.tools.model.TableConf in project free-framework by a601942905git.

the class DataBase2File method generateFiles.

/**
 * 主方法,反转生成相关文件
 *
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public void generateFiles() throws IOException, ClassNotFoundException, SQLException {
    System.out.println("Generating...");
    Long start = System.currentTimeMillis();
    // 根据数据库类型获取数据库实例
    tableService = TableServiceFactory.getInstance(config.getDb().getDbType());
    // 设置数据库实例的配置信息
    tableService.setConfig(config);
    // 加载数据库驱动
    Class.forName(config.getDb().getDriver());
    // 连接数据库
    Connection con = DriverManager.getConnection(config.getDb().getUrl(), config.getDb().getUser(), config.getDb().getPwd());
    // 加载模块信息
    for (Module module : config.getModules()) {
        System.out.println("module=" + module.getName());
        // 如果没有配置数据表,则默认加载模块名为前缀的所有数据表
        if (module.getTables() == null || module.getTables().isEmpty()) {
            // 获取对应数据库中所有的表
            module.setTables(tableService.getAllTables(module.getName() + "%"));
        }
        // 系统的model文件
        StringBuffer sbModel = new StringBuffer();
        if (module.getTables() != null) {
            for (TableConf tbConf : module.getTables()) {
                tbConf.setPrefix(module.getPrefix());
                // 获取一个表的相关信息同时设置模块名称
                Table table = tableService.getTable(tbConf, module, con);
                genFile(table, module);
            }
        }
        // System.out.println(sbModel.toString());
        if ("dorado".equals(module.getFramework())) {
            // 生成model文件
            writeModelFile(module.getName().split("/")[0] + ".model.xml", sbModel.toString());
        }
    }
    con.close();
    ZipUtil.zip(config.getBaseDir(), config.getBaseDir() + ".zip");
    Long end = System.currentTimeMillis();
    System.out.println("Generate Success! total time = " + (end - start));
    System.out.println("Please check: " + config.getBaseDir());
}
Also used : TableConf(com.free.framework.code.tools.model.TableConf) Table(com.free.framework.code.tools.model.Table) Connection(java.sql.Connection) Module(com.free.framework.code.tools.model.Module)

Example 2 with TableConf

use of com.free.framework.code.tools.model.TableConf in project free-framework by a601942905git.

the class MysqlTableService method getTable.

/**
 * 获取指定表信息并封装成Table对象
 * @param tbConf
 * @param module
 * @param con
 */
public Table getTable(TableConf tbConf, Module module, Connection con) throws SQLException {
    String tableName = tbConf.getName();
    Table table = new Table();
    table.setTableFullName(tableName);
    table.setTableName(tableName);
    // 如果设置了表的前缀,则去掉表的前缀
    if (module.isDeleteTablePrefix() && !CodeUtil.isEmpty(tbConf.getPrefix())) {
        table.setTableName(tableName.toLowerCase().replaceFirst(tbConf.getPrefix(), ""));
    }
    System.out.println("==表信息===" + tbConf);
    // 设置模块名称
    module.setName(CodeUtil.getModuleByTableName(tableName));
    // 获取表各字段的信息
    getTableColumns(table, con);
    table.setPrimaryKey(getTablePrimaryKey(tableName, con));
    System.out.println("========table.getPrimaryKey()========" + table.getPrimaryKey() + "=====" + tableName);
    table.setPrimaryProperty(CodeUtil.convertToFirstLetterLowerCaseCamelCase(table.getPrimaryKey()));
    table.setRemark(getTableRemark(tableName, con));
    table.setPrimaryKeyType(getColumnType(table, table.getPrimaryKey()));
    table.setPrimaryPropertyType(CodeUtil.convertType(table.getPrimaryKeyType()));
    table.setPrimaryCamelProperty(CodeUtil.convertToCamelCase(table.getPrimaryKey()));
    // 根据表名获取实体类名称 如:表名称为free_user去掉前缀之后为user,转换之后为User
    table.setEntityCamelName(CodeUtil.convertToCamelCase(table.getTableName()));
    // 根据表名获取实体类名称参数 如:User转换之后为user
    table.setEntityCamelParamName(CodeUtil.firstLetterToLowerCase(table.getEntityCamelName()));
    /**
     *********************************此处原本的处理方式是将首字母小写,由于不满足,所以改成了首字母大写***********************************
     */
    table.setEntityName(CodeUtil.convertToCamelCase(table.getTableName()));
    table.setEntityParamName(CodeUtil.firstLetterToLowerCase(table.getEntityName()));
    table.setModule(module);
    // 设置子表的entity属性
    if (!tbConf.getSubTables().isEmpty()) {
        List<Table> subTables = new ArrayList<Table>();
        for (TableConf tc : tbConf.getSubTables()) {
            Table tb = getTable(tc, module, con);
            tb.setParentProperty(CodeUtil.convertToFirstLetterLowerCaseCamelCase(tc.getParentField()));
            tb.setRefType(tc.getRefType());
            subTables.add(tb);
        }
        table.setSubTables(subTables);
    }
    return table;
}
Also used : TableConf(com.free.framework.code.tools.model.TableConf) Table(com.free.framework.code.tools.model.Table) ArrayList(java.util.ArrayList)

Example 3 with TableConf

use of com.free.framework.code.tools.model.TableConf in project free-framework by a601942905git.

the class MysqlTableService method getAllTables.

/* 
     * 连接数据库获取所有表信息 
     */
public List<TableConf> getAllTables(String pattern) {
    if (CodeUtil.isEmpty(pattern)) {
        pattern = "*";
    }
    List<TableConf> tbConfList = new ArrayList<TableConf>();
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        Class.forName(config.getDb().getDriver());
        con = DriverManager.getConnection(config.getDb().getUrl(), config.getDb().getUser(), config.getDb().getPwd());
        // 获取所有表名
        String showTablesSql = "";
        // MySQL查询所有表格名称命令
        showTablesSql = "show tables like '" + pattern + "'";
        ps = con.prepareStatement(showTablesSql);
        rs = ps.executeQuery();
        // 循环生成所有表的表信息
        while (rs.next()) {
            if (rs.getString(1) == null)
                continue;
            System.out.println("config.getExcludeTables() +======" + config.getExcludeTables() + "====" + rs.getString(1));
            String excludeTables = config.getExcludeTables();
            // 过滤掉系统默认的表
            if (StringUtils.isNotEmpty(excludeTables) && excludeTables.indexOf(rs.getString(1) + "&") == -1) {
                continue;
            }
            TableConf cf = new TableConf();
            cf.setName(rs.getString(1));
            tbConfList.add(cf);
        }
        rs.close();
        ps.close();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tbConfList;
}
Also used : TableConf(com.free.framework.code.tools.model.TableConf) ArrayList(java.util.ArrayList)

Example 4 with TableConf

use of com.free.framework.code.tools.model.TableConf in project free-framework by a601942905git.

the class Config method readTableConfList.

/**
 * 以递归方式读取主从表关系
 * @param module
 * @return
 */
private static List<TableConf> readTableConfList(Element module) {
    List<TableConf> tableList = new ArrayList<TableConf>();
    List<Element> tables = XmlUtil.getChildElements(module, "table");
    for (Element e : tables) {
        TableConf table = initTableConf(e);
        // table标签下面还可以有table标签
        List<TableConf> subTables = readTableConfList(e);
        if (subTables != null && !subTables.isEmpty()) {
            table.getSubTables().addAll(subTables);
        }
        tableList.add(table);
    }
    return tableList;
}
Also used : TableConf(com.free.framework.code.tools.model.TableConf) Element(org.dom4j.Element) ArrayList(java.util.ArrayList)

Example 5 with TableConf

use of com.free.framework.code.tools.model.TableConf in project free-framework by a601942905git.

the class Config method initTableConf.

/**
 * 初始化配置的表格信息
 * @param e
 * @return
 */
private static TableConf initTableConf(Element e) {
    TableConf table = new TableConf();
    // 表对应的实体类名称
    Attribute attr = XmlUtil.getAttribute(e, "entityName");
    if (attr != null) {
        table.setEntityName(attr.getValue());
    }
    // 表名称
    Attribute name = XmlUtil.getAttribute(e, "name");
    if (name != null) {
        table.setName(name.getValue());
    }
    // 表前缀
    Attribute prefix = XmlUtil.getAttribute(e, "prefix");
    if (prefix != null) {
        table.setPrefix(prefix.getValue());
    }
    // 如果是主从表,则从表需设置该属性,表示父表的关联属性
    Attribute parentField = XmlUtil.getAttribute(e, "parentField");
    if (parentField != null) {
        table.setParentField(parentField.getValue());
    }
    // 表关联类型,分为OneToOne,OneToMany
    Attribute refType = XmlUtil.getAttribute(e, "refType");
    if (refType != null) {
        table.setRefType(refType.getValue());
    } else {
        // 默认OneToMany
        table.setRefType("OneToMany");
    }
    return table;
}
Also used : TableConf(com.free.framework.code.tools.model.TableConf) Attribute(org.dom4j.Attribute)

Aggregations

TableConf (com.free.framework.code.tools.model.TableConf)9 ArrayList (java.util.ArrayList)7 Table (com.free.framework.code.tools.model.Table)4 Module (com.free.framework.code.tools.model.Module)1 Connection (java.sql.Connection)1 Attribute (org.dom4j.Attribute)1 Element (org.dom4j.Element)1