Search in sources :

Example 6 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)

Example 7 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 8 with TableConf

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

the class OracleTableService 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().toLowerCase(), ""));
    }
    System.out.println(tbConf);
    // 获取表各字段的信息
    getTableColumns(table, con);
    table.setPrimaryKey(getTablePrimaryKey(tableName, con));
    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()));
    table.setEntityCamelName(CodeUtil.isEmpty(tbConf.getEntityName()) ? CodeUtil.convertToCamelCase(table.getTableName()) : tbConf.getEntityName());
    table.setEntityName(CodeUtil.convertToFirstLetterLowerCaseCamelCase(table.getTableName()));
    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 9 with TableConf

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

the class OracleTableService 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 = "";
        // ORACLE查询所有表格名称命令
        showTablesSql = "select table_name from user_tables where table_name like ? and owner=upper(?)";
        ps = con.prepareStatement(showTablesSql);
        ps.setString(1, pattern);
        ps.setString(2, config.getDb().getUser());
        rs = ps.executeQuery();
        // 循环生成所有表的表信息
        while (rs.next()) {
            if (rs.getString(1) == null)
                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)

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