Skip to content

Commit

Permalink
1. 使用sqlite3实现配置管理
Browse files Browse the repository at this point in the history
  • Loading branch information
zouzg committed Aug 21, 2016
1 parent c7e99a8 commit 2056517
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 199 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.0</version>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/zzg/mybatis/generator/MainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -32,16 +33,15 @@ 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);
}
controller.setPrimaryStage(primaryStage);
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,30 @@

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;
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 {

Expand Down Expand Up @@ -172,7 +152,7 @@ void loadLeftDBTree() {
rootTreeItem.getChildren().clear();
List<DatabaseConfig> dbConfigs = null;
try {
dbConfigs = XMLConfigHelper.loadDatabaseConfig();
dbConfigs = ConfigHelper.loadDatabaseConfig();
for (DatabaseConfig dbConfig : dbConfigs) {
TreeItem<String> treeItem = new TreeItem<>();
treeItem.setValue(dbConfig.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
160 changes: 160 additions & 0 deletions src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java
Original file line number Diff line number Diff line change
@@ -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
* <p>
* 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<DatabaseConfig> 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<DatabaseConfig> 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();
}
}


}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 2056517

Please sign in to comment.