Skip to content

Commit

Permalink
fix左侧数据库连接树不能更新配置名称的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Jun 17, 2017
1 parent f38cd8f commit 88f1696
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 76 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mybatis-generator-gui是基于[mybatis generator](http://www.mybatis.org/generat

![MainUI](https://cloud.githubusercontent.com/assets/3505708/22870467/834ed1c2-f1e2-11e6-9ae5-650b88722a7c.png)

### 注意事项
* 0.8.3版本请删除版本之前版本config目录下的sqlite3.db文件,重新`mvn install`安装

### 核心特性
* 按照界面步骤轻松生成代码,省去XML繁琐的学习与配置过程
* 保存数据库连接与Generator配置,每次代码生成轻松搞定
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.internal.ObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,8 +20,6 @@
import java.util.List;
import java.util.Set;

import static org.mybatis.generator.internal.util.ClassloaderUtility.getCustomClassloader;

/**
* The bridge between GUI and the mybatis generator. All the operation to mybatis generator should proceed through this
* class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.zzg.mybatis.generator.controller;

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.zzg.mybatis.generator.view.AlertUtil;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseFXController implements Initializable {
private static final Logger _LOG = LoggerFactory.getLogger(BaseFXController.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class DbConnectionController extends BaseFXController {
private ChoiceBox<String> dbTypeChoice;
private MainUIController mainUIController;
private boolean isUpdate = false;
private Integer primayKey;


@Override
Expand All @@ -49,7 +50,7 @@ void saveConnection() {
return;
}
try {
ConfigHelper.saveDatabaseConfig(this.isUpdate, config);
ConfigHelper.saveDatabaseConfig(this.isUpdate, primayKey, config);
getDialogStage().close();
mainUIController.loadLeftDBTree();
} catch (Exception e) {
Expand Down Expand Up @@ -110,6 +111,7 @@ private DatabaseConfig extractConfigForUI() {

public void setConfig(DatabaseConfig config) {
isUpdate = true;
primayKey = config.getId(); // save id for update config
nameField.setText(config.getName());
hostField.setText(config.getHost());
portField.setText(config.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import java.util.Optional;
import java.util.ResourceBundle;

import static org.junit.Assert.assertNotNull;

public class MainUIController extends BaseFXController {

private static final Logger _LOG = LoggerFactory.getLogger(MainUIController.class);
Expand Down Expand Up @@ -135,7 +133,7 @@ public void initialize(URL location, ResourceBundle resources) {
item3.setOnAction(event1 -> {
DatabaseConfig selectedConfig = (DatabaseConfig) treeItem.getGraphic().getUserData();
try {
ConfigHelper.deleteDatabaseConfig(selectedConfig.getName());
ConfigHelper.deleteDatabaseConfig(selectedConfig);
this.loadLeftDBTree();
} catch (Exception e) {
AlertUtil.showErrorAlert("Delete connection failed! Reason: " + e.getMessage());
Expand Down Expand Up @@ -188,9 +186,8 @@ public void initialize(URL location, ResourceBundle resources) {
void loadLeftDBTree() {
TreeItem rootTreeItem = leftDBTree.getRoot();
rootTreeItem.getChildren().clear();
List<DatabaseConfig> dbConfigs = null;
try {
dbConfigs = ConfigHelper.loadDatabaseConfig();
List<DatabaseConfig> dbConfigs = ConfigHelper.loadDatabaseConfig();
for (DatabaseConfig dbConfig : dbConfigs) {
TreeItem<String> treeItem = new TreeItem<>();
treeItem.setValue(dbConfig.getName());
Expand Down Expand Up @@ -265,12 +262,16 @@ private String validateConfig() {

@FXML
public void saveGeneratorConfig() {
TextInputDialog dialog = new TextInputDialog("保存配置");
TextInputDialog dialog = new TextInputDialog("");
dialog.setTitle("保存当前配置");
dialog.setContentText("请输入配置名称");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String name = result.get();
if (StringUtils.isEmpty(name)) {
AlertUtil.showErrorAlert("名称不能为空");
return;
}
_LOG.info("user choose name: {}", name);
try {
GeneratorConfig generatorConfig = getGeneratorConfigFromUI();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.zzg.mybatis.generator.controller;

import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.UITableColumnVO;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/zzg/mybatis/generator/model/DatabaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
public class DatabaseConfig {

/**
* The primary key in the sqlite db
*/
private Integer id;

private String dbType;
/**
* The name of the config
Expand All @@ -25,6 +30,14 @@ public class DatabaseConfig {

private String encoding;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public void addModelClassComment(TopLevelClass topLevelClass,
topLevelClass.addJavaDocLine(" * @author ");
topLevelClass.addJavaDocLine(" */");
if(isAnnotations) {

topLevelClass.addAnnotation("@Talbe(name=\"" + introspectedTable.getFullyQualifiedTableNameAtRuntime() + "\")");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
/**
* Created by zouzhigang on 2016/6/14.
*/
import java.util.List;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

public class MySQLLimitPlugin extends PluginAdapter {

@Override
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.GeneratorConfig;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URISyntaxException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
Expand Down Expand Up @@ -68,10 +68,10 @@ public static List<DatabaseConfig> loadDatabaseConfig() throws Exception {
rs = stat.executeQuery("SELECT * FROM dbs");
List<DatabaseConfig> configs = new ArrayList<>();
while (rs.next()) {
String name = rs.getString("name");
int id = rs.getInt("id");
String value = rs.getString("value");
DatabaseConfig databaseConfig = JSON.parseObject(value, DatabaseConfig.class);
databaseConfig.setName(name);
databaseConfig.setId(id);
configs.add(databaseConfig);
}

Expand All @@ -83,7 +83,7 @@ public static List<DatabaseConfig> loadDatabaseConfig() throws Exception {
}
}

public static void saveDatabaseConfig(boolean isUpdate, DatabaseConfig dbConfig) throws Exception {
public static void saveDatabaseConfig(boolean isUpdate, Integer primaryKey, DatabaseConfig dbConfig) throws Exception {
String configName = dbConfig.getName();
Connection conn = null;
Statement stat = null;
Expand All @@ -100,9 +100,9 @@ public static void saveDatabaseConfig(boolean isUpdate, DatabaseConfig dbConfig)
String jsonStr = JSON.toJSONString(dbConfig);
String sql;
if (isUpdate) {
sql = String.format("UPDATE dbs SET value = '%s' where name = '%s'", jsonStr, configName);
sql = String.format("UPDATE dbs SET name = '%s', value = '%s' where id = %d", configName, jsonStr, primaryKey);
} else {
sql = String.format("INSERT INTO dbs values('%s', '%s')", configName, jsonStr);
sql = String.format("INSERT INTO dbs (name, value) values('%s', '%s')", configName, jsonStr);
}
stat.executeUpdate(sql);
} finally {
Expand All @@ -112,14 +112,14 @@ public static void saveDatabaseConfig(boolean isUpdate, DatabaseConfig dbConfig)
}
}

public static void deleteDatabaseConfig(String name) throws Exception {
public static void deleteDatabaseConfig(DatabaseConfig databaseConfig) throws Exception {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
stat = conn.createStatement();
String sql = String.format("delete from dbs where name='%s'", name);
String sql = String.format("delete from dbs where id=%d", databaseConfig.getId());
stat.executeUpdate(sql);
} finally {
if (rs != null) rs.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package com.zzg.mybatis.generator.util;

import com.alibaba.fastjson.JSON;
import com.zzg.mybatis.generator.model.DatabaseConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Owen on 8/21/16.
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/zzg/mybatis/generator/util/DbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.UITableColumnVO;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.internal.util.ClassloaderUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.*;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

/**
* Created by Owen on 6/12/16.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.WeakInvalidationListener;
import javafx.scene.Node;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.HBox;
Expand Down
10 changes: 3 additions & 7 deletions src/main/resources/fxml/MainUI.fxml
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.collections.*?>
<?import com.zzg.mybatis.generator.model.*?>

<?import javafx.scene.text.Font?>
<?import java.lang.*?>
<BorderPane prefHeight="613.0" prefWidth="918.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.MainUIController">
<top>
<VBox>
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/fxml/generatorConfigs.fxml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.GeneratorConfigController">
<children>
<TableView fx:id="configTable" layoutX="47.0" layoutY="53.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
Expand Down
8 changes: 2 additions & 6 deletions src/main/resources/fxml/newConnection.fxml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.collections.*?>
<?import com.zzg.mybatis.generator.model.*?>

<?import java.lang.*?>
<AnchorPane prefHeight="389.0" prefWidth="569.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.DbConnectionController">
<children>
<GridPane alignment="CENTER_RIGHT" layoutX="10.0" layoutY="30.0" vgap="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/fxml/selectTableColumn.fxml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="526.0" prefWidth="730.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zzg.mybatis.generator.controller.SelectTableColumnController">
<children>
<TableView fx:id="columnListView" editable="true" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="100.0">
Expand Down
Binary file modified src/main/resources/sqlite3.db
Binary file not shown.

0 comments on commit 88f1696

Please sign in to comment.