diff --git a/pom.xml b/pom.xml index f8120945..da836397 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,13 @@ mysql-connector-java 5.1.38 + + com.oracle + ojdbc14 + 10.2.0.3.0 + system + ${project.basedir}/src/main/resources/lib/ojdbc14.jar + com.alibaba fastjson diff --git a/src/main/java/com/zzg/mybatis/generator/MainUI.java b/src/main/java/com/zzg/mybatis/generator/MainUI.java index 46e17182..8a3d7263 100644 --- a/src/main/java/com/zzg/mybatis/generator/MainUI.java +++ b/src/main/java/com/zzg/mybatis/generator/MainUI.java @@ -15,6 +15,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * 这是本软件的主入口,要运行本软件请直接运行本类就可以了,不用传入任何参数 + */ public class MainUI extends Application { private static final Logger _LOG = LoggerFactory.getLogger(MainUI.class); diff --git a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java index da9dd3f6..866ebbaf 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java @@ -2,8 +2,10 @@ import java.io.File; import java.net.URL; +import java.sql.SQLTimeoutException; import java.util.*; +import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; import com.zzg.mybatis.generator.bridge.MybatisGeneratorBridge; import com.zzg.mybatis.generator.model.*; import com.zzg.mybatis.generator.util.DbUtil; @@ -14,6 +16,7 @@ import com.zzg.mybatis.generator.view.UIProgressCallback; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.event.EventType; import javafx.scene.Node; @@ -21,6 +24,7 @@ import javafx.scene.control.cell.TextFieldTreeCell; import javafx.scene.input.MouseEvent; import javafx.scene.text.Text; +import javafx.stage.*; import javafx.util.Callback; import org.apache.commons.configuration2.HierarchicalConfiguration; import org.apache.commons.configuration2.XMLConfiguration; @@ -41,11 +45,7 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.ImageView; -import javafx.stage.DirectoryChooser; -import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; -import javafx.stage.Modality; -import javafx.stage.Stage; public class MainUIController extends BaseFXController { @@ -109,64 +109,52 @@ public void initialize(URL location, ResourceBundle resources) { leftDBTree.setCellFactory((TreeView tv) -> { TreeCell cell = defaultCellFactory.call(tv); cell.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { + int level = leftDBTree.getTreeItemLevel(cell.getTreeItem()); + TreeCell treeCell = (TreeCell) event.getSource(); + TreeItem treeItem = treeCell.getTreeItem(); + if (level == 1) { + final ContextMenu contextMenu = new ContextMenu(); + MenuItem item1 = new MenuItem("Close Connection"); + item1.setOnAction(event1 -> { + treeItem.getChildren().clear(); + }); + MenuItem item2 = new MenuItem("Delete"); + item2.setOnAction(event1 -> { + // TODO + }); + contextMenu.getItems().addAll(item1); + cell.setContextMenu(contextMenu); + } if (event.getClickCount() == 2) { - int level = leftDBTree.getTreeItemLevel(cell.getTreeItem()); - TreeCell treeCell = (TreeCell) event.getSource(); - TreeItem item = treeCell.getTreeItem(); - item.setExpanded(true); + treeItem.setExpanded(true); if (level == 1) { - DatabaseConfig selectedConfig = (DatabaseConfig) item.getGraphic().getUserData(); - // Accept clicks only on node cells, and not on empty spaces of the TreeView - leftDBTree.getSelectionModel().getSelectedItem().setExpanded(true); - System.out.println("Node click: " + selectedConfig); - List schemas = null; - try { - schemas = DbUtil.getSchemas(selectedConfig); - System.out.println(schemas); - if (schemas != null && schemas.size() > 0) { - ObservableList> children = cell.getTreeItem().getChildren(); - children.clear(); - for (String schema : schemas) { - TreeItem treeItem = new TreeItem<>(); - ImageView imageView = new ImageView("icons/database.png"); - imageView.setFitHeight(16); - imageView.setFitWidth(16); - treeItem.setGraphic(imageView); - treeItem.setValue(schema); - children.add(treeItem); - } - } - } catch (Exception e) { - AlertUtil.showErrorAlert(e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e)); - } - } else if (level == 2) { System.out.println("index: " + leftDBTree.getSelectionModel().getSelectedIndex()); - DatabaseConfig selectedConfig = (DatabaseConfig) item.getParent().getGraphic().getUserData(); - String schema = treeCell.getTreeItem().getValue(); + DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData(); try { - List tables = DbUtil.getTableNames(selectedConfig, schema); + List tables = DbUtil.getTableNames(selectedConfig); if (tables != null && tables.size() > 0) { ObservableList> children = cell.getTreeItem().getChildren(); children.clear(); for (String tableName : tables) { - TreeItem treeItem = new TreeItem<>(); + TreeItem newTreeItem = new TreeItem<>(); ImageView imageView = new ImageView("icons/table.png"); imageView.setFitHeight(16); imageView.setFitWidth(16); - treeItem.setGraphic(imageView); - treeItem.setValue(tableName); - children.add(treeItem); + newTreeItem.setGraphic(imageView); + newTreeItem.setValue(tableName); + children.add(newTreeItem); } } + } catch (CommunicationsException e) { + _LOG.error(e.getMessage(), e); + AlertUtil.showErrorAlert("Connection timeout"); } catch (Exception e) { _LOG.error(e.getMessage(), e); AlertUtil.showErrorAlert(e.getMessage()); } - } else if (level == 3) { // left DB tree level3 + } else if (level == 2) { // left DB tree level3 String tableName = treeCell.getTreeItem().getValue(); - selectedDatabaseConfig = (DatabaseConfig)item.getParent().getParent().getGraphic().getUserData(); - String schema = item.getParent().getValue(); - selectedDatabaseConfig.setSchema(schema); + selectedDatabaseConfig = (DatabaseConfig) treeItem.getParent().getGraphic().getUserData(); this.tableName = tableName; tableNameField.setText(tableName); domainObjectNameField.setText(StringUtils.dbStringToCamelStyle(tableName)); @@ -277,7 +265,7 @@ public void openTableColumnCustomizationPage() { try { // If select same schema and another table, update table data if (!tableName.equals(controller.getTableName())) { - List tableColumns = DbUtil.getTableColumns(selectedDatabaseConfig, selectedDatabaseConfig.getSchema(), tableName); + List tableColumns = DbUtil.getTableColumns(selectedDatabaseConfig, tableName); controller.setColumnList(FXCollections.observableList(tableColumns)); controller.setTableName(tableName); } diff --git a/src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java b/src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java index ad030243..8f9b1fed 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java @@ -40,6 +40,8 @@ public class NewConnectionController extends BaseFXController { private TextField passwordField; // @FXML // private CheckBox savePwdCheckBox; + @FXML + private TextField schemaField; @FXML private ChoiceBox encodingChoice; @FXML @@ -69,6 +71,7 @@ void saveConnection() { dbConfig.setPassword(password); // if (savePwdCheckBox.isSelected()) { // } + dbConfig.setSchema(schemaField.getText()); dbConfig.setEncoding(encoding); try { XMLConfigHelper.saveDatabaseConfig(name, dbConfig); @@ -90,13 +93,15 @@ void testConnection() { String encoding = encodingChoice.getValue(); String dbType = dbTypeChoice.getValue(); DatabaseConfig config = new DatabaseConfig(); + config.setName(name); config.setDbType(dbType); config.setHost(host); config.setPort(port); config.setUsername(userName); config.setPassword(password); + config.setSchema(schemaField.getText()); config.setEncoding(encoding); - String url = DbUtil.getConnectionUrlWithoutSchema(config); + String url = DbUtil.getConnectionUrlWithSchema(config); System.out.println(url); try { DbUtil.getConnection(config); diff --git a/src/main/java/com/zzg/mybatis/generator/model/DbType.java b/src/main/java/com/zzg/mybatis/generator/model/DbType.java index efbb1a6c..971e2ea8 100644 --- a/src/main/java/com/zzg/mybatis/generator/model/DbType.java +++ b/src/main/java/com/zzg/mybatis/generator/model/DbType.java @@ -4,17 +4,16 @@ * Created by Owen on 6/14/16. */ public enum DbType { - MYSQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s?useUnicode=true&useSSL=false&characterEncoding=%s", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s"), - ORACLE("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s", ""); + + MySQL("com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&useSSL=false&characterEncoding=%s"), + Oracle("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s"); private final String driverClass; private final String connectionUrlPattern; - private final String fullConnectionUrlPattern; - DbType(String driverClass, String connectionUrlPattern, String fullConnectionUrlPattern) { + DbType(String driverClass, String connectionUrlPattern) { this.driverClass = driverClass; this.connectionUrlPattern = connectionUrlPattern; - this.fullConnectionUrlPattern = fullConnectionUrlPattern; } public String getDriverClass() { @@ -25,7 +24,4 @@ public String getConnectionUrlPattern() { return connectionUrlPattern; } - public String getFullConnectionUrlPattern() { - return fullConnectionUrlPattern; - } } \ No newline at end of file diff --git a/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java b/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java index eacb5026..a58e8488 100644 --- a/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java +++ b/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java @@ -16,6 +16,7 @@ package com.zzg.mybatis.generator.plugins; +import org.apache.commons.lang3.StringUtils; import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; @@ -27,6 +28,8 @@ import static org.mybatis.generator.internal.util.StringUtility.isTrue; /** + * 此插件使用数据库表中列的注释来生成Java Model中属性的注释 + * * @author Owen Zou * */ @@ -59,7 +62,6 @@ public void addRootComment(XmlElement rootElement) { public void addConfigurationProperties(Properties properties) { this.properties.putAll(properties); - columnRemarks = isTrue(properties .getProperty("columnRemarks")); } @@ -75,12 +77,13 @@ public void addEnumComment(InnerEnum innerEnum, public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { - if (!columnRemarks) { + String columnRemarks = introspectedColumn.getRemarks(); + if (!this.columnRemarks || StringUtils.isEmpty(columnRemarks)) { return; } - field.addJavaDocLine("/**"); //$NON-NLS-1$ - field.addJavaDocLine(" * " + introspectedColumn.getRemarks()); - field.addJavaDocLine(" */"); //$NON-NLS-1$ + field.addJavaDocLine("/**"); + field.addJavaDocLine("* " + columnRemarks); + field.addJavaDocLine("*/"); } public void addFieldComment(Field field, IntrospectedTable introspectedTable) { diff --git a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java index 116389c6..21878129 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java +++ b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java @@ -3,6 +3,8 @@ import com.zzg.mybatis.generator.model.DatabaseConfig; import com.zzg.mybatis.generator.model.DbType; import com.zzg.mybatis.generator.model.UITableColumnVO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.sql.*; import java.util.ArrayList; @@ -13,32 +15,27 @@ */ public class DbUtil { - public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException { - DbType dbType = DbType.valueOf(config.getDbType()); - Class.forName(dbType.getDriverClass()); - return DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword()); - } + private static final Logger _LOG = LoggerFactory.getLogger(DbUtil.class); + private static final int DB_CONNECTION_TIMEOUTS_SENCONDS = 1; - public static List getSchemas(DatabaseConfig config) throws Exception { + public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException { DbType dbType = DbType.valueOf(config.getDbType()); Class.forName(dbType.getDriverClass()); - Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword()); - DatabaseMetaData md = conn.getMetaData(); - ResultSet rs = md.getCatalogs(); - List schemas = new ArrayList<>(); - while (rs.next()) { - schemas.add(rs.getString("TABLE_CAT")); - } - return schemas; + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); + String url = getConnectionUrlWithSchema(config); + _LOG.info("getConnection, connection url: {}", url); + return DriverManager.getConnection(url, config.getUsername(), config.getPassword()); } - public static List getTableNames(DatabaseConfig config, String schema) throws Exception { + public static List getTableNames(DatabaseConfig config) throws Exception { DbType dbType = DbType.valueOf(config.getDbType()); Class.forName(dbType.getDriverClass()); - Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword()); - conn.setSchema(schema); + String url = getConnectionUrlWithSchema(config); + _LOG.info("getTableNames, connection url: {}", url); + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); + Connection conn = DriverManager.getConnection(url, config.getUsername(), config.getPassword()); DatabaseMetaData md = conn.getMetaData(); - ResultSet rs = md.getTables(schema, null, null, null); + ResultSet rs = md.getTables(null, null, null, null); List tables = new ArrayList<>(); while (rs.next()) { tables.add(rs.getString(3)); @@ -46,13 +43,15 @@ public static List getTableNames(DatabaseConfig config, String schema) t return tables; } - public static List getTableColumns(DatabaseConfig dbConfig, String schema, String tableName) throws Exception { + public static List getTableColumns(DatabaseConfig dbConfig, String tableName) throws Exception { DbType dbType = DbType.valueOf(dbConfig.getDbType()); Class.forName(dbType.getDriverClass()); - Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(dbConfig), dbConfig.getUsername(), dbConfig.getPassword()); - conn.setSchema(schema); + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); + String url = getConnectionUrlWithSchema(dbConfig); + _LOG.info("getTableColumns, connection url: {}", url); + Connection conn = DriverManager.getConnection(url, dbConfig.getUsername(), dbConfig.getPassword()); DatabaseMetaData md = conn.getMetaData(); - ResultSet rs = md.getColumns(schema, null, tableName, null); + ResultSet rs = md.getColumns(null, null, tableName, null); List columns = new ArrayList<>(); while (rs.next()) { UITableColumnVO columnVO = new UITableColumnVO(); @@ -65,15 +64,10 @@ public static List getTableColumns(DatabaseConfig dbConfig, Str return columns; } - public static String getConnectionUrlWithoutSchema(DatabaseConfig dbConfig) { - DbType dbType = DbType.valueOf(dbConfig.getDbType()); - String connectionUrl = String.format(dbType.getConnectionUrlPattern(), dbConfig.getHost(), dbConfig.getPort(), dbConfig.getEncoding()); - return connectionUrl; - } - public static String getConnectionUrlWithSchema(DatabaseConfig dbConfig) { DbType dbType = DbType.valueOf(dbConfig.getDbType()); - String connectionUrl = String.format(dbType.getFullConnectionUrlPattern(), dbConfig.getHost(), dbConfig.getPort(), dbConfig.getSchema(), dbConfig.getEncoding()); + String connectionUrl = String.format(dbType.getConnectionUrlPattern(), dbConfig.getHost(), dbConfig.getPort(), dbConfig.getSchema(), dbConfig.getEncoding()); + _LOG.info("getConnectionUrlWithSchema, connection url: {}", connectionUrl); return connectionUrl; } diff --git a/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java b/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java index 72f6ba44..de6decfe 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java +++ b/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java @@ -72,6 +72,7 @@ public static List loadDatabaseConfig() { dbConfig.setUsername(hc.getString("userName")); dbConfig.setPassword(hc.getString("password")); dbConfig.setEncoding(hc.getString("encoding")); + dbConfig.setSchema(hc.getString("schema")); dbConfig.setDbType(hc.getString("dbType")); dbs.add(dbConfig); } @@ -95,6 +96,7 @@ public static void saveDatabaseConfig(String name, DatabaseConfig dbConfig) { config.addProperty(name + ".port", dbConfig.getPort()); config.addProperty(name + ".userName", dbConfig.getUsername()); config.addProperty(name + ".password", dbConfig.getPassword()); + config.addProperty(name + ".schema", dbConfig.getSchema()); config.addProperty(name + ".encoding", dbConfig.getEncoding()); // save configuration @@ -105,6 +107,21 @@ public static void saveDatabaseConfig(String name, DatabaseConfig dbConfig) { } } + public static void deleteDatabaseConfig(String name) { + Configurations configs = new Configurations(); + try { + // obtain the configuration + FileBasedConfigurationBuilder builder = configs.xmlBuilder(BASE_DIR + DB_CONFIG_FILE); + XMLConfiguration config = builder.getConfiguration(); + + // save configuration + builder.save(); + } catch (ConfigurationException cex) { + // Something went wrong + cex.printStackTrace(); + } + } + public static void saveGeneratorConfig(GeneratorConfig generatorConfig) throws Exception { Configurations configs = new Configurations(); // obtain the configuration diff --git a/src/main/resources/fxml/newConnection.fxml b/src/main/resources/fxml/newConnection.fxml index 15fcffc3..12bf2e3d 100644 --- a/src/main/resources/fxml/newConnection.fxml +++ b/src/main/resources/fxml/newConnection.fxml @@ -23,6 +23,7 @@ + -