feat:增加指定初始文件路径

V0.5.x
xiwa 2024-02-28 22:36:56 +08:00
parent 8c44979dcf
commit 3cd0622adb
2 changed files with 12 additions and 3 deletions

View File

@ -47,6 +47,7 @@ import org.springframework.stereotype.Service;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -55,9 +56,12 @@ import java.util.TimerTask;
@Service @Service
public class ExampleDataInit implements SmartInitializingSingleton { public class ExampleDataInit implements SmartInitializingSingleton {
@Value("${iita.init.data:true}") @Value("${init.data.flag:true}")
private boolean initDataFlg; private boolean initDataFlg;
@Value("${init.data.path:.}")
private String initDataPath;
@Autowired @Autowired
private IDbStructureData dbStructureData; private IDbStructureData dbStructureData;
@ -199,10 +203,14 @@ public class ExampleDataInit implements SmartInitializingSingleton {
try { try {
log.info("init {} data...", name); log.info("init {} data...", name);
if (service.count() > 0) { if (service.count() > 0) {
new RuntimeException("原数据库已存在" + name + "的旧数据,请清除后再重新初始化!").printStackTrace(); log.error("原数据库已存在" + name + "的旧数据,请清除后再重新初始化!系统正在退出。。。");
System.exit(0); System.exit(0);
} }
String json = FileUtils.readFileToString(new File("./data/init/" + name + ".json"), StandardCharsets.UTF_8); String path = initDataPath;
if (initDataPath.equals(".")) {
path = "./data/init";
}
String json = FileUtils.readFileToString(Paths.get(path, name + ".json").toFile(), StandardCharsets.UTF_8);
List list = (List) JsonUtils.parseObject(json, type); List list = (List) JsonUtils.parseObject(json, type);
for (Object obj : list) { for (Object obj : list) {
service.save((Id) obj); service.save((Id) obj);

View File

@ -205,3 +205,4 @@ plugin:
mainPackage: cc.iotkit mainPackage: cc.iotkit
pluginPath: pluginPath:
- ./data/plugins - ./data/plugins