Skip to content

Commit

Permalink
generator配置可以保存了并且可以维护多个配置了
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Aug 21, 2016
1 parent 59f2e52 commit 577da50
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 34 deletions.
13 changes: 0 additions & 13 deletions src/main/java/com/zzg/mybatis/generator/MainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,7 @@ public void start(Stage primaryStage) throws Exception {
primaryStage.show();

MainUIController controller = fxmlLoader.getController();
GeneratorConfig config = ConfigHelper.loadGeneratorConfig();
if (config != null) {
controller.setGeneratorConfigIntoUI(config);
}
controller.setPrimaryStage(primaryStage);
primaryStage.setOnCloseRequest(event -> {
GeneratorConfig generatorConfig = controller.getGeneratorConfigFromUI();
try {
ConfigHelper.saveGeneratorConfig(generatorConfig);
} catch (Exception e) {
_LOG.error(e.getMessage(), e);
AlertUtil.showErrorAlert(e.getMessage());
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,10 @@ public void showDialogStage() {
}
}

public void closeDialogStage() {
if (dialogStage != null) {
dialogStage.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public enum FXMLPage {

NEW_CONNECTION("fxml/newConnection.fxml"),
SELECT_TABLE_COLUMN("fxml/selectTableColumn.fxml");
SELECT_TABLE_COLUMN("fxml/selectTableColumn.fxml"),
GENERATOR_CONFIG("fxml/generatorConfigs.fxml"),;

private String fxml;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.zzg.mybatis.generator.controller;

import com.zzg.mybatis.generator.model.GeneratorConfig;
import com.zzg.mybatis.generator.util.ConfigHelper;
import com.zzg.mybatis.generator.view.AlertUtil;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
* Created by Owen on 8/21/16.
*/
public class GeneratorConfigController extends BaseFXController {

private static final Logger _LOG = LoggerFactory.getLogger(GeneratorConfigController.class);

@FXML
private TableView<GeneratorConfig> configTable;
@FXML
private TableColumn nameColumn;
@FXML
private TableColumn opsColumn;

private MainUIController mainUIController;

private GeneratorConfigController controller;

@Override
public void initialize(URL location, ResourceBundle resources) {
controller = this;
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
// 自定义操作列
opsColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
opsColumn.setCellFactory(cell -> {
return new TableCell() {
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
Button btn1 = new Button("应用");
Button btn2 = new Button("删除");
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.getChildren().add(btn1);
hBox.getChildren().add(btn2);
btn1.setOnAction(event -> {
try {
// 应用配置
GeneratorConfig generatorConfig = ConfigHelper.loadGeneratorConfig(item.toString());
mainUIController.setGeneratorConfigIntoUI(generatorConfig);
controller.closeDialogStage();
} catch (Exception e) {
AlertUtil.showErrorAlert(e.getMessage());
}
});
btn2.setOnAction(event -> {
try {
// 删除配置
_LOG.debug("item: {}", item);
ConfigHelper.deleteGeneratorConfig(item.toString());
refreshTableView();
} catch (Exception e) {
AlertUtil.showErrorAlert(e.getMessage());
}
});
setGraphic(hBox);
}
}
};
});
refreshTableView();
}

public void refreshTableView() {
try {
List<GeneratorConfig> configs = ConfigHelper.loadGeneratorConfigs();
configTable.setItems(FXCollections.observableList(configs));
} catch (Exception e) {
AlertUtil.showErrorAlert(e.getMessage());
}
}

void setMainUIController(MainUIController mainUIController) {
this.mainUIController = mainUIController;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class MainUIController extends BaseFXController {
@FXML
private Label connectionLabel;
@FXML
private Label configsLabel;
@FXML
private TextField connectorPathField;
@FXML
private TextField modelTargetPackage;
Expand Down Expand Up @@ -83,6 +85,15 @@ public void initialize(URL location, ResourceBundle resources) {
controller.setMainUIController(this);
controller.showDialogStage();
});
ImageView configImage = new ImageView("icons/config-list.png");
configImage.setFitHeight(40);
configImage.setFitWidth(40);
configsLabel.setGraphic(configImage);
configsLabel.setOnMouseClicked(event -> {
GeneratorConfigController controller = (GeneratorConfigController) loadFXMLPage("Generator Config", FXMLPage.GENERATOR_CONFIG, false);
controller.setMainUIController(this);
controller.showDialogStage();
});

leftDBTree.setShowRoot(false);
leftDBTree.setRoot(new TreeItem<>());
Expand Down Expand Up @@ -213,6 +224,25 @@ public void generateCode() {
}
}

@FXML
public void saveGeneratorConfig() {
TextInputDialog dialog = new TextInputDialog("保存配置");
dialog.setTitle("保存当前配置");
dialog.setContentText("请输入配置名称");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String name = result.get();
_LOG.info("user choose name: {}", name);
try {
GeneratorConfig generatorConfig = getGeneratorConfigFromUI();
generatorConfig.setName(name);
ConfigHelper.saveGeneratorConfig(generatorConfig);
} catch (Exception e) {
AlertUtil.showErrorAlert("删除配置失败");
}
}
}

public GeneratorConfig getGeneratorConfigFromUI() {
GeneratorConfig generatorConfig = new GeneratorConfig();
generatorConfig.setConnectorJarPath(connectorPathField.getText());
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/zzg/mybatis/generator/model/GeneratorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
*/
public class GeneratorConfig {

/**
* 本配置的名称
*/
private String name;

private String connectorJarPath;

private String projectFolder;
Expand All @@ -32,6 +37,14 @@ public class GeneratorConfig {

private boolean comment;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTableName() {
return tableName;
}
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static void saveGeneratorConfig(GeneratorConfig generatorConfig) throws E
conn = ConnectionManager.getConnection();
stat = conn.createStatement();
String jsonStr = JSON.toJSONString(generatorConfig);
String sql = String.format("INSERT INTO generator_config values('%s', '%s')", "current", jsonStr);
String sql = String.format("INSERT INTO generator_config values('%s', '%s')", generatorConfig.getName(), jsonStr);
stat.executeUpdate(sql);
} finally {
if (rs != null) rs.close();
Expand All @@ -133,14 +133,14 @@ public static void saveGeneratorConfig(GeneratorConfig generatorConfig) throws E
}
}

public static GeneratorConfig loadGeneratorConfig() throws Exception {
public static GeneratorConfig loadGeneratorConfig(String name) throws Exception {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
stat = conn.createStatement();
String sql = String.format("SELECT * FROM generator_config where name='%s'", "current");
String sql = String.format("SELECT * FROM generator_config where name='%s'", name);
_LOG.info("sql: ", sql);
rs = stat.executeQuery(sql);
GeneratorConfig generatorConfig = null;
Expand All @@ -156,5 +156,43 @@ public static GeneratorConfig loadGeneratorConfig() throws Exception {
}
}

public static List<GeneratorConfig> loadGeneratorConfigs() throws Exception {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
stat = conn.createStatement();
String sql = String.format("SELECT * FROM generator_config");
_LOG.info("sql: ", sql);
rs = stat.executeQuery(sql);
List<GeneratorConfig> configs = new ArrayList<>();
while (rs.next()) {
String value = rs.getString("value");
configs.add(JSON.parseObject(value, GeneratorConfig.class));
}
return configs;
} finally {
if (rs != null) rs.close();
if (stat != null) stat.close();
if (conn != null) conn.close();
}
}

public static int deleteGeneratorConfig(String name) throws Exception {
Connection conn = null;
Statement stat = null;
try {
conn = ConnectionManager.getConnection();
stat = conn.createStatement();
String sql = String.format("DELETE FROM generator_config where name='%s'", name);
_LOG.info("sql: {}", sql);
return stat.executeUpdate(sql);
} finally {
if (stat != null) stat.close();
if (conn != null) conn.close();
}
}


}
40 changes: 23 additions & 17 deletions src/main/resources/fxml/MainUI.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
</MenuBar>
<ToolBar minHeight="70.0" prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Label fx:id="connectionLabel" contentDisplay="TOP" text="Connections">
<Label fx:id="connectionLabel" contentDisplay="TOP" text="数据库连接">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Label>
<Label fx:id="configsLabel" contentDisplay="TOP" text="生成器配置" />
</items>
</ToolBar>
</children>
Expand Down Expand Up @@ -76,36 +77,36 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Connector Jar" />
<Label text="数据库连接驱动" />
<TextField fx:id="connectorPathField" promptText="C:\path\mysql-connector-java-5.1.37.jar" GridPane.columnIndex="1" GridPane.columnSpan="2" />
<Button mnemonicParsing="false" onAction="#chooseConnectorFile" text="Choose" GridPane.columnIndex="3">
<Button mnemonicParsing="false" onAction="#chooseConnectorFile" text="选择" GridPane.columnIndex="3">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin></Button>
<Label text="Table Name" GridPane.rowIndex="1" />
<Label text="数据库表名" GridPane.rowIndex="1" />
<TextField fx:id="tableNameField" promptText="person" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="5.0" />
</GridPane.margin>
</TextField>
<Label text="Domain Object Name" GridPane.rowIndex="2" />
<Label text="Java Domain名" GridPane.rowIndex="2" />
<TextField fx:id="domainObjectNameField" promptText="Person" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="5.0" />
</GridPane.margin>
</TextField>
<Label text="Project Folder" GridPane.rowIndex="3" />
<Label text="项目目录" GridPane.rowIndex="3" />
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="3">
<children>
<TextField fx:id="projectFolderField" prefHeight="27.0" prefWidth="348.0" promptText="D:\workspace\example">
<HBox.margin>
<Insets right="5.0" />
</HBox.margin>
</TextField>
<Button mnemonicParsing="false" onAction="#chooseProjectFolder" text="Choose" />
<Button mnemonicParsing="false" onAction="#chooseProjectFolder" text="选择" />
</children>
</HBox>
<Label text="Model Package" GridPane.rowIndex="4" />
<Label text="Model包名" GridPane.rowIndex="4" />
<HBox alignment="CENTER_LEFT" prefHeight="27.0" prefWidth="261.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
<children>
<TextField fx:id="modelTargetPackage" prefHeight="27.0" prefWidth="248.0" promptText="com.example.model">
Expand All @@ -115,9 +116,9 @@
</TextField>
</children>
</HBox>
<Label text="Target Folder" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<Label text="目标目录" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<TextField fx:id="modelTargetProject" promptText="src/main/java" GridPane.columnIndex="3" GridPane.rowIndex="4" />
<Label text="DAO Package" GridPane.rowIndex="5" />
<Label text="DAO包名" GridPane.rowIndex="5" />
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="5">
<children>
<TextField fx:id="daoTargetPackage" prefHeight="27.0" prefWidth="248.0" promptText="com.example.mapper">
Expand All @@ -127,9 +128,9 @@
</TextField>
</children>
</HBox>
<Label text="Target Folder" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<Label text="目标目录" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<TextField fx:id="daoTargetProject" promptText="src/main/java" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Label text="Mapping XML Package" GridPane.rowIndex="6" />
<Label text="XML包名" GridPane.rowIndex="6" />
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
<children>
<TextField fx:id="mapperTargetPackage" prefHeight="27.0" prefWidth="248.0" promptText="com.example">
Expand All @@ -139,16 +140,21 @@
</TextField>
</children>
</HBox>
<Label text="Target Folder" GridPane.columnIndex="2" GridPane.rowIndex="6" />
<Label text="目标目录" GridPane.columnIndex="2" GridPane.rowIndex="6" />
<TextField fx:id="mappingTargetProject" promptText="src/main/resources" GridPane.columnIndex="3" GridPane.rowIndex="6" />
<HBox alignment="CENTER_LEFT" spacing="10.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="7">
<children>
<CheckBox fx:id="offsetLimitCheckBox" mnemonicParsing="false" selected="true" text="offset/limit" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<CheckBox fx:id="commentCheckBox" mnemonicParsing="false" selected="true" text="column remarks" />
<CheckBox fx:id="offsetLimitCheckBox" mnemonicParsing="false" selected="true" text="分页" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<CheckBox fx:id="commentCheckBox" mnemonicParsing="false" selected="true" text="数据库列注释作为Model属性注释" />
</children>
</HBox>
<Button mnemonicParsing="false" onAction="#openTableColumnCustomizationPage" text="选择" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="8">
<children>
<Button mnemonicParsing="false" onAction="#generateCode" text="代码生成" />
<Button mnemonicParsing="false" onAction="#saveGeneratorConfig" text="保存配置" />
</children>
</HBox>
<Button mnemonicParsing="false" onAction="#generateCode" text="Generate" GridPane.columnIndex="1" GridPane.rowIndex="8" />
<Button mnemonicParsing="false" onAction="#openTableColumnCustomizationPage" text="Customize" GridPane.columnIndex="2" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
Expand Down
Loading

0 comments on commit 577da50

Please sign in to comment.