diff --git a/README.md b/README.md index f4e7d7d4..a278b3ff 100644 --- a/README.md +++ b/README.md @@ -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配置,每次代码生成轻松搞定 diff --git a/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java b/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java index 5522fd7b..0dbade9c 100644 --- a/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java +++ b/src/main/java/com/zzg/mybatis/generator/bridge/MybatisGeneratorBridge.java @@ -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; @@ -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 diff --git a/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java b/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java index cc84af1d..2cd1b9e6 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/BaseFXController.java @@ -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); diff --git a/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java b/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java index 05ebbb27..cdd5f38a 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/DbConnectionController.java @@ -36,6 +36,7 @@ public class DbConnectionController extends BaseFXController { private ChoiceBox dbTypeChoice; private MainUIController mainUIController; private boolean isUpdate = false; + private Integer primayKey; @Override @@ -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) { @@ -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()); 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 5b990150..53f04bdf 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java @@ -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); @@ -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()); @@ -188,9 +186,8 @@ public void initialize(URL location, ResourceBundle resources) { void loadLeftDBTree() { TreeItem rootTreeItem = leftDBTree.getRoot(); rootTreeItem.getChildren().clear(); - List dbConfigs = null; try { - dbConfigs = ConfigHelper.loadDatabaseConfig(); + List dbConfigs = ConfigHelper.loadDatabaseConfig(); for (DatabaseConfig dbConfig : dbConfigs) { TreeItem treeItem = new TreeItem<>(); treeItem.setValue(dbConfig.getName()); @@ -265,12 +262,16 @@ private String validateConfig() { @FXML public void saveGeneratorConfig() { - TextInputDialog dialog = new TextInputDialog("保存配置"); + TextInputDialog dialog = new TextInputDialog(""); dialog.setTitle("保存当前配置"); dialog.setContentText("请输入配置名称"); Optional 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(); diff --git a/src/main/java/com/zzg/mybatis/generator/controller/SelectTableColumnController.java b/src/main/java/com/zzg/mybatis/generator/controller/SelectTableColumnController.java index cb2918bf..d65e9960 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/SelectTableColumnController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/SelectTableColumnController.java @@ -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; diff --git a/src/main/java/com/zzg/mybatis/generator/model/DatabaseConfig.java b/src/main/java/com/zzg/mybatis/generator/model/DatabaseConfig.java index 6accf1ba..e0fc7f94 100644 --- a/src/main/java/com/zzg/mybatis/generator/model/DatabaseConfig.java +++ b/src/main/java/com/zzg/mybatis/generator/model/DatabaseConfig.java @@ -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 @@ -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; } 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 5d28c3d2..9656327b 100644 --- a/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java +++ b/src/main/java/com/zzg/mybatis/generator/plugins/DbRemarksCommentGenerator.java @@ -87,7 +87,6 @@ public void addModelClassComment(TopLevelClass topLevelClass, topLevelClass.addJavaDocLine(" * @author "); topLevelClass.addJavaDocLine(" */"); if(isAnnotations) { - topLevelClass.addAnnotation("@Talbe(name=\"" + introspectedTable.getFullyQualifiedTableNameAtRuntime() + "\")"); } } diff --git a/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java b/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java index 1bfb1c90..a428b396 100644 --- a/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java +++ b/src/main/java/com/zzg/mybatis/generator/plugins/MySQLLimitPlugin.java @@ -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 diff --git a/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java b/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java index f6f912db..7aa5e802 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java +++ b/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java @@ -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; @@ -68,10 +68,10 @@ public static List loadDatabaseConfig() throws Exception { rs = stat.executeQuery("SELECT * FROM dbs"); List 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); } @@ -83,7 +83,7 @@ public static List 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; @@ -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 { @@ -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(); diff --git a/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java b/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java index 244d2c53..a23ecec8 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java +++ b/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java @@ -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. 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 0a2475cd..b1254a3b 100644 --- a/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java +++ b/src/main/java/com/zzg/mybatis/generator/util/DbUtil.java @@ -3,8 +3,6 @@ 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; @@ -12,8 +10,6 @@ import java.sql.*; import java.util.*; -import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; - /** * Created by Owen on 6/12/16. */ diff --git a/src/main/java/com/zzg/mybatis/generator/view/LeftDbTreeCell.java b/src/main/java/com/zzg/mybatis/generator/view/LeftDbTreeCell.java index 664b52b2..cf421b95 100644 --- a/src/main/java/com/zzg/mybatis/generator/view/LeftDbTreeCell.java +++ b/src/main/java/com/zzg/mybatis/generator/view/LeftDbTreeCell.java @@ -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; diff --git a/src/main/resources/fxml/MainUI.fxml b/src/main/resources/fxml/MainUI.fxml index b38154ec..35e47075 100644 --- a/src/main/resources/fxml/MainUI.fxml +++ b/src/main/resources/fxml/MainUI.fxml @@ -1,15 +1,11 @@ - - - + - - - - + + diff --git a/src/main/resources/fxml/generatorConfigs.fxml b/src/main/resources/fxml/generatorConfigs.fxml index e82aa126..a474d30f 100644 --- a/src/main/resources/fxml/generatorConfigs.fxml +++ b/src/main/resources/fxml/generatorConfigs.fxml @@ -1,9 +1,8 @@ - - + + - diff --git a/src/main/resources/fxml/newConnection.fxml b/src/main/resources/fxml/newConnection.fxml index 1d43f509..fd2cc526 100644 --- a/src/main/resources/fxml/newConnection.fxml +++ b/src/main/resources/fxml/newConnection.fxml @@ -1,13 +1,9 @@ - + - - - - - + diff --git a/src/main/resources/fxml/selectTableColumn.fxml b/src/main/resources/fxml/selectTableColumn.fxml index 3c0465e9..18cf955f 100644 --- a/src/main/resources/fxml/selectTableColumn.fxml +++ b/src/main/resources/fxml/selectTableColumn.fxml @@ -1,10 +1,8 @@ - - - + diff --git a/src/main/resources/sqlite3.db b/src/main/resources/sqlite3.db index 2be34ed4..fc4dd6db 100644 Binary files a/src/main/resources/sqlite3.db and b/src/main/resources/sqlite3.db differ