diff --git a/README.md b/README.md index 2cb5dd650e51a1d72dbdbc42340ce3b9f1493869..0f389b2a029eb6789d1ef8cbdfe1b3ddae55ec08 100644 --- a/README.md +++ b/README.md @@ -4,159 +4,121 @@ CodeGenerator 本生成器只是将数据库中的表结构数据提取出来,然后将这份结构模型提交给模板引擎, 根据你自定义的模板生成你需要的代码。 -##配置文件 +## 配置文件 ```xml - - - - - lib/mysql-connector-java-5.1.36-bin.jar - - - D:\temp - - org.joy - - test - - - - - + + + + + lib/mysql-connector-java-5.1.36-bin.jar + + + D:\temp + + org.joy + + test + + + + + ``` -##模板中可用的变量和方法 - String targetProject 目标工程路径(代码保存的基准路径) - String basePackage 基准包 - String moduleName 模块名 - Map template 模板是否可用(是否勾选生成) - -### table 对象 -####属性 - String tableName 表名 - - String tableType 表类型 - - String tableAlias 表别名 - - String remarks 表注释 - - String remarksUnicode 表注释转Unicode后的字符串 - - String className 实体类名 - - String javaProperty 实体类作为属性时的名字 == ${table.className?uncap_first} - - List primaryKeys 主键集 - - List baseColumns 基本字段集 - - List columns 所有字段 == primaryKeys + baseColumns - - List importedKeys 所有 importedKeys - - List exportedKeys 所有 exportedKeys - - boolean hasDateColumn 是否有日期类型字段 - - boolean hasBigDecimalColumn 是否有 BigDecimal 字段 - - boolean hasNotNullColumn 是否有非空的基本类型字段 - - boolean hasNotBlankColumn 是否有非空的 String 字段 - - boolean hasSearchableColumn 是否有可搜索的 字段 - - -### Column -####属性 - String columnName 字段名 - - boolean primaryKey 是否为主键 - - boolean foreignKey 是否为外键 - - int size 字段长度 - - int decimalDigits 小数位长度 - - boolean nullable 是否可空 - - boolean autoincrement 是否自增 - - boolean unique 是否唯一值 - - boolean indexed 是否有索引 +## 模板中可用的变量和方法 - String defaultValue 默认值 - - String remarks 注释 - - String remarksUnicode 数字转Unicode后的字符串 - - int jdbcType 对应 java.sql.Types - - String jdbcTypeName 对应jdbcType的名称 - - String javaProperty 属性名 - - String javaType java类型,比如String、Long、Integer - - String javaPrimitiveType java基本类型,如果不是基本类型时等同于 javaType - - String fullJavaType 完整的Java类型,比如 java.lang.String - - String getterMethodName Get方法名 - - String setterMethodName Set方法名 - - boolean display 是否显示 - - boolean searchable 是否可搜索 +类型|属性名|说明 +----|----|---- +String | targetProject | 目标工程路径(代码保存的基准路径) +String | basePackage | 基准包 +String | moduleName | 模块名 +String | author | 开发者 +Map | template | 模板是否可用(是否勾选生成) - boolean orderable 是否可排序 - - String dict 数据字典名 - - -####方法 - boolean isString() 是否是字符串 - - boolean isFloatNumber() 是否是浮点型,含Float、Double、BigDecimal - - boolean isIntegerNumber() 是否是整型,含Byte、Short、Integer、Long - - boolean isBigDecimal() 是否是BigDecimal类型 - - boolean isBoolean() 是否是布尔类型 - - boolean isDate() 是否有日期类型,含Date、Timestamp、Time - - boolean isBLOB() 是否有 BLOB、CLOB、LONGVARCHAR、LONGVARBINARY或VARBINARY - - boolean isPrimitiveType() 是否是基本类型 - - boolean hasDict() 是否使用了数据字典 +### Table对象 +#### 可用的属性 + +类型|属性名|说明 +----|----|---- +String | tableName | 表名 +String | tableType | 表类型 +String | tableAlias | 表别名 +String | remarks | 表注释 +String | remarksUnicode | 表注释转Unicode后的字符串 +String | className | 实体类名 +String | javaProperty | 实体类作为属性时的名字 == ${table.className?uncap_first} +List | primaryKeys | 主键集 +List | baseColumns | 基本字段集 +List | columns | 所有字段 == primaryKeys + baseColumns +List | importedKeys | 所有importedKeys +List | exportedKeys | 所有exportedKeys +boolean | hasDateColumn | 是否有日期类型字段 +boolean | hasBigDecimalColumn | 是否有BigDecimal 字段 +boolean | hasNotNullColumn | 是否有非空的基本类型字段 +boolean | hasNotBlankColumn | 是否有非空的String 字段 +boolean | hasSearchableColumn | 是否有可搜索的字段 + + +### Column对象 +#### 属性 + +类型|属性名|说明 +----|----|---- +String | columnName | 字段名 +boolean | primaryKey | 是否为主键 +boolean | foreignKey | 是否为外键 +int | size | 字段长度 +int | decimalDigits | 小数位长度 +boolean | nullable | 是否可空 +boolean | autoincrement | 是否自增 +boolean | unique | 是否唯一值 +boolean | indexed | 是否有索引 +String | defaultValue | 默认值 +String | remarks | 注释 +String | remarksUnicode | 数字转Unicode后的字符串 +int | jdbcType | 对应的java.sql.Types +String | jdbcTypeName | 对应jdbcType的名称 +String | javaProperty | 属性名 +String | javaType | java类型,比如String、Long、Integer +String | javaPrimitiveType | java基本类型,如果不是基本类型时等同于javaType +String | fullJavaType | 完整的Java类型,比如java.lang.String +String | getterMethodName | Get方法名 +String | setterMethodName | Set方法名 +boolean | display | 是否显示 +boolean | searchable | 是否可搜索 +boolean | sortable | 是否可排序 +String | dict | 数据字典名 + +#### 方法 +类型|属性名|说明 +----|----|---- +boolean | isString() | 是否是字符串 +boolean | isFloatNumber() | 是否是浮点型,含Float、Double、BigDecimal +boolean | isIntegerNumber() | 是否是整型,含Byte、Short、Integer、Long +boolean | isBigDecimal() | 是否是BigDecimal类型 +boolean | isBoolean() | 是否是布尔类型 +boolean | isDate() | 是否有日期类型,含Date、Timestamp、Time +boolean | isBLOB() | 是否有BLOB、CLOB、LONGVARCHAR、LONGVARBINARY或VARBINARY +boolean | isPrimitiveType() | 是否是基本类型 +boolean | hasDict() | 是否使用了数据字典 ### Key - String pkTableName PKTABLE_NAME - - String pkColumnName PKCOLUMN_NAME - - String fkTableName FKTABLE_NAME - - String fkColumnName FKCOLUMN_NAME - - Integer seq KEY_SEQ +类型|属性名|说明 +----|----|---- +String | pkTableName | PKTABLE_NAME +String | pkColumnName | PKCOLUMN_NAME +String | fkTableName | FKTABLE_NAME +String | fkColumnName | FKCOLUMN_NAME +Integer | seq | KEY_SEQ ##作者 diff --git a/bin.jar b/bin.jar index 7050fb0848587a00462e6636e490a7c95d5255f9..831ea99c7f53233887693dfaf5880a4ff60ddc75 100644 Binary files a/bin.jar and b/bin.jar differ diff --git a/configuration.xml b/configuration.xml index df2996643ceb48634d40572be1b74cd184aaece5..f7a3ad1d42c6f0ac0a1d20a6b2bea44b244e98d0 100644 --- a/configuration.xml +++ b/configuration.xml @@ -1,29 +1,79 @@ - - lib/ojdbc14.jar - lib/sqljdbc4.jar - lib/mysql-connector-java-5.1.36-bin.jar - lib/postgresql-8.2-505.jdbc2.jar - - - - - D:\temp - org.joy - test - - - - + + lib/ojdbc14.jar + lib/sqljdbc4.jar + lib/mysql-connector-java-5.1.46.jar + lib/postgresql-8.2-505.jdbc2.jar + + + + com.microsoft.sqlserver.jdbc.SQLServerDriver + jdbc:sqlserver://192.168.1.206:1433;databaseName=hty_java + sa + sa + + + + com.mysql.jdbc.Driver + jdbc:mysql://192.168.1.222:3306/test?characterEncoding=UTF-8 + root + root + + + + d:\temp\ + com.hty.frame + 测试模块 + + + + + + + + + diff --git a/lib/mysql-connector-java-5.1.36-bin.jar b/lib/mysql-connector-java-5.1.36-bin.jar deleted file mode 100644 index 21b99db0971c6070b0b7bfaa45f0187b64576458..0000000000000000000000000000000000000000 Binary files a/lib/mysql-connector-java-5.1.36-bin.jar and /dev/null differ diff --git a/lib/mysql-connector-java-5.1.46.jar b/lib/mysql-connector-java-5.1.46.jar new file mode 100644 index 0000000000000000000000000000000000000000..25532374cff0a259dab66497d7dc03db6cc42762 Binary files /dev/null and b/lib/mysql-connector-java-5.1.46.jar differ diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF index 5d51e45e56426085cf0e7733347bfb89a2bbdd5d..22e07f7a49212648e5cf4363b3d914997d9d92f1 100644 --- a/src/META-INF/MANIFEST.MF +++ b/src/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -Main-Class: org.joy.generator.Generator +Main-Class: org.joy.Application Class-Path: lib\freemarker.jar lib\velocity-1.7-dep.jar lib\log4j-1.2.16.jar lib\beautyeye_lnf.jar diff --git a/src/org/joy/Application.java b/src/org/joy/Application.java new file mode 100644 index 0000000000000000000000000000000000000000..029e77baa5a735b5eb63baf769c8aa844edaaeca --- /dev/null +++ b/src/org/joy/Application.java @@ -0,0 +1,12 @@ +package org.joy; + +import org.joy.generator.Generator; + +public class Application { + /** + * Launch the application. + */ + public static void main(String[] args) { + Generator.main(args); + } +} diff --git a/src/org/joy/config/Configuration.java b/src/org/joy/config/Configuration.java index 33fda8c9f05ebcb11470dd275ef215e5833845f6..eb5bd0e6ba2a12594371c43c620a9528b8d2a872 100644 --- a/src/org/joy/config/Configuration.java +++ b/src/org/joy/config/Configuration.java @@ -15,11 +15,17 @@ */ package org.joy.config; -import java.io.File; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.joy.config.model.DatabaseElement; +import org.joy.config.model.TemplateElement; +import org.joy.exception.ApplicationException; +import org.joy.util.StringUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -32,17 +38,11 @@ import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; - -import org.apache.log4j.Logger; -import org.joy.config.model.DatabaseElement; -import org.joy.config.model.TemplateElement; -import org.joy.exception.ApplicationException; -import org.joy.util.StringUtil; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; public class Configuration { @@ -56,6 +56,7 @@ public class Configuration { private String basePackage; private String moduleName; private List templates; + private String author; public Configuration(String classPath){ this.configurationFile = classPath + CONFIGURATION_FILE; @@ -77,9 +78,10 @@ public class Configuration { parseConnections(doc, path); parseTemplates(doc, path); - targetProject = path.evaluate("/configuration/targetProject/text()", doc); - basePackage = path.evaluate("/configuration/basePackage/text()", doc); - moduleName = path.evaluate("/configuration/moduleName/text()", doc); + targetProject = StringUtils.trim(path.evaluate("/configuration/targetProject/text()", doc)); + basePackage = StringUtils.trim(path.evaluate("/configuration/basePackage/text()", doc)); + moduleName = StringUtils.trim(path.evaluate("/configuration/moduleName/text()", doc)); + author = StringUtils.trim(path.evaluate("/configuration/author/text()", doc)); } catch (Exception e) { throw new ApplicationException(e); } @@ -90,7 +92,7 @@ public class Configuration { XPathConstants.NODESET); if (classpathEntrys != null) { for (int i = 0; i < classpathEntrys.getLength(); i++) { - String entry = parseElementNodeValue(classpathEntrys.item(i)); + String entry = StringUtils.trim(parseElementNodeValue(classpathEntrys.item(i))); if (StringUtil.isNotEmpty(entry)) { classPathEntries.add(entry); } @@ -109,12 +111,12 @@ public class Configuration { } private void parseDatabase(Node node, XPath path) throws XPathExpressionException { - String name = path.evaluate("@name", node); - String driverClass = path.evaluate("./driverClass/text()", node); - String url = path.evaluate("./url/text()", node); - String username = path.evaluate("./username/text()", node); - String password = path.evaluate("./password/text()", node); - String schema = path.evaluate("./schema/text()", node); + String name = StringUtils.trim(path.evaluate("@name", node)); + String driverClass = StringUtils.trim(path.evaluate("./driverClass/text()", node)); + String url = StringUtils.trim( path.evaluate("./url/text()", node)); + String username = StringUtils.trim(path.evaluate("./username/text()", node)); + String password = StringUtils.trim( path.evaluate("./password/text()", node)); + String schema = StringUtils.trim(path.evaluate("./schema/text()", node)); if (StringUtil.isNotEmpty(name) && StringUtil.isNotEmpty(driverClass) && StringUtil.isNotEmpty(url) && StringUtil.isNotEmpty(username)) { @@ -133,15 +135,14 @@ public class Configuration { } private void parseTemplate(Node node, XPath path) throws XPathExpressionException { - String id=null, name = null, engine = null, templateFile = null, targetPath = null, targetFileName = null, encoding = null; - templateFile = path.evaluate("./templateFile/text()", node); - targetPath = path.evaluate("./targetPath/text()", node); - targetFileName = path.evaluate("./targetFileName/text()", node); - encoding = path.evaluate("./encoding/text()", node); + String templateFile = StringUtils.trim(path.evaluate("./templateFile/text()", node)); + String targetPath = StringUtils.trim(path.evaluate("./targetPath/text()", node)); + String targetFileName = StringUtils.trim(path.evaluate("./targetFileName/text()", node)); + String encoding = StringUtils.trim(path.evaluate("./encoding/text()", node)); - id = path.evaluate("@id", node); - name = path.evaluate("@name", node); - engine = path.evaluate("@engine", node); + String id = StringUtils.trim(path.evaluate("@id", node)); + String name = StringUtils.trim(path.evaluate("@name", node)); + String engine = StringUtils.trim(path.evaluate("@engine", node)); if (StringUtil.isEmpty(engine)) { engine = "freemarker"; @@ -253,6 +254,10 @@ public class Configuration { e.appendChild(doc.createTextNode(moduleName)); root.appendChild(e); + e = doc.createElement("author"); + e.appendChild(doc.createTextNode(author)); + root.appendChild(e); + Element templatesEle = doc.createElement("templates"); root.appendChild(templatesEle); for (TemplateElement t : templates) { @@ -341,7 +346,15 @@ public class Configuration { this.moduleName = moduleName; } - public List getTemplates() { + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public List getTemplates() { return templates; } } diff --git a/src/org/joy/db/DefaultDatabase.java b/src/org/joy/db/DefaultDatabase.java index 9df19ddbf37a341a95a8ae4e579a36dbd38026db..cc78f99c240f9edb5a1afdc6bbd6773b2b3fbf64 100644 --- a/src/org/joy/db/DefaultDatabase.java +++ b/src/org/joy/db/DefaultDatabase.java @@ -15,12 +15,6 @@ */ package org.joy.db; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; - import org.joy.config.TypeMapping; import org.joy.db.model.Column; import org.joy.db.model.Key; @@ -28,257 +22,278 @@ import org.joy.db.model.Table; import org.joy.db.model.util.JdbcTypeResolver; import org.joy.util.StringUtil; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; + public class DefaultDatabase extends Database { - private static final String COLUMN_NAME = "COLUMN_NAME"; + private static final String COLUMN_NAME = "COLUMN_NAME"; - public static final String PKTABLE_NAME = "PKTABLE_NAME"; - public static final String PKCOLUMN_NAME = "PKCOLUMN_NAME"; - public static final String FKTABLE_NAME = "FKTABLE_NAME"; - public static final String FKCOLUMN_NAME = "FKCOLUMN_NAME"; - public static final String KEY_SEQ = "KEY_SEQ"; + public static final String PKTABLE_NAME = "PKTABLE_NAME"; + public static final String PKCOLUMN_NAME = "PKCOLUMN_NAME"; + public static final String FKTABLE_NAME = "FKTABLE_NAME"; + public static final String FKCOLUMN_NAME = "FKCOLUMN_NAME"; + public static final String KEY_SEQ = "KEY_SEQ"; - public DefaultDatabase(Connection connection, TypeMapping typeMapping){ - super(connection, typeMapping); - } + public DefaultDatabase(Connection connection, TypeMapping typeMapping) { + super(connection, typeMapping); + } - /* (non-Javadoc) - * @see org.joy.db.Database#getTable(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public Table getTable(String catalog, String schema, String tableName) throws SQLException { - ResultSet rs = null; - Table table = null; - String schemaPattern = null; - try { - if (StringUtil.isNotEmpty(schema)) { - schemaPattern = schema; - } - rs = connection.getMetaData().getTables(catalog, schemaPattern, tableName, new String[] { "TABLE", "VIEW" }); - if (rs.next()) { - table = new Table(); - table.setCatalog(rs.getString("TABLE_CAT")); - table.setSchema(rs.getString("TABLE_SCHEM")); - table.setTableName(tableName); - table.setRemarks(rs.getString("REMARKS")); - table.setTableType(rs.getString("TABLE_TYPE")); + /* (non-Javadoc) + * @see org.joy.db.Database#getTable(java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public Table getTable(String catalog, String schema, String tableName) throws SQLException { + ResultSet rs = null; + Table table = null; + String schemaPattern = null; + try { + if (StringUtil.isNotEmpty(schema)) { + schemaPattern = schema; + } + rs = connection.getMetaData().getTables(catalog, schemaPattern, tableName, new String[]{"TABLE", "VIEW"}); + if (rs.next()) { + table = new Table(); + table.setCatalog(rs.getString("TABLE_CAT")); + table.setSchema(rs.getString("TABLE_SCHEM")); + table.setTableName(tableName); + table.setRemarks(rs.getString("REMARKS")); + table.setTableType(rs.getString("TABLE_TYPE")); - introspectPrimaryKeys(table); - introspectColumns(table); - introspectForeignKeys(table); - introspectExportedKeys(table); - introspectIndex(table); - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); - } - return table; + introspectPrimaryKeys(table); + introspectColumns(table); + introspectForeignKeys(table); + introspectExportedKeys(table); + introspectIndex(table); + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); } + return table; + } - protected void introspectPrimaryKeys(Table table) throws SQLException { - ResultSet rs = null; - try { - rs = connection.getMetaData().getPrimaryKeys(null, table.getSchema(), table.getTableName()); - while (rs.next()) { - String columnName = rs.getString(COLUMN_NAME); - Column column = table.getColumn(columnName); - if (column == null) { - column = new Column(columnName); - table.addPrimaryKey(column); - } - column.setPrimaryKey(true); - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); + protected void introspectPrimaryKeys(Table table) throws SQLException { + ResultSet rs = null; + try { + rs = connection.getMetaData().getPrimaryKeys(null, table.getSchema(), table.getTableName()); + while (rs.next()) { + String columnName = rs.getString(COLUMN_NAME); + Column column = table.getColumn(columnName); + if (column == null) { + column = new Column(columnName); + table.addPrimaryKey(column); } + column.setPrimaryKey(true); + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); } + } - protected void introspectColumns(Table table) throws SQLException { - ResultSet rs = null; - try { - rs = connection.getMetaData().getColumns(null, table.getSchema(), table.getTableName(), "%"); - while (rs.next()) { - String columnName = rs.getString(COLUMN_NAME); - if (StringUtil.isEmpty(columnName)) { - continue; - } + protected void introspectColumns(Table table) throws SQLException { + ResultSet rs = null; + try { + rs = connection.getMetaData().getColumns(null, table.getSchema(), table.getTableName(), "%"); + while (rs.next()) { + String columnName = rs.getString(COLUMN_NAME); + if (StringUtil.isEmpty(columnName)) { + continue; + } - Column column = table.getColumn(columnName); - if (column == null) { - column = new Column(columnName); - table.addBaseColumn(column); - } - column.setJdbcType(rs.getInt("DATA_TYPE")); - column.setSize(rs.getInt("COLUMN_SIZE")); - column.setNullable(rs.getInt("NULLABLE") == 1); - column.setDefaultValue(rs.getString("COLUMN_DEF")); - column.setDecimalDigits(rs.getInt("DECIMAL_DIGITS")); - column.setRemarks(rs.getString("REMARKS")); - if (hasColumn(rs, "IS_AUTOINCREMENT")) { - column.setAutoincrement(rs.getBoolean("IS_AUTOINCREMENT")); + Column column = table.getColumn(columnName); + if (column == null) { + column = new Column(columnName); + table.addBaseColumn(column); + } + column.setJdbcType(rs.getInt("DATA_TYPE")); + column.setSize(rs.getInt("COLUMN_SIZE")); + column.setNullable(rs.getInt("NULLABLE") == 1); + column.setDefaultValue(rs.getString("COLUMN_DEF")); + column.setDecimalDigits(rs.getInt("DECIMAL_DIGITS")); + column.setRemarks(rs.getString("REMARKS")); + if (hasColumn(rs, "IS_AUTOINCREMENT")) { + boolean autoincrement = false; + try { + autoincrement = rs.getBoolean("IS_AUTOINCREMENT"); + } catch (Exception ex) { + try { + String am = rs.getString("IS_AUTOINCREMENT"); + if (am != null) { + am = am.trim(); + if ("YES".equals(am) || "1".equals(am)) { + autoincrement = true; } - - column.setJdbcTypeName(JdbcTypeResolver.getJdbcTypeName(column.getJdbcType())); - column.setJavaType(typeMapping.calculateJavaType(column)); - column.setFullJavaType(typeMapping.calculateFullJavaType(column)); - column.setJavaProperty(StringUtil.getCamelCaseString(columnName, false)); + } + } catch (Exception ex1) { } - - } catch (SQLException e) { - throw e; - } finally { - close(rs); + } + column.setAutoincrement(autoincrement); } + + column.setJdbcTypeName(JdbcTypeResolver.getJdbcTypeName(column.getJdbcType())); + column.setJavaType(typeMapping.calculateJavaType(column)); + column.setFullJavaType(typeMapping.calculateFullJavaType(column)); + column.setJavaProperty(StringUtil.getCamelCaseString(columnName, false)); + } + + } catch (SQLException e) { + throw e; + } finally { + close(rs); } + } - // 获得外键的信息 - protected void introspectForeignKeys(Table table) throws SQLException { - ResultSet rs = null; - try { - rs = connection.getMetaData().getImportedKeys(null, table.getSchema(), table.getTableName()); - while (rs.next()) { - String columnName = rs.getString(FKCOLUMN_NAME); - if (StringUtil.isEmpty(columnName)) { - continue; - } + // 获得外键的信息 + protected void introspectForeignKeys(Table table) throws SQLException { + ResultSet rs = null; + try { + rs = connection.getMetaData().getImportedKeys(null, table.getSchema(), table.getTableName()); + while (rs.next()) { + String columnName = rs.getString(FKCOLUMN_NAME); + if (StringUtil.isEmpty(columnName)) { + continue; + } - Column column = table.getColumn(columnName); - if (column != null) { - column.setForeignKey(true); - } - String pkTableName = rs.getString(PKTABLE_NAME); - String pkColumnName = rs.getString(PKCOLUMN_NAME); - String fkTableName = rs.getString(FKTABLE_NAME); - String fkColumnName = rs.getString(FKCOLUMN_NAME); - String seq = rs.getString(KEY_SEQ); - Integer iseq = new Integer(seq); - table.addImportedKey(new Key(pkTableName, pkColumnName, fkTableName, fkColumnName, iseq)); - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); + Column column = table.getColumn(columnName); + if (column != null) { + column.setForeignKey(true); } + String pkTableName = rs.getString(PKTABLE_NAME); + String pkColumnName = rs.getString(PKCOLUMN_NAME); + String fkTableName = rs.getString(FKTABLE_NAME); + String fkColumnName = rs.getString(FKCOLUMN_NAME); + String seq = rs.getString(KEY_SEQ); + Integer iseq = new Integer(seq); + table.addImportedKey(new Key(pkTableName, pkColumnName, fkTableName, fkColumnName, iseq)); + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); } + } - // 获得外键的信息 - protected void introspectExportedKeys(Table table) throws SQLException { - ResultSet rs = null; - try { - rs = connection.getMetaData().getExportedKeys(null, table.getSchema(), table.getTableName()); - while (rs.next()) { - String columnName = rs.getString(FKCOLUMN_NAME); - if (StringUtil.isEmpty(columnName)) { - continue; - } - String pkTableName = rs.getString(PKTABLE_NAME); - String pkColumnName = rs.getString(PKCOLUMN_NAME); - String fkTableName = rs.getString(FKTABLE_NAME); - String fkColumnName = rs.getString(FKCOLUMN_NAME); - String seq = rs.getString(KEY_SEQ); - Integer iseq = new Integer(seq); - table.addExportedKey(new Key(pkTableName, pkColumnName, fkTableName, fkColumnName, iseq)); - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); + // 获得外键的信息 + protected void introspectExportedKeys(Table table) throws SQLException { + ResultSet rs = null; + try { + rs = connection.getMetaData().getExportedKeys(null, table.getSchema(), table.getTableName()); + while (rs.next()) { + String columnName = rs.getString(FKCOLUMN_NAME); + if (StringUtil.isEmpty(columnName)) { + continue; } + String pkTableName = rs.getString(PKTABLE_NAME); + String pkColumnName = rs.getString(PKCOLUMN_NAME); + String fkTableName = rs.getString(FKTABLE_NAME); + String fkColumnName = rs.getString(FKCOLUMN_NAME); + String seq = rs.getString(KEY_SEQ); + Integer iseq = new Integer(seq); + table.addExportedKey(new Key(pkTableName, pkColumnName, fkTableName, fkColumnName, iseq)); + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); } + } - // 获得索引 - protected void introspectIndex(Table table) throws SQLException { - ResultSet rs = null; - try { - rs = connection.getMetaData().getIndexInfo(null, table.getSchema(), table.getTableName(), true, true); - while (rs.next()) { - String columnName = rs.getString(COLUMN_NAME); - if (StringUtil.isEmpty(columnName)) { - continue; - } - Column column = table.getColumn(columnName); - if (column != null) { - column.setUnique(!rs.getBoolean("NON_UNIQUE")); - column.setIndexed(true); - } - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); + // 获得索引 + protected void introspectIndex(Table table) throws SQLException { + ResultSet rs = null; + try { + rs = connection.getMetaData().getIndexInfo(null, table.getSchema(), table.getTableName(), true, true); + while (rs.next()) { + String columnName = rs.getString(COLUMN_NAME); + if (StringUtil.isEmpty(columnName)) { + continue; } + Column column = table.getColumn(columnName); + if (column != null) { + column.setUnique(!rs.getBoolean("NON_UNIQUE")); + column.setIndexed(true); + } + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); } + } - /** - * 查询表注释 - * - * @param table - * @param commentSql 查询表注释的SQL, 含一个占位符 (tableName) - * @param columnComments 注释列列名 - * @throws SQLException - */ - protected void introspectTableComments(Table table, String commentSql, String columnComments) throws SQLException { - PreparedStatement psmt = null; - ResultSet rs = null; - try { - psmt = connection.prepareStatement(commentSql); - psmt.setString(1, table.getTableName()); - rs = psmt.executeQuery(); - if (rs.next()) { - table.setRemarks(rs.getString(columnComments)); - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); - close(psmt); - } + /** + * 查询表注释 + * + * @param table + * @param commentSql 查询表注释的SQL, 含一个占位符 (tableName) + * @param columnComments 注释列列名 + * @throws SQLException + */ + protected void introspectTableComments(Table table, String commentSql, String columnComments) throws SQLException { + PreparedStatement psmt = null; + ResultSet rs = null; + try { + psmt = connection.prepareStatement(commentSql); + psmt.setString(1, table.getTableName()); + rs = psmt.executeQuery(); + if (rs.next()) { + table.setRemarks(rs.getString(columnComments)); + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); + close(psmt); } + } - /** - * 查询表中列注释 - * - * @param table - * @param colCommentSql 查询表中列注释的SQL, 含一个占位符 (tableName) - * @param columnColumnName 字段名列列名 - * @param columnComments 注释列列名 - * @throws SQLException - */ - protected void introspectTableColumnsComments(Table table, String colCommentSql, String columnColumnName, - String columnComments) throws SQLException { - PreparedStatement psmt = null; - ResultSet rs = null; - try { - psmt = connection.prepareStatement(colCommentSql); - psmt.setString(1, table.getTableName()); - rs = psmt.executeQuery(); - while (rs.next()) { - String columnName = rs.getString(columnColumnName); - Column column = table.getColumn(columnName); - if (column != null) { - column.setRemarks(rs.getString(columnComments)); - } - } - } catch (SQLException e) { - throw e; - } finally { - close(rs); - close(psmt); + /** + * 查询表中列注释 + * + * @param table + * @param colCommentSql 查询表中列注释的SQL, 含一个占位符 (tableName) + * @param columnColumnName 字段名列列名 + * @param columnComments 注释列列名 + * @throws SQLException + */ + protected void introspectTableColumnsComments(Table table, String colCommentSql, String columnColumnName, + String columnComments) throws SQLException { + PreparedStatement psmt = null; + ResultSet rs = null; + try { + psmt = connection.prepareStatement(colCommentSql); + psmt.setString(1, table.getTableName()); + rs = psmt.executeQuery(); + while (rs.next()) { + String columnName = rs.getString(columnColumnName); + Column column = table.getColumn(columnName); + if (column != null) { + column.setRemarks(rs.getString(columnComments)); } + } + } catch (SQLException e) { + throw e; + } finally { + close(rs); + close(psmt); } + } - protected boolean hasColumn(ResultSet rs, String columnName) throws SQLException { - ResultSetMetaData rsmd = rs.getMetaData(); - int columns = rsmd.getColumnCount(); - for (int x = 1; x <= columns; x++) { - if (columnName.equals(rsmd.getColumnName(x))) { - return true; - } - } - return false; + protected boolean hasColumn(ResultSet rs, String columnName) throws SQLException { + ResultSetMetaData rsmd = rs.getMetaData(); + int columns = rsmd.getColumnCount(); + for (int x = 1; x <= columns; x++) { + if (columnName.equals(rsmd.getColumnName(x))) { + return true; + } } + return false; + } } diff --git a/src/org/joy/db/model/Column.java b/src/org/joy/db/model/Column.java index a75596ef0bf764eb1c8c31bd95fa3d14127e34ee..12e4d89f5c3be3da74bf14840e5bd59c1cd2b083 100644 --- a/src/org/joy/db/model/Column.java +++ b/src/org/joy/db/model/Column.java @@ -15,11 +15,11 @@ */ package org.joy.db.model; -import java.io.Serializable; - import org.joy.db.model.util.JavaTypeResolver; import org.joy.util.StringUtil; +import java.io.Serializable; + public class Column implements Serializable { private static final long serialVersionUID = 6987289314682844881L; @@ -54,8 +54,8 @@ public class Column implements Serializable { private String fullJavaType; private String editor; private boolean display = true; - private boolean searchable; - private boolean sortable = false; + private boolean searchable = true; + private boolean sortable = true; private String dict = ""; public Column(String columnName){ diff --git a/src/org/joy/generator/GenerationDialog.java b/src/org/joy/generator/GenerationDialog.java index a697469002d82d02b66354400f8cc5bb7f7b2abb..c3a17edaefe81d8428e735037fd4339638684006 100644 --- a/src/org/joy/generator/GenerationDialog.java +++ b/src/org/joy/generator/GenerationDialog.java @@ -15,27 +15,6 @@ */ package org.joy.generator; -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.FlowLayout; -import java.awt.Rectangle; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.util.HashMap; -import java.util.Map; - -import javax.swing.JButton; -import javax.swing.JDialog; -import javax.swing.JLabel; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTextField; -import javax.swing.ListSelectionModel; -import javax.swing.SwingConstants; -import javax.swing.border.EmptyBorder; - import org.apache.log4j.Logger; import org.joy.config.Configuration; import org.joy.config.model.TemplateElement; @@ -45,248 +24,275 @@ import org.joy.generator.engine.TemplateEngine; import org.joy.generator.ui.component.CheckBoxList; import org.joy.util.StringUtil; -public class GenerationDialog extends JDialog { +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.util.HashMap; +import java.util.Map; - private static final Logger LOGGER = Logger.getLogger(GenerationDialog.class); - - private static final long serialVersionUID = 6159091936841897188L; - - private final JPanel contentPanel = new JPanel(); - private JTextField textTargetProject; - private JTextField textBasePackage; - private JTextField textModuleName; - private CheckBoxList templatesList; - - private Configuration configuration; - private Table tableModel; - private JTextField textTableName; - private JTextField textTableAlias; - private EngineBuilder engineBuilder; - - /** - * Create the dialog. - */ - public GenerationDialog(Configuration configuration, Table tableModel, String classPath){ - - setModal(true); - setTitle("生成代码"); - setResizable(false); - setBounds(100, 100, 419, 485); - getContentPane().setLayout(new BorderLayout()); - contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); - getContentPane().add(contentPanel, BorderLayout.CENTER); - contentPanel.setLayout(null); - - JLabel lblTargetProject = new JLabel("代码保存路径"); - lblTargetProject.setHorizontalAlignment(SwingConstants.TRAILING); - lblTargetProject.setBounds(10, 13, 72, 15); - contentPanel.add(lblTargetProject); - - textTargetProject = new JTextField(); - textTargetProject.setBounds(92, 10, 311, 21); - contentPanel.add(textTargetProject); - textTargetProject.setColumns(10); - - textBasePackage = new JTextField(); - textBasePackage.setBounds(92, 41, 311, 21); - contentPanel.add(textBasePackage); - textBasePackage.setColumns(10); - - textModuleName = new JTextField(); - textModuleName.setBounds(92, 72, 130, 21); - contentPanel.add(textModuleName); - textModuleName.setColumns(10); - - JLabel lblBasePackage = new JLabel("基准包"); - lblBasePackage.setHorizontalAlignment(SwingConstants.TRAILING); - lblBasePackage.setBounds(28, 44, 54, 15); - contentPanel.add(lblBasePackage); - - JLabel lblModuleName = new JLabel("模块名"); - lblModuleName.setHorizontalAlignment(SwingConstants.TRAILING); - lblModuleName.setBounds(28, 75, 54, 15); - contentPanel.add(lblModuleName); - - JPanel panel = new JPanel(); - panel.setBounds(92, 103, 311, 234); - contentPanel.add(panel); - panel.setLayout(new BorderLayout(0, 0)); - - JScrollPane scrollPane = new JScrollPane(); - panel.add(scrollPane); - - templatesList = new CheckBoxList(); - templatesList.setBounds(0, 0, 1, 1); - scrollPane.setViewportView(templatesList); - - JLabel lblTemplates = new JLabel("代码模板"); - lblTemplates.setHorizontalAlignment(SwingConstants.TRAILING); - lblTemplates.setBounds(28, 103, 54, 15); - contentPanel.add(lblTemplates); - - textTableName = new JTextField(); - textTableName.setEditable(false); - textTableName.setBounds(92, 347, 130, 21); - contentPanel.add(textTableName); - textTableName.setColumns(10); - - textTableAlias = new JTextField(); - textTableAlias.setBounds(92, 378, 130, 21); - contentPanel.add(textTableAlias); - textTableAlias.setColumns(10); - - JLabel lblTableName = new JLabel("表名"); - lblTableName.setHorizontalAlignment(SwingConstants.TRAILING); - lblTableName.setBounds(28, 350, 54, 15); - contentPanel.add(lblTableName); - - JLabel lblTableAlias = new JLabel("别名"); - lblTableAlias.setHorizontalAlignment(SwingConstants.TRAILING); - lblTableAlias.setBounds(28, 381, 54, 15); - contentPanel.add(lblTableAlias); - JPanel buttonPane = new JPanel(); - getContentPane().add(buttonPane, BorderLayout.SOUTH); - buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5)); - - JButton btnOK = new JButton("生成"); - buttonPane.add(btnOK); - btnOK.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - okButtonClick(); - } - }); - btnOK.setMnemonic(KeyEvent.VK_ENTER); - btnOK.setActionCommand("OK"); - getRootPane().setDefaultButton(btnOK); - - JButton btnCancel = new JButton("取消"); - buttonPane.add(btnCancel); - btnCancel.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - doClose(); - } - }); - btnCancel.setMnemonic(KeyEvent.VK_CANCEL); - btnCancel.setActionCommand("Cancel"); - - this.configuration = configuration; - this.tableModel = tableModel; - - setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - centerScreen(); - loadConfiguration(); - - engineBuilder = new EngineBuilder(classPath); - } +public class GenerationDialog extends JDialog { - public void centerScreen() { - Dimension dim = getToolkit().getScreenSize(); - Rectangle abounds = getBounds(); - setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2); + private static final Logger LOGGER = Logger.getLogger(GenerationDialog.class); + + private static final long serialVersionUID = 6159091936841897188L; + + private final JPanel contentPanel = new JPanel(); + private JTextField textTargetProject; + private JTextField textBasePackage; + private JTextField textModuleName; + private JTextField textAuthor; + private CheckBoxList templatesList; + + private Configuration configuration; + private Table tableModel; + private JTextField textTableName; + private JTextField textTableAlias; + private EngineBuilder engineBuilder; + + /** + * Create the dialog. + */ + public GenerationDialog(Configuration configuration, Table tableModel, String classPath) { + + setModal(true); + setTitle("生成代码"); + setResizable(false); + setBounds(100, 100, 419, 485); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(null); + + JLabel lblTargetProject = new JLabel("代码保存路径"); + lblTargetProject.setHorizontalAlignment(SwingConstants.TRAILING); + lblTargetProject.setBounds(10, 13, 72, 15); + contentPanel.add(lblTargetProject); + + textTargetProject = new JTextField(); + textTargetProject.setBounds(92, 10, 311, 21); + contentPanel.add(textTargetProject); + textTargetProject.setColumns(10); + + textBasePackage = new JTextField(); + textBasePackage.setBounds(92, 41, 311, 21); + contentPanel.add(textBasePackage); + textBasePackage.setColumns(10); + + textModuleName = new JTextField(); + textModuleName.setBounds(92, 72, 130, 21); + contentPanel.add(textModuleName); + textModuleName.setColumns(10); + + JLabel lblBasePackage = new JLabel("基准包"); + lblBasePackage.setHorizontalAlignment(SwingConstants.TRAILING); + lblBasePackage.setBounds(28, 44, 54, 15); + contentPanel.add(lblBasePackage); + + JLabel lblModuleName = new JLabel("模块名"); + lblModuleName.setHorizontalAlignment(SwingConstants.TRAILING); + lblModuleName.setBounds(28, 75, 54, 15); + contentPanel.add(lblModuleName); + + JLabel lblAuthor = new JLabel("开发者"); + lblAuthor.setBounds(232, 75, 54, 15); + contentPanel.add(lblAuthor); + + textAuthor = new JTextField(); + textAuthor.setBounds(296, 72, 80, 21); + contentPanel.add(textAuthor); + textAuthor.setColumns(10); + + JPanel panel = new JPanel(); + panel.setBounds(92, 103, 311, 234); + contentPanel.add(panel); + panel.setLayout(new BorderLayout(0, 0)); + + JScrollPane scrollPane = new JScrollPane(); + panel.add(scrollPane); + + templatesList = new CheckBoxList(); + templatesList.setBounds(0, 0, 1, 1); + scrollPane.setViewportView(templatesList); + + JLabel lblTemplates = new JLabel("代码模板"); + lblTemplates.setHorizontalAlignment(SwingConstants.TRAILING); + lblTemplates.setBounds(28, 103, 54, 15); + contentPanel.add(lblTemplates); + + textTableName = new JTextField(); + textTableName.setEditable(false); + textTableName.setBounds(92, 347, 130, 21); + contentPanel.add(textTableName); + textTableName.setColumns(10); + + textTableAlias = new JTextField(); + textTableAlias.setBounds(92, 378, 130, 21); + contentPanel.add(textTableAlias); + textTableAlias.setColumns(10); + + JLabel lblTableName = new JLabel("表名"); + lblTableName.setHorizontalAlignment(SwingConstants.TRAILING); + lblTableName.setBounds(28, 350, 54, 15); + contentPanel.add(lblTableName); + + JLabel lblTableAlias = new JLabel("别名"); + lblTableAlias.setHorizontalAlignment(SwingConstants.TRAILING); + lblTableAlias.setBounds(28, 381, 54, 15); + contentPanel.add(lblTableAlias); + JPanel buttonPane = new JPanel(); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5)); + + JButton btnOK = new JButton("生成"); + buttonPane.add(btnOK); + btnOK.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + okButtonClick(); + } + }); + btnOK.setMnemonic(KeyEvent.VK_ENTER); + btnOK.setActionCommand("OK"); + getRootPane().setDefaultButton(btnOK); + + JButton btnCancel = new JButton("取消"); + buttonPane.add(btnCancel); + btnCancel.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + doClose(); + } + }); + btnCancel.setMnemonic(KeyEvent.VK_CANCEL); + btnCancel.setActionCommand("Cancel"); + + this.configuration = configuration; + this.tableModel = tableModel; + + setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + centerScreen(); + loadConfiguration(); + + engineBuilder = new EngineBuilder(classPath); + } + + public void centerScreen() { + Dimension dim = getToolkit().getScreenSize(); + Rectangle abounds = getBounds(); + setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2); + } + + private void loadConfiguration() { + textTargetProject.setText(configuration.getTargetProject()); + textBasePackage.setText(configuration.getBasePackage()); + textModuleName.setText(configuration.getModuleName()); + textAuthor.setText(configuration.getAuthor()); + + templatesList.setListData(configuration.getTemplates().toArray()); + + templatesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); + for (int i = 0; i < configuration.getTemplates().size(); i++) { + TemplateElement templateElement = configuration.getTemplates().get(i); + if (templateElement.isSelected()) { + templatesList.addSelectionInterval(i, i); + } } - private void loadConfiguration() { - textTargetProject.setText(configuration.getTargetProject()); - textBasePackage.setText(configuration.getBasePackage()); - textModuleName.setText(configuration.getModuleName()); + textTableName.setText(tableModel.getTableName()); + textTableAlias.setText(tableModel.getTableAlias()); + } - templatesList.setListData(configuration.getTemplates().toArray()); + private void doClose() { + setVisible(false); + dispose(); + } - templatesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); - for (int i = 0; i < configuration.getTemplates().size(); i++) { - TemplateElement templateElement = configuration.getTemplates().get(i); - if (templateElement.isSelected()) { - templatesList.addSelectionInterval(i, i); - } - } - - textTableName.setText(tableModel.getTableName()); - textTableAlias.setText(tableModel.getTableAlias()); + private void okButtonClick() { + Object[] selectedValues = templatesList.getSelectedValues(); + if (selectedValues.length == 0) { + JOptionPane.showMessageDialog(this, "请选择模板.", "提示", JOptionPane.INFORMATION_MESSAGE); + return; } - - private void doClose() { - setVisible(false); - dispose(); + String targetProject = textTargetProject.getText(); + if (StringUtil.isEmpty(targetProject)) { + JOptionPane.showMessageDialog(this, "请输入代码保存路径.", "提示", JOptionPane.INFORMATION_MESSAGE); + return; + } + String basePackage = textBasePackage.getText(); + if (StringUtil.isEmpty(basePackage)) { + JOptionPane.showMessageDialog(this, "请输入基准包.", "提示", JOptionPane.INFORMATION_MESSAGE); + return; + } + String moduleName = textModuleName.getText(); + if (StringUtil.isEmpty(moduleName)) { + JOptionPane.showMessageDialog(this, "请输入模块名.", "提示", JOptionPane.INFORMATION_MESSAGE); + return; + } + String tableAlias = textTableAlias.getText(); + if (StringUtil.isEmpty(moduleName)) { + JOptionPane.showMessageDialog(this, "请输入表别名.", "提示", JOptionPane.INFORMATION_MESSAGE); + return; } - private void okButtonClick() { - Object[] selectedValues = templatesList.getSelectedValues(); - if (selectedValues.length==0) { - JOptionPane.showMessageDialog(this, "请选择模板.", "提示", JOptionPane.INFORMATION_MESSAGE); - return; - } - String targetProject = textTargetProject.getText(); - if (StringUtil.isEmpty(targetProject)) { - JOptionPane.showMessageDialog(this, "请输入代码保存路径.", "提示", JOptionPane.INFORMATION_MESSAGE); - return; - } - String basePackage = textBasePackage.getText(); - if (StringUtil.isEmpty(basePackage)) { - JOptionPane.showMessageDialog(this, "请输入基准包.", "提示", JOptionPane.INFORMATION_MESSAGE); - return; - } - String moduleName = textModuleName.getText(); - if (StringUtil.isEmpty(moduleName)) { - JOptionPane.showMessageDialog(this, "请输入模块名.", "提示", JOptionPane.INFORMATION_MESSAGE); - return; - } - String tableAlias = textTableAlias.getText(); - if (StringUtil.isEmpty(moduleName)) { - JOptionPane.showMessageDialog(this, "请输入表别名.", "提示", JOptionPane.INFORMATION_MESSAGE); - return; - } - - processSelectedTemplates(selectedValues, targetProject, basePackage, moduleName, tableAlias); + String author = textAuthor.getText(); + if (StringUtil.isEmpty(author)) { + JOptionPane.showMessageDialog(this, "请输入开发者.", "提示", JOptionPane.INFORMATION_MESSAGE); + return; } + processSelectedTemplates(selectedValues, targetProject, basePackage, moduleName, tableAlias, author); + } - private void processSelectedTemplates(Object[] selectedTemplateElements, String targetProject, String basePackage, String moduleName, String tableAlias){ - configuration.setTargetProject(targetProject); - configuration.setBasePackage(basePackage); - configuration.setModuleName(moduleName); + private void processSelectedTemplates(Object[] selectedTemplateElements, String targetProject, String basePackage, String moduleName, String tableAlias, String author) { + configuration.setTargetProject(targetProject); + configuration.setBasePackage(basePackage); + configuration.setModuleName(moduleName); + configuration.setAuthor(author); - tableModel.setTableAlias(tableAlias); + tableModel.setTableAlias(tableAlias); - Map model = new HashMap(); - model.put("targetProject", configuration.getTargetProject()); - model.put("basePackage", configuration.getBasePackage()); - model.put("moduleName", configuration.getModuleName()); - model.put("table", tableModel); + Map model = new HashMap(); + model.put("targetProject", configuration.getTargetProject()); + model.put("basePackage", configuration.getBasePackage()); + model.put("moduleName", configuration.getModuleName()); + model.put("author", configuration.getAuthor()); + model.put("table", tableModel); - Map selectedTemplateMap = new HashMap(); + Map selectedTemplateMap = new HashMap(); - for (TemplateElement templateElement : configuration.getTemplates()) { - templateElement.setSelected(false); - selectedTemplateMap.put(templateElement.getTemplateId(), false); - } - for (int i=0;i0) { - TemplateEngine templateEngine = engineBuilder.getTemplateEngine(templateElement.getEngine()); - if (templateEngine == null) { - JOptionPane.showMessageDialog(this, "没有对应的模板引擎: " + templateElement.getEngine(), "错误", JOptionPane.ERROR_MESSAGE); - } else { - templateEngine.processToFile(model, templateElement); - } - } - } catch (Exception e) { - LOGGER.info(e.getMessage(), e); - JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); - } + for (TemplateElement templateElement : configuration.getTemplates()) { + templateElement.setSelected(false); + selectedTemplateMap.put(templateElement.getTemplateId(), false); + } + for (int i = 0; i < selectedTemplateElements.length; i++) { + TemplateElement templateElement = (TemplateElement) selectedTemplateElements[i]; + selectedTemplateMap.put(templateElement.getTemplateId(), true); + } + model.put("template", selectedTemplateMap); + + for (int i = 0; i < selectedTemplateElements.length; i++) { + try { + TemplateElement templateElement = (TemplateElement) selectedTemplateElements[i]; + templateElement.setSelected(true); + + if (templateElement.getTemplateFile().trim().length() > 0) { + TemplateEngine templateEngine = engineBuilder.getTemplateEngine(templateElement.getEngine()); + if (templateEngine == null) { + JOptionPane.showMessageDialog(this, "没有对应的模板引擎: " + templateElement.getEngine(), "错误", JOptionPane.ERROR_MESSAGE); + } else { + templateEngine.processToFile(model, templateElement); + } } - configuration.save(); - JOptionPane.showMessageDialog(this, "生成完毕.", "提示", JOptionPane.INFORMATION_MESSAGE); + } catch (Exception e) { + LOGGER.info(e.getMessage(), e); + JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); + } } + configuration.save(); + JOptionPane.showMessageDialog(this, "生成完毕.", "提示", JOptionPane.INFORMATION_MESSAGE); + } } diff --git a/src/org/joy/generator/Generator.java b/src/org/joy/generator/Generator.java index b9f5ba0e05a6d3cb38a38536f7498101ff9e11ba..6a80e7eea206f4f1527c40590c14663813860733 100644 --- a/src/org/joy/generator/Generator.java +++ b/src/org/joy/generator/Generator.java @@ -15,58 +15,6 @@ */ package org.joy.generator; -import java.awt.BorderLayout; -import java.awt.Component; -import java.awt.Dimension; -import java.awt.EventQueue; -import java.awt.Font; -import java.awt.Rectangle; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.File; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Enumeration; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import javax.swing.ImageIcon; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JPopupMenu; -import javax.swing.JScrollPane; -import javax.swing.JSplitPane; -import javax.swing.JTable; -import javax.swing.JTextField; -import javax.swing.JTree; -import javax.swing.UIManager; -import javax.swing.border.EmptyBorder; -import javax.swing.event.CaretEvent; -import javax.swing.event.CaretListener; -import javax.swing.event.TableModelEvent; -import javax.swing.event.TableModelListener; -import javax.swing.plaf.FontUIResource; -import javax.swing.table.DefaultTableModel; -import javax.swing.table.JTableHeader; -import javax.swing.table.TableColumn; -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.DefaultTreeCellRenderer; -import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreePath; - import org.apache.log4j.Logger; import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper; import org.joy.config.Configuration; @@ -86,6 +34,38 @@ import org.joy.util.ClassloaderUtility; import org.joy.util.ObjectFactory; import org.joy.util.StringUtil; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; +import javax.swing.plaf.FontUIResource; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableColumn; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreePath; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.File; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Enumeration; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + public class Generator extends JFrame { private static final Logger LOGGER = Logger.getLogger(Generator.class); @@ -152,7 +132,7 @@ public class Generator extends JFrame { } }); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setBounds(100, 100, 886, 566); + setBounds(100, 100, 1080, 566); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); @@ -364,7 +344,11 @@ public class Generator extends JFrame { } } - private void loadTableTree(String schema) { + /** + * 载入表和视图 + * @param schema 模式名称 + */ + private void loadTableTree(String schema) { String schemaPattern = null; try { tablesNode.removeAllChildren(); @@ -520,12 +504,16 @@ public class Generator extends JFrame { fitTableColumns(tableGrid, headerWidth); } - private void loadDatabaseTable() { + /** + * 获取表视图信息 + */ + private void loadDatabaseTable() { TreePath selPath = tablesTree.getSelectionPath(); if (selPath == null || selPath.getPath() == null) { return; } if (selPath.getPath().length == 3) { + // 获取表的详细信息 DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) selPath.getPath()[2]; String nodeText = ((TreeNodeData) selNode.getUserObject()).getText(); String tableSchema = null; @@ -538,6 +526,11 @@ public class Generator extends JFrame { } } + /** + * 获取表的详情 + * @param tableSchema 表的schema名 + * @param tableName 表名 + */ private void loadTableGridData(String tableSchema, String tableName) { try { Database db = DatabaseFactory.createDatabase(connection, typeMapping); diff --git "a/\347\224\237\346\210\220jar\345\214\205.bat" "b/\347\224\237\346\210\220jar\345\214\205.bat" new file mode 100644 index 0000000000000000000000000000000000000000..66eed6cd150d6017a2ce00f50562cae9c67599c9 --- /dev/null +++ "b/\347\224\237\346\210\220jar\345\214\205.bat" @@ -0,0 +1 @@ +jar cvfm ./bin_new.jar ./bin/META-INF/MANIFEST.MF -C bin ./ \ No newline at end of file