初始化db数据逻辑完善.

This commit is contained in:
aoshiguchen
2022-07-31 18:21:30 +08:00
parent 0a1e849beb
commit 0368004736
7 changed files with 179 additions and 36 deletions
+5
View File
@@ -25,6 +25,11 @@
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.24</version>
</dependency>
</dependencies>
<build>
@@ -21,15 +21,14 @@
*/
package fun.asgc.neutrino.proxy.server.base.rest;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Lists;
import fun.asgc.neutrino.core.annotation.PreLoad;
import fun.asgc.neutrino.core.util.CollectionUtil;
import fun.asgc.neutrino.core.util.FileUtil;
import fun.asgc.neutrino.core.util.LockUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import fun.asgc.neutrino.core.db.template.JdbcTemplate;
import fun.asgc.neutrino.core.util.*;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;
/**
@@ -37,21 +36,25 @@ import java.util.List;
* @author: 初始化数据库
* @date: 2022/7/31
*/
@Slf4j
@PreLoad("init")
public class DBInitialize {
private static Connection conn;
private static List<String> initDataTableNameList = Lists.newArrayList("user");
private static SqliteConfig sqliteConfig;
private static JdbcTemplate jdbcTemplate;
public static void init() throws Exception {
sqliteConfig = ConfigUtil.getYmlConfig(SqliteConfig.class);
jdbcTemplate = getJdbcTemplate();
initDBStructure();
initDBData();
}
/**
* 初始化数据库结构
*/
private static void initDBStructure() throws Exception {
Statement stat = getOrNewConnection().createStatement();
List<String> lines = FileUtil.readContentAsStringList("classpath:/init-structure.sql");
List<String> lines = FileUtil.readContentAsStringList("classpath:/sql/init-structure.sql");
if (CollectionUtil.isEmpty(lines)) {
return;
}
@@ -62,27 +65,66 @@ public class DBInitialize {
}
sql += "\r\n" + line.trim();
if (sql.endsWith(";")) {
stat.executeUpdate(sql);
log.debug("初始化数据库表 sql:{}", sql);
jdbcTemplate.update(sql);
sql = "";
}
}
}
/**
* 获取一个数据库连接,如果数据库不存在,则会创建一个空数据
* 初始化数据
* @throws Exception
*/
private static void initDBData() throws Exception {
if (CollectionUtil.isEmpty(initDataTableNameList)) {
return;
}
for (String tableName : initDataTableNameList) {
// 表里没有数据的时候,才进行初始化操作
int count = jdbcTemplate.queryForInt(String.format("select count(1) from `%s`", tableName));
if (count > 0) {
continue;
}
List<String> lines = FileUtil.readContentAsStringList(String.format("classpath:/sql/%s.data.sql", tableName));
if (CollectionUtil.isEmpty(lines)) {
return;
}
String sql = "";
for (String line : lines) {
if (StringUtil.isEmpty(line) || StringUtil.isEmpty(line.trim()) || line.trim().startsWith("#")) {
continue;
}
sql += "\r\n" + line.trim();
if (sql.endsWith(";")) {
jdbcTemplate.update(sql);
sql = "";
}
}
}
}
/**
* 获取jdbcTemplate实例
* @return
* @throws Exception
*/
public static Connection getOrNewConnection() throws Exception {
private static JdbcTemplate getJdbcTemplate() throws Exception {
return LockUtil.doubleCheckProcess(
() -> null == conn,
() -> null == jdbcTemplate,
DBInitialize.class,
() -> {
Class.forName("org.sqlite.JDBC");
Class.forName(sqliteConfig.getDriverClass());
//建立一个数据库名data.db的连接,如果不存在就在当前目录下创建之
DBInitialize.conn = DriverManager.getConnection("jdbc:sqlite:data.db");
DriverManager.getConnection(sqliteConfig.getUrl());
// 创建数据源
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(sqliteConfig.getUrl());
dataSource.setDriverClassName(sqliteConfig.getDriverClass());
// 创建jdbcTemplate
jdbcTemplate = new JdbcTemplate(dataSource);
},
() -> conn
() -> jdbcTemplate
);
}
}
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.base.rest;
import fun.asgc.neutrino.core.annotation.Configuration;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.annotation.Value;
import lombok.Data;
/**
*
* @author: aoshiguchen
* @date: 2022/7/31
*/
@Data
@NonIntercept
@Configuration(prefix = "neutrino.data.sqlite")
public class SqliteConfig {
/**
* 连接url
*/
@Value("url")
private String url;
/**
* 驱动类
*/
@Value("driver-class")
private String driverClass;
}
@@ -29,3 +29,7 @@ neutrino:
license:
79419a1a8691413aa5e845b9e3e90051: 3
9352b1c25f564c81a5677131d7769876: 2
data:
sqlite:
url: jdbc:sqlite:data.db
driver-class: org.sqlite.JDBC
@@ -1,19 +0,0 @@
#
CREATE TABLE IF NOT EXISTS `user` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` varchar(50) NOT NULL,
`login_name` varchar(50) NOT NULL,
`login_password` varchar(255) NOT NULL,
`create_time` INTEGER(20) NOT NULL,
`update_time` INTEGER(20) NOT NULL
);
#license表
CREATE TABLE IF NOT EXISTS `license` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` varchar(50) NOT NULL,
`key` varchar(100) NOT NULL,
`user_id` INTEGER NOT NULL,
`create_time` INTEGER(20) NOT NULL,
`update_time` INTEGER(20) NOT NULL
);
@@ -0,0 +1,63 @@
#
CREATE TABLE IF NOT EXISTS `user` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` varchar(50) NOT NULL,
`login_name` varchar(50) NOT NULL,
`login_password` varchar(255) NOT NULL,
`create_time` INTEGER(20) NOT NULL,
`update_time` INTEGER(20) NOT NULL
);
#license表
CREATE TABLE IF NOT EXISTS `license` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` varchar(50) NOT NULL,
`key` varchar(100) NOT NULL,
`user_id` INTEGER NOT NULL,
`is_online` INTEGER(2) NOT NULL,
`create_time` INTEGER(20) NOT NULL,
`update_time` INTEGER(20) NOT NULL
);
#token表
CREATE TABLE IF NOT EXISTS `user_token` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`token` varchar(50) NOT NULL,
`user_id` INTEGER NOT NULL,
`expiration_time` INTEGER NOT NULL,
`create_time` INTEGER NOT NULL,
`update_time` INTEGER NOT NULL
);
#
CREATE TABLE IF NOT EXISTS `user_login_record` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`user_id` INTEGER NOT NULL,
`ip` varchar(50) NOT NULL,
`token` varchar(100) NOT NULL,
`type` INTEGER(2) NOT NULL,
`create_time` INTEGER(20) NOT NULL
);
#
CREATE TABLE IF NOT EXISTS `user_login_record` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`user_id` INTEGER NOT NULL,
`ip` varchar(50) NOT NULL,
`token` varchar(50) NOT NULL,
`type` INTEGER(2) NOT NULL,
`create_time` INTEGER(20) NOT NULL
);
#
CREATE TABLE IF NOT EXISTS `port_mapping` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`license_id` INTEGER(20) NOT NULL,
`service_port` INTEGER NOT NULL,
`client_ip` varchar(20) NOT NULL,
`client_port` INTEGER NOT NULL,
`is_online` INTEGER(2) NOT NULL,
`enable` INTEGER NOT NULL,
`create_time` INTEGER(20) NOT NULL,
`update_time` INTEGER(20) NOT NULL
);