新增sqlite支持
SQLiteManager使用json创建sqlite表
This commit is contained in:
parent
3fe153d2c4
commit
03e5f36795
5
pom.xml
5
pom.xml
@ -116,6 +116,11 @@
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.28.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
10
src/main/java/com/yutou/tools/ad/AdController.java
Normal file
10
src/main/java/com/yutou/tools/ad/AdController.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.yutou.tools.ad;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("ad/")
|
||||
public class AdController {
|
||||
|
||||
}
|
22
src/main/java/com/yutou/tools/ad/Datas/AppData.java
Normal file
22
src/main/java/com/yutou/tools/ad/Datas/AppData.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.yutou.tools.ad.Datas;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AppData {
|
||||
private String appName;
|
||||
private String packageName;
|
||||
private String appId;
|
||||
|
||||
private String adSrc;
|
||||
private String adAppId;
|
||||
private String adAppKey;
|
||||
private String adOpenKey;
|
||||
private String adInterKey;
|
||||
private String adBannerKey;
|
||||
private String adVideoKey;
|
||||
private String adNativeKey;
|
||||
}
|
||||
/*
|
||||
{"file":"ad.db","table":[{"name":"app","item":[{"name":"id","type":"int","isNull":false,"isKey":true},{"name":"appName","type":"String","isNull":false,"isKey":false},{"name":"packageName","type":"String","isNull":false,"isKey":false},{"name":"appId","type":"String","isNull":false,"isKey":false}]},{"name":"app_ad","item":[{"name":"id","type":"int","isNull":false,"isKey":true},{"name":"appId","type":"String","isNull":false,"isKey":false},{"name":"adSrc","type":"String","isNull":false,"isKey":false},{"name":"adAppId","type":"String","isNull":false,"isKey":false},{"name":"adAppKey","type":"String","isNull":false,"isKey":false},{"name":"adOpenKey","type":"String","isNull":true,"isKey":false},{"name":"adInterKey","type":"String","isNull":true,"isKey":false},{"name":"adBannerKey","type":"String","isNull":true,"isKey":false},{"name":"adVideoKey","type":"String","isNull":true,"isKey":false},{"name":"adNativeKey","type":"String","isNull":true,"isKey":false}]},{"name":"ad_log","table":[{"name":"id","type":"int","isNull":false,"isKey":true},{"name":"appId","type":"String","isNull":false,"isKey":false},{"name":"adSrc","type":"String","isNull":false,"isKey":false},{"name":"adType","type":"String","isNull":false,"isKey":false},{"name":"model","type":"String","isNull":false,"isKey":false},{"name":"subtime","type":"String","isNull":false,"isKey":false}]}]}
|
||||
*/
|
117
src/main/java/com/yutou/tools/sqlite/SQLiteManager.java
Normal file
117
src/main/java/com/yutou/tools/sqlite/SQLiteManager.java
Normal file
@ -0,0 +1,117 @@
|
||||
package com.yutou.tools.sqlite;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.tools.utils.ConfigTools;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class SQLiteManager {
|
||||
private static SQLiteManager manager;
|
||||
protected Connection conn;
|
||||
private String url = "jdbc:sqlite:";
|
||||
private File sql;
|
||||
|
||||
public static SQLiteManager getInstance() {
|
||||
if (manager == null) {
|
||||
manager = new SQLiteManager();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
|
||||
public void startBatch() {
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void closeBatch() {
|
||||
try {
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void commit() {
|
||||
try {
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected SQLiteManager() {
|
||||
|
||||
}
|
||||
|
||||
private void createSql(JSONObject json) {
|
||||
JSONObject table = json.getJSONObject("table");
|
||||
String tableName = table.getString("name");
|
||||
try {
|
||||
sql.mkdirs();
|
||||
sql.delete();
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
Statement statement = conn.createStatement();
|
||||
JSONArray items = table.getJSONArray("item");
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("CREATE TABLE `")
|
||||
.append(tableName)
|
||||
.append("` (");
|
||||
for (Object item : items) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
JSONObject it = (JSONObject) item;
|
||||
String type;
|
||||
switch (it.getString("type")) {
|
||||
case "int":
|
||||
type = " INTEGER ";
|
||||
break;
|
||||
case "TIME":
|
||||
type = " NUMERIC ";
|
||||
break;
|
||||
default:
|
||||
type = " TEXT ";
|
||||
break;
|
||||
}
|
||||
builder.append("`")
|
||||
.append(it.getString("name"))
|
||||
.append("`")
|
||||
.append(type)
|
||||
.append(it.getBoolean("isNull") ? "" : " NOT NULL ")
|
||||
.append(it.getBoolean("isKey") ? " PRIMARY KEY AUTOINCREMENT " : "")
|
||||
.append(",");
|
||||
sql.append(builder.toString());
|
||||
}
|
||||
sql.append(");");
|
||||
statement.execute(sql.toString().replace(",);", ");"));
|
||||
ConfigTools.save(ConfigTools.SQLITE,json.getString("file"),json);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void build(JSONObject json) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
sql = new File("db" + File.separator + json.getString("file"));
|
||||
if (!sql.exists()) {
|
||||
createSql(json);
|
||||
} else {
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
JSONObject json = JSONObject.parseObject("{\"file\":\"ad.db\",\"table\":{\"name\":\"sql\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"ticket_tmp\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"ticket\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"scannIndex\",\"type\":\"int\",\"isNull\":false,\"isKey\":false}]}}");
|
||||
SQLiteManager.getInstance().build(json);
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ import java.io.*;
|
||||
public class ConfigTools {
|
||||
public static final String CONFIG="config.json";
|
||||
public static final String DATA="data.json";
|
||||
public static final String SQLITE="sqlite.json";
|
||||
static {
|
||||
try {
|
||||
File file=new File(CONFIG);
|
||||
@ -43,11 +44,12 @@ public class ConfigTools {
|
||||
public static boolean save(String type,String key,Object data){
|
||||
File file=new File(type);
|
||||
String src=readFile(file);
|
||||
if(src!=null){
|
||||
if(src==null){
|
||||
src="{}";
|
||||
}
|
||||
JSONObject json=JSONObject.parseObject(src);
|
||||
json.put(key,data);
|
||||
saveFile(file,json.toJSONString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static boolean saveFile(File file,String data){
|
||||
|
Loading…
Reference in New Issue
Block a user