初始化数据调整

This commit is contained in:
bwcx_jzy 2020-10-23 14:55:31 +08:00
parent b9b30b8b0d
commit a88cacdd26
6 changed files with 78 additions and 16 deletions

View File

@ -16,6 +16,7 @@
5. 【Server】开始构建时输出代码目录
6. 【Server】编辑构建类型为SVN没有分组bug感谢@JAVA-落泪归枫)
7. 更新文档Jpom 的JDK要为1.8.0_40+(感谢@JAVA 企鹅)
8. 【Server】数据库初始化时间前置,打印成功日志,未初始化结束数据库相关操作都忽略
-----------------------------------------------------------

View File

@ -192,7 +192,9 @@ public class JpomManifest {
*/
public static File getRunPath() {
URL location = ClassUtil.getLocation(JpomApplication.getAppClass());
return FileUtil.file(location);
String file = location.getFile();
String before = StrUtil.subBefore(file, "!", false);
return FileUtil.file(before);
}
/**
@ -221,7 +223,7 @@ public class JpomManifest {
* @param clsName 类名
* @return 结果消息
*/
public static JsonMessage checkJpomJar(String path, Class clsName) {
public static JsonMessage<String> checkJpomJar(String path, Class<?> clsName) {
String version;
File jarFile = new File(path);
try (JarFile jarFile1 = new JarFile(jarFile)) {
@ -229,37 +231,37 @@ public class JpomManifest {
Attributes attributes = manifest.getMainAttributes();
String mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS);
if (mainClass == null) {
return new JsonMessage(405, "清单文件中没有找到对应的MainClass属性");
return new JsonMessage<>(405, "清单文件中没有找到对应的MainClass属性");
}
JarClassLoader jarClassLoader = JarClassLoader.load(jarFile);
try {
jarClassLoader.loadClass(mainClass);
} catch (ClassNotFoundException notFound) {
return new JsonMessage(405, "中没有找到对应的MainClass:" + mainClass);
return new JsonMessage<>(405, "中没有找到对应的MainClass:" + mainClass);
}
ZipEntry entry = jarFile1.getEntry(StrUtil.format("BOOT-INF/classes/{}.class",
StrUtil.replace(clsName.getName(), ".", "/")));
if (entry == null) {
return new JsonMessage(405, "此包不是Jpom【" + JpomApplication.getAppType().name() + "】包");
return new JsonMessage<>(405, "此包不是Jpom【" + JpomApplication.getAppType().name() + "】包");
}
version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
if (StrUtil.isEmpty(version)) {
return new JsonMessage(405, "此包没有版本号");
return new JsonMessage<>(405, "此包没有版本号");
}
String timeStamp = attributes.getValue("Jpom-Timestamp");
if (StrUtil.isEmpty(timeStamp)) {
return new JsonMessage(405, "此包没有版本号");
return new JsonMessage<>(405, "此包没有版本号");
}
timeStamp = parseJpomTime(timeStamp);
if (StrUtil.equals(version, JpomManifest.getInstance().getVersion()) &&
StrUtil.equals(timeStamp, JpomManifest.getInstance().getTimeStamp())) {
return new JsonMessage(405, "新包和正在运行的包一致");
return new JsonMessage<>(405, "新包和正在运行的包一致");
}
} catch (Exception e) {
DefaultSystemLog.getLog().error("解析jar", e);
return new JsonMessage(500, " 解析错误:" + e.getMessage());
return new JsonMessage<>(500, " 解析错误:" + e.getMessage());
}
return new JsonMessage(200, version);
return new JsonMessage<>(200, version);
}
/**

View File

@ -8,6 +8,7 @@ import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import io.jpom.system.JpomRuntimeException;
import io.jpom.system.db.DbConfig;
import java.sql.SQLException;
import java.util.ArrayList;
@ -23,8 +24,8 @@ public abstract class BaseDbLogService<T> {
/**
* 表名
*/
private String tableName;
private Class<T> tClass;
private final String tableName;
private final Class<T> tClass;
/**
* 主键
*/
@ -53,6 +54,10 @@ public abstract class BaseDbLogService<T> {
* @param t 数据
*/
public void insert(T t) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return;
}
Db db = Db.use();
db.setWrapper((Character) null);
try {
@ -82,6 +87,10 @@ public abstract class BaseDbLogService<T> {
* @return 影响行数
*/
public int update(Entity entity, Entity where) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return 0;
}
Db db = Db.use();
db.setWrapper((Character) null);
if (where.isEmpty()) {
@ -103,6 +112,10 @@ public abstract class BaseDbLogService<T> {
* @return 数据
*/
public T getByKey(String keyValue) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return null;
}
Entity where = new Entity(tableName);
where.set(key, keyValue);
Db db = Db.use();
@ -129,6 +142,10 @@ public abstract class BaseDbLogService<T> {
* @return 影响行数
*/
public int delByKey(String keyValue) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return 0;
}
Entity where = new Entity(tableName);
where.set(key, keyValue);
return del(where);
@ -141,6 +158,10 @@ public abstract class BaseDbLogService<T> {
* @return 影响行数
*/
public int del(Entity where) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return 0;
}
where.setTableName(tableName);
if (where.isEmpty()) {
throw new JpomRuntimeException("没有删除条件");
@ -162,6 +183,10 @@ public abstract class BaseDbLogService<T> {
* @return 结果
*/
public PageResult<T> listPage(Entity where, Page page) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return new PageResult<>(page.getPageNumber(), page.getPageSize(), 0);
}
where.setTableName(getTableName());
PageResult<Entity> pageResult;
Db db = Db.use();

View File

@ -31,6 +31,11 @@ public class DbConfig {
private static DbConfig dbConfig;
/**
* 是否初始化成功
*/
private volatile boolean init;
/**
* 单利模式
*
@ -43,6 +48,14 @@ public class DbConfig {
return dbConfig;
}
public void initOk() {
init = true;
}
public boolean isInit() {
return init;
}
/**
* 获取数据库的jdbc 连接
*

View File

@ -14,20 +14,20 @@ import io.jpom.system.db.DbConfig;
import java.io.InputStream;
/**
* 初始化数据库
*
* @author jiangzeyin
* @date 2019/4/19
*/
@PreLoadClass
@PreLoadClass(-1)
public class InitDb {
@PreLoadMethod
private static void init() {
Setting setting = new Setting();
setting.set("url", DbConfig.getInstance().getDbUrl());
DbConfig instance = DbConfig.getInstance();
setting.set("url", instance.getDbUrl());
setting.set("user", "jpom");
setting.set("pass", "jpom");
// 调试模式显示sql 信息
@ -36,7 +36,7 @@ public class InitDb {
setting.set("sqlLevel", "INFO");
setting.set("showParams", "true");
}
DefaultSystemLog.getLog().info("初始化数据中....");
DefaultSystemLog.getLog().warn("load h2 db");
try {
// 创建连接
DSFactory dsFactory = DSFactory.create(setting);
@ -47,6 +47,9 @@ public class InitDb {
} catch (Exception e) {
DefaultSystemLog.getLog().error("初始化数据库失败", e);
System.exit(0);
return;
}
instance.initOk();
DefaultSystemLog.getLog().warn("h2 db inited");
}
}

View File

@ -1,5 +1,11 @@
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Test;
import org.springframework.util.AntPathMatcher;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author bwcx_jzy
* @date 2019/8/24
@ -10,4 +16,16 @@ public class TestPath {
AntPathMatcher antPathMatcher = new AntPathMatcher();
System.out.println(antPathMatcher.match("/s/**/sss.html", "//s/s/s/sss.html"));
}
@Test
public void test() throws MalformedURLException {
URL url = new URL("jar:file:/home/jpom/server/lib/server-2.4.8.jar!/BOOT-INF/classes!/");
String file = url.getFile();
String x = StrUtil.subBefore(file, "!", false);
System.out.println(x);
System.out.println(FileUtil.file(x));
System.out.println(StrUtil.subBefore("sssddsf", "!", false));
}
}