-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
200 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/main/java/com/zzg/mybatis/generator/util/ConfigHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/zzg/mybatis/generator/util/ConnectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.