diff --git a/pom.xml b/pom.xml index bc1fd606..2f8a2be2 100644 --- a/pom.xml +++ b/pom.xml @@ -28,8 +28,13 @@ org.apache.commons - commons-configuration2 - 2.0 + commons-lang3 + 3.4 + + + org.xerial + sqlite-jdbc + 3.8.11.2 commons-beanutils diff --git a/src/main/java/com/zzg/mybatis/generator/MainUI.java b/src/main/java/com/zzg/mybatis/generator/MainUI.java index 8a3d7263..7683bc6e 100644 --- a/src/main/java/com/zzg/mybatis/generator/MainUI.java +++ b/src/main/java/com/zzg/mybatis/generator/MainUI.java @@ -5,7 +5,7 @@ import com.zzg.mybatis.generator.controller.MainUIController; import com.zzg.mybatis.generator.model.GeneratorConfig; -import com.zzg.mybatis.generator.util.XMLConfigHelper; +import com.zzg.mybatis.generator.util.ConfigHelper; import com.zzg.mybatis.generator.view.AlertUtil; import javafx.application.Application; import javafx.fxml.FXMLLoader; @@ -24,6 +24,7 @@ public class MainUI extends Application { @Override public void start(Stage primaryStage) throws Exception { + ConfigHelper.createEmptyFiles(); URL url = Thread.currentThread().getContextClassLoader().getResource("fxml/MainUI.fxml"); FXMLLoader fxmlLoader = new FXMLLoader(url); Parent root = fxmlLoader.load(); @@ -32,8 +33,7 @@ public void start(Stage primaryStage) throws Exception { primaryStage.show(); MainUIController controller = fxmlLoader.getController(); - XMLConfigHelper.createEmptyFiles(); - GeneratorConfig config = XMLConfigHelper.loadGeneratorConfig(); + GeneratorConfig config = ConfigHelper.loadGeneratorConfig(); if (config != null) { controller.setGeneratorConfigIntoUI(config); } @@ -41,7 +41,7 @@ public void start(Stage primaryStage) throws Exception { primaryStage.setOnCloseRequest(event -> { GeneratorConfig generatorConfig = controller.getGeneratorConfigFromUI(); try { - XMLConfigHelper.saveGeneratorConfig(generatorConfig); + ConfigHelper.saveGeneratorConfig(generatorConfig); } catch (Exception e) { _LOG.error(e.getMessage(), e); AlertUtil.showErrorAlert(e.getMessage()); 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 c7201240..8e62b44a 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/MainUIController.java @@ -2,7 +2,6 @@ import java.io.File; import java.net.URL; -import java.sql.SQLTimeoutException; import java.util.*; import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException; @@ -10,42 +9,23 @@ import com.zzg.mybatis.generator.model.*; import com.zzg.mybatis.generator.util.DbUtil; import com.zzg.mybatis.generator.util.StringUtils; -import com.zzg.mybatis.generator.util.XMLConfigHelper; +import com.zzg.mybatis.generator.util.ConfigHelper; import com.zzg.mybatis.generator.view.AlertUtil; -import com.zzg.mybatis.generator.view.LeftDbTreeCell; 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; import javafx.scene.control.*; 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; -import org.apache.commons.configuration2.builder.fluent.Configurations; -import org.apache.commons.configuration2.tree.ImmutableNode; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.mybatis.generator.api.MyBatisGenerator; -import org.mybatis.generator.api.ProgressCallback; -import org.mybatis.generator.api.ShellCallback; -import org.mybatis.generator.api.VerboseProgressCallback; import org.mybatis.generator.config.*; -import org.mybatis.generator.internal.DefaultShellCallback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.scene.Scene; import javafx.scene.image.ImageView; -import javafx.stage.FileChooser.ExtensionFilter; public class MainUIController extends BaseFXController { @@ -172,7 +152,7 @@ void loadLeftDBTree() { rootTreeItem.getChildren().clear(); List dbConfigs = null; try { - dbConfigs = XMLConfigHelper.loadDatabaseConfig(); + dbConfigs = ConfigHelper.loadDatabaseConfig(); for (DatabaseConfig dbConfig : dbConfigs) { TreeItem treeItem = new TreeItem<>(); treeItem.setValue(dbConfig.getName()); 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 8f9b1fed..cb476ae3 100644 --- a/src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java +++ b/src/main/java/com/zzg/mybatis/generator/controller/NewConnectionController.java @@ -2,26 +2,16 @@ import com.zzg.mybatis.generator.model.DatabaseConfig; import com.zzg.mybatis.generator.util.DbUtil; -import com.zzg.mybatis.generator.util.XMLConfigHelper; +import com.zzg.mybatis.generator.util.ConfigHelper; import com.zzg.mybatis.generator.view.AlertUtil; import javafx.fxml.FXML; import javafx.scene.control.Alert; -import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceBox; import javafx.scene.control.TextField; -import org.apache.commons.configuration2.XMLConfiguration; -import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; -import org.apache.commons.configuration2.builder.fluent.Configurations; -import org.apache.commons.configuration2.ex.ConfigurationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URL; -import java.sql.Connection; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.Iterator; import java.util.ResourceBundle; public class NewConnectionController extends BaseFXController { @@ -74,7 +64,7 @@ void saveConnection() { dbConfig.setSchema(schemaField.getText()); dbConfig.setEncoding(encoding); try { - XMLConfigHelper.saveDatabaseConfig(name, dbConfig); + ConfigHelper.saveDatabaseConfig(name, dbConfig); getDialogStage().close(); mainUIController.loadLeftDBTree(); } catch (Exception e) { diff --git a/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java b/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java new file mode 100644 index 00000000..64dc94bf --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java @@ -0,0 +1,160 @@ +package com.zzg.mybatis.generator.util; + +import com.alibaba.fastjson.JSON; +import com.zzg.mybatis.generator.model.DatabaseConfig; +import com.zzg.mybatis.generator.model.GeneratorConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +/** + * XML based config file help class + *

+ * Created by Owen on 6/16/16. + */ +public class ConfigHelper { + + private static final Logger _LOG = LoggerFactory.getLogger(ConfigHelper.class); + private static final String BASE_DIR = "config"; + private static final String CONFIG_FILE = "/sqlite3.db"; + + public static void createEmptyFiles() throws Exception { + File file = new File(BASE_DIR); + if (!file.exists()) { + file.mkdir(); + } + File uiConfigFile = new File(BASE_DIR + CONFIG_FILE); + if (!uiConfigFile.exists()) { + createEmptyXMLFile(uiConfigFile); + } + } + + static void createEmptyXMLFile(File uiConfigFile) throws IOException { + InputStream fis = null; + FileOutputStream fos = null; + try { + fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("sqlite3.db"); + fos = new FileOutputStream(uiConfigFile); + byte[] buffer = new byte[1024]; + int byteread = 0; + while ((byteread = fis.read(buffer)) != -1) { + fos.write(buffer, 0, byteread); + } + } finally { + if (fis != null) fis.close(); + if (fos != null) fos.close(); + } + + } + + public static List loadDatabaseConfig() throws Exception { + Connection conn = null; + Statement stat = null; + ResultSet rs = null; + try { + conn = ConnectionManager.getConnection(); + stat = conn.createStatement(); + rs = stat.executeQuery("select * from dbs"); + List configs = new ArrayList<>(); + while (rs.next()) { + String name = rs.getString("name"); + String value = rs.getString("value"); + DatabaseConfig databaseConfig = JSON.parseObject(value, DatabaseConfig.class); + databaseConfig.setName(name); + configs.add(databaseConfig); + } + + return configs; + } finally { + if (rs != null) rs.close(); + if (stat != null) stat.close(); + if (conn != null) conn.close(); + } + } + + public static void saveDatabaseConfig(String name, DatabaseConfig dbConfig) throws Exception { + Connection conn = null; + Statement stat = null; + ResultSet rs = null; + try { + conn = ConnectionManager.getConnection(); + stat = conn.createStatement(); + ResultSet rs1 = stat.executeQuery("SELECT * from dbs where name = '" + name + "'"); + if (rs1.next()) { + throw new RuntimeException("配置已经存在, 请使用其它名字"); + } + String jsonStr = JSON.toJSONString(dbConfig); + String sql = String.format("INSERT INTO dbs values('%s', '%s')", name, jsonStr); + stat.executeUpdate(sql); + } finally { + if (rs != null) rs.close(); + if (stat != null) stat.close(); + if (conn != null) conn.close(); + } + } + + public static void deleteDatabaseConfig(String name) 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); + stat.executeUpdate(sql); + } finally { + if (rs != null) rs.close(); + if (stat != null) stat.close(); + if (conn != null) conn.close(); + } + } + + public static void saveGeneratorConfig(GeneratorConfig generatorConfig) throws Exception { + Connection conn = null; + Statement stat = null; + ResultSet rs = null; + try { + conn = ConnectionManager.getConnection(); + stat = conn.createStatement(); + String jsonStr = JSON.toJSONString(generatorConfig); + String sql = String.format("INSERT INTO generator_config values('%s', '%s')", "current", jsonStr); + stat.executeUpdate(sql); + } finally { + if (rs != null) rs.close(); + if (stat != null) stat.close(); + if (conn != null) conn.close(); + } + } + + public static GeneratorConfig loadGeneratorConfig() 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"); + _LOG.info("sql: ", sql); + rs = stat.executeQuery(sql); + GeneratorConfig generatorConfig = null; + if (rs.next()) { + String value = rs.getString("value"); + generatorConfig = JSON.parseObject(value, GeneratorConfig.class); + } + return generatorConfig; + } finally { + if (rs != null) rs.close(); + if (stat != null) stat.close(); + if (conn != null) conn.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 new file mode 100644 index 00000000..244d2c53 --- /dev/null +++ b/src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java @@ -0,0 +1,25 @@ +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. + */ +public class ConnectionManager { + + private static final String DB_URL = "jdbc:sqlite:./config/sqlite3.db"; + + public static Connection getConnection() throws Exception { + Class.forName("org.sqlite.JDBC"); + Connection conn = DriverManager.getConnection(DB_URL); + return conn; + } +} diff --git a/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java b/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java deleted file mode 100644 index de6decfe..00000000 --- a/src/main/java/com/zzg/mybatis/generator/util/XMLConfigHelper.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.zzg.mybatis.generator.util; - -import com.alibaba.fastjson.JSON; -import com.zzg.mybatis.generator.model.DatabaseConfig; -import com.zzg.mybatis.generator.model.GeneratorConfig; -import com.zzg.mybatis.generator.view.AlertUtil; -import org.apache.commons.configuration2.HierarchicalConfiguration; -import org.apache.commons.configuration2.XMLConfiguration; -import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; -import org.apache.commons.configuration2.builder.fluent.Configurations; -import org.apache.commons.configuration2.ex.ConfigurationException; -import org.apache.commons.configuration2.tree.ImmutableNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -/** - * XML based config file help class - *

- * Created by Owen on 6/16/16. - */ -public class XMLConfigHelper { - - private static final Logger _LOG = LoggerFactory.getLogger(XMLConfigHelper.class); - private static final String BASE_DIR = "config"; - private static final String CONFIG_FILE = "/config.xml"; - private static final String DB_CONFIG_FILE = "/dbConfig.xml"; - - public static void createEmptyFiles() throws Exception { - File file = new File(BASE_DIR); - if (!file.exists()) { - file.mkdir(); - } - File uiConfigFile = new File(BASE_DIR + CONFIG_FILE); - if (!uiConfigFile.exists()) { - createEmptyXMLFile(uiConfigFile); - } - File dbConfigFile = new File(BASE_DIR + DB_CONFIG_FILE); - if (!dbConfigFile.exists()) { - createEmptyXMLFile(dbConfigFile); - } - } - - static void createEmptyXMLFile(File uiConfigFile) throws IOException { - String content = ""; - uiConfigFile.createNewFile(); - FileOutputStream fos = new FileOutputStream(uiConfigFile); - fos.write(content.getBytes()); - if (fos != null) { - fos.close(); - } - } - - public static List loadDatabaseConfig() { - List dbs = new ArrayList<>(); - Configurations configs = new Configurations(); - try { - XMLConfiguration config = configs.xml(new File(BASE_DIR + DB_CONFIG_FILE)); - List> list = config.childConfigurationsAt(""); - System.out.println(list); - for (HierarchicalConfiguration hc : list) { - String name = hc.getRootElementName(); - DatabaseConfig dbConfig = new DatabaseConfig(); - dbConfig.setName(name); - dbConfig.setHost(hc.getString("host")); - dbConfig.setPort(hc.getString("port")); - 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); - } - } catch (Exception e) { - _LOG.error(e.getMessage(), e); - AlertUtil.showErrorAlert(e.getMessage()); - } - return dbs; - } - - public static void saveDatabaseConfig(String name, DatabaseConfig dbConfig) { - Configurations configs = new Configurations(); - try { - // obtain the configuration - FileBasedConfigurationBuilder builder = configs.xmlBuilder(BASE_DIR + DB_CONFIG_FILE); - XMLConfiguration config = builder.getConfiguration(); - - // update property - config.addProperty(name + ".dbType", dbConfig.getDbType()); - config.addProperty(name + ".host", dbConfig.getHost()); - 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 - builder.save(); - } catch (ConfigurationException cex) { - // Something went wrong - cex.printStackTrace(); - } - } - - 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 - FileBasedConfigurationBuilder builder = configs.xmlBuilder(BASE_DIR + CONFIG_FILE); - XMLConfiguration config = builder.getConfiguration(); - config.clear(); - // update property - config.addProperty("GeneratorConfig.Current", JSON.toJSON(generatorConfig)); - builder.save(); - } - - public static GeneratorConfig loadGeneratorConfig() throws Exception { - Configurations configs = new Configurations(); - XMLConfiguration config = configs.xml(new File(BASE_DIR + CONFIG_FILE)); - List> list = config.childConfigurationsAt("GeneratorConfig"); - if (list != null && list.size() > 0) { - HierarchicalConfiguration configuration = list.get(0); - String jsonContent = configuration.getString(""); - GeneratorConfig generatorConfig = JSON.parseObject(jsonContent, GeneratorConfig.class); - _LOG.debug("generatorConfig: {}", generatorConfig); - return generatorConfig; - } - return null; - } - - - - -} diff --git a/src/main/resources/config.xml b/src/main/resources/config.xml deleted file mode 100644 index bbb529c1..00000000 --- a/src/main/resources/config.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/src/main/resources/dbConfig.xml b/src/main/resources/dbConfig.xml deleted file mode 100644 index bbb529c1..00000000 --- a/src/main/resources/dbConfig.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/src/main/resources/sqlite3.db b/src/main/resources/sqlite3.db new file mode 100644 index 00000000..ce1531ec Binary files /dev/null and b/src/main/resources/sqlite3.db differ