From 26c8a246b7f86d2ce1354eb9a7c0cc85a585d123 Mon Sep 17 00:00:00 2001 From: Owen Zou Date: Thu, 21 Jul 2016 06:14:24 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8:=20=E6=B3=A8=E9=87=8A=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E4=B8=8D=E7=94=9F=E6=88=90Model=20Field=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/zzg/mybatis/generator/MainUI.java | 3 +++ .../plugins/DbRemarksCommentGenerator.java | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) 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/plugins/DbRemarksCommentGenerator.java b/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java index eacb5026..aa64681e 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) { From fc393420e1858c1e59a477f227e01449a4c8aa7f Mon Sep 17 00:00:00 2001 From: Owen Zou Date: Sat, 23 Jul 2016 22:39:31 +0800 Subject: [PATCH 2/5] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0gbk=E5=92=8Cgb2312?= =?UTF-8?q?=E7=9A=84=E7=BC=96=E7=A0=81=E4=BB=A5=E8=A7=A3=E5=86=B3=E4=B9=B1?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E9=97=AE=E9=A2=98=202.=20=E5=8F=B3=E9=94=AE?= =?UTF-8?q?=E5=B7=A6=E4=BE=A7=E7=9A=84=E6=A0=91=E5=8F=AF=E4=BB=A5=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MainUIController.java | 59 ++++++++++++------- .../plugins/DbRemarksCommentGenerator.java | 6 +- .../zzg/mybatis/generator/util/DbUtil.java | 6 ++ .../generator/util/XMLConfigHelper.java | 15 +++++ src/main/resources/fxml/newConnection.fxml | 4 +- 5 files changed, 66 insertions(+), 24 deletions(-) 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..7abe6912 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,13 +109,26 @@ 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(); + DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.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); @@ -127,21 +140,24 @@ public void initialize(URL location, ResourceBundle resources) { ObservableList> children = cell.getTreeItem().getChildren(); children.clear(); for (String schema : schemas) { - TreeItem treeItem = new TreeItem<>(); + TreeItem newTreeItem = new TreeItem<>(); ImageView imageView = new ImageView("icons/database.png"); imageView.setFitHeight(16); imageView.setFitWidth(16); - treeItem.setGraphic(imageView); - treeItem.setValue(schema); - children.add(treeItem); + newTreeItem.setGraphic(imageView); + newTreeItem.setValue(schema); + children.add(newTreeItem); } } + } catch (CommunicationsException e) { + _LOG.error(e.getMessage(), e); + AlertUtil.showErrorAlert("Connection timeout"); } 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(); + DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getParent().getGraphic().getUserData(); String schema = treeCell.getTreeItem().getValue(); try { List tables = DbUtil.getTableNames(selectedConfig, schema); @@ -149,23 +165,26 @@ public void initialize(URL location, ResourceBundle resources) { 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 String tableName = treeCell.getTreeItem().getValue(); - selectedDatabaseConfig = (DatabaseConfig)item.getParent().getParent().getGraphic().getUserData(); - String schema = item.getParent().getValue(); + selectedDatabaseConfig = (DatabaseConfig) treeItem.getParent().getParent().getGraphic().getUserData(); + String schema = treeItem.getParent().getValue(); selectedDatabaseConfig.setSchema(schema); this.tableName = tableName; tableNameField.setText(tableName); 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 aa64681e..a58e8488 100644 --- a/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java +++ b/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java @@ -78,12 +78,12 @@ public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { String columnRemarks = introspectedColumn.getRemarks(); - if (!this.columnRemarks && StringUtils.isEmpty(columnRemarks)) { + if (!this.columnRemarks || StringUtils.isEmpty(columnRemarks)) { return; } field.addJavaDocLine("/**"); - field.addJavaDocLine(" * " + columnRemarks); - 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..0aafa6fd 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java +++ b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java @@ -13,15 +13,19 @@ */ public class DbUtil { + private static final int DB_CONNECTION_TIMEOUTS_SENCONDS = 1; + public static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException { DbType dbType = DbType.valueOf(config.getDbType()); Class.forName(dbType.getDriverClass()); + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); return DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword()); } public static List getSchemas(DatabaseConfig config) throws Exception { DbType dbType = DbType.valueOf(config.getDbType()); Class.forName(dbType.getDriverClass()); + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword()); DatabaseMetaData md = conn.getMetaData(); ResultSet rs = md.getCatalogs(); @@ -35,6 +39,7 @@ public static List getSchemas(DatabaseConfig config) throws Exception { public static List getTableNames(DatabaseConfig config, String schema) throws Exception { DbType dbType = DbType.valueOf(config.getDbType()); Class.forName(dbType.getDriverClass()); + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(config), config.getUsername(), config.getPassword()); conn.setSchema(schema); DatabaseMetaData md = conn.getMetaData(); @@ -49,6 +54,7 @@ public static List getTableNames(DatabaseConfig config, String schema) t public static List getTableColumns(DatabaseConfig dbConfig, String schema, String tableName) throws Exception { DbType dbType = DbType.valueOf(dbConfig.getDbType()); Class.forName(dbType.getDriverClass()); + DriverManager.setLoginTimeout(DB_CONNECTION_TIMEOUTS_SENCONDS); Connection conn = DriverManager.getConnection(getConnectionUrlWithoutSchema(dbConfig), dbConfig.getUsername(), dbConfig.getPassword()); conn.setSchema(schema); DatabaseMetaData md = conn.getMetaData(); 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..40f07af8 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java +++ b/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java @@ -105,6 +105,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..a0337f01 100644 --- a/src/main/resources/fxml/newConnection.fxml +++ b/src/main/resources/fxml/newConnection.fxml @@ -52,7 +52,9 @@ - + + + From 333fb2aa458b529ba6478a1819598414c0ca2360 Mon Sep 17 00:00:00 2001 From: Owen Zou Date: Tue, 2 Aug 2016 06:19:13 +0800 Subject: [PATCH 3/5] improve words --- src/main/resources/fxml/selectTableColumn.fxml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/fxml/selectTableColumn.fxml b/src/main/resources/fxml/selectTableColumn.fxml index ff6c9ca8..a47039ab 100644 --- a/src/main/resources/fxml/selectTableColumn.fxml +++ b/src/main/resources/fxml/selectTableColumn.fxml @@ -22,7 +22,7 @@