|
|
@@ -1,7 +1,10 @@
|
|
|
package com.izouma.codegenerator;
|
|
|
|
|
|
+import com.izouma.awesomeadmin.model.DataSourceInfo;
|
|
|
import com.izouma.awesomeadmin.model.GenCode;
|
|
|
import com.izouma.awesomeadmin.model.TableField;
|
|
|
+import com.izouma.awesomeadmin.util.Const;
|
|
|
+import com.izouma.awesomeadmin.util.JDBC;
|
|
|
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
@@ -43,37 +46,47 @@ public class TableGenerator {
|
|
|
GenTable(genCode);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成表,数据库建表
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
public static void GenTable(GenCode model) throws Exception {
|
|
|
Connection conn = null;
|
|
|
try {
|
|
|
|
|
|
- if ("Mysql".equals(model.getDataBaseType())) {
|
|
|
- Class.forName("com.mysql.jdbc.Driver");
|
|
|
- System.out.println("成功加载MySQL驱动!");
|
|
|
+ String DRIVER = "com.mysql.jdbc.Driver";
|
|
|
|
|
|
- PropertiesFileLoader propertiesFileLoader = PropertiesFileLoader.getInstance();
|
|
|
- propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");
|
|
|
+ PropertiesFileLoader propertiesFileLoader = PropertiesFileLoader.getInstance();
|
|
|
|
|
|
- String url = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");
|
|
|
- String user = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.username");
|
|
|
- String password = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.password");
|
|
|
- conn = DriverManager.getConnection(url, user, password);
|
|
|
+ //基础数据库
|
|
|
+ String url = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");
|
|
|
+ String user = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.username");
|
|
|
+ String password = propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.password");
|
|
|
|
|
|
- System.out.println("成功连接到数据库!");
|
|
|
- } else if ("SqlServer".equals(model.getDataBaseType())) {
|
|
|
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
|
|
- System.out.println("成功加载SQLServer驱动!");
|
|
|
|
|
|
- PropertiesFileLoader propertiesFileLoader = PropertiesFileLoader.getInstance();
|
|
|
- propertiesFileLoader.getProperties("properties/jdbc.properties", "jdbc.url");
|
|
|
+ if (!"dataSource".equals(model.getDataSourceCode())) {//如果不是基础数据库
|
|
|
+
|
|
|
+ //获取数据源配置信息
|
|
|
+ DataSourceInfo dataSourceInfo = Const.DataSourceInfoMap.get(model.getDataSourceCode());
|
|
|
+
|
|
|
+ if ("Mysql".equals(dataSourceInfo.getDatabaseType())) {
|
|
|
+ url = "jdbc:mysql://" + dataSourceInfo.getUrl() + "/" + dataSourceInfo.getDatabaseName();
|
|
|
+ user = dataSourceInfo.getUsername();
|
|
|
+ password = dataSourceInfo.getPassword();
|
|
|
+ } else if ("SqlServer".equals(dataSourceInfo.getDatabaseType())) {
|
|
|
+ DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
|
|
+ url = "jdbc:sqlserver://" + dataSourceInfo.getUrl() + ";DatabaseName=" + dataSourceInfo.getDatabaseName();
|
|
|
+ user = dataSourceInfo.getUsername();
|
|
|
+ password = dataSourceInfo.getPassword();
|
|
|
+ }
|
|
|
|
|
|
- String url = propertiesFileLoader.getProperties("properties/jdbc.properties", "sqlserver.url");
|
|
|
- String user = propertiesFileLoader.getProperties("properties/jdbc.properties", "sqlserver.user");
|
|
|
- String password = propertiesFileLoader.getProperties("properties/jdbc.properties", "sqlserver.password");
|
|
|
- conn = DriverManager.getConnection(url, user, password);
|
|
|
}
|
|
|
|
|
|
|
|
|
+ conn = JDBC.connectDB(DRIVER, url, user, password);
|
|
|
+
|
|
|
Statement stmt = conn.createStatement();
|
|
|
|
|
|
String sql = "DROP TABLE IF EXISTS " + model.getTableName() + "; create table " + model.getTableName() + "(";
|