From b1e9d4040da17a518dc6364e1c860016cc2905dc Mon Sep 17 00:00:00 2001 From: wangyuan Date: Mon, 24 Jun 2019 12:06:48 +0800 Subject: [PATCH] fix read s table --- .../com/ljsd/jieling/logic/STableManager.java | 108 ++++++++---------- 1 file changed, 45 insertions(+), 63 deletions(-) diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/STableManager.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/STableManager.java index 55bf0b24d..a76ae81fc 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/STableManager.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/STableManager.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; @@ -81,43 +82,48 @@ public class STableManager { String tableName = clazz.getAnnotation(Table.class).name(); String path = SysUtil.getPath("conf", "server", tableName + ".txt"); File file = new File(path); - String line; - List key = new ArrayList<>(); - List type = new ArrayList<>(); - int lineNum = 0; + if (!file.exists()) { LOGGER.error("file not find {}, do not find sheet with {} you need rebuild all sheet by gen sheet tool!", path, tableName); // return null; } - try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){ - LOGGER.info("initMap:{}", clazz.getSimpleName()); - while ((line = bufferedReader.readLine()) != null) { - if (line.isEmpty()){ - continue; - } - T obj = clazz.newInstance(); - String[] prarms = line.split("\\t"); - switch (lineNum) { - case 0: - prarms = StringUtil.fieldHandle(prarms); - key.addAll(Arrays.asList(prarms)); - break; - case 1: - type.addAll(Arrays.asList(prarms)); - break; - default: - dealParams(clazz, map, key, type, obj, prarms); - break; - } - lineNum++; - } - } + LOGGER.info("initMap:{}", clazz.getSimpleName()); + readFileToCache(clazz, map, file); } catch (Exception e) { e.printStackTrace(); } return map; } + private static void readFileToCache(Class clazz, Map map, File file) throws IOException, InstantiationException, IllegalAccessException, NoSuchFieldException { + String line; + List key = new ArrayList<>(); + List type = new ArrayList<>(); + int lineNum = 0; + try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){ + while ((line = bufferedReader.readLine()) != null) { + if (line.isEmpty()){ + continue; + } + T obj = clazz.newInstance(); + String[] prarms = line.split("\\t"); + switch (lineNum) { + case 0: + prarms = StringUtil.fieldHandle(prarms); + key.addAll(Arrays.asList(prarms)); + break; + case 1: + type.addAll(Arrays.asList(prarms)); + break; + default: + dealParams(clazz, map, key, type, obj, prarms); + break; + } + lineNum++; + } + } + } + /** * 读取配置表到内存 @@ -125,44 +131,20 @@ public class STableManager { public static Map> getMapConfig(Class clazz) throws Exception { Map> map = new HashMap<>(); String tableName = clazz.getAnnotation(Table.class).name(); - int mapType = 100; - for (int i = 100; i < 1000; i++) { - Map mapConf = new HashMap<>(); - tableName = tableName + "_"+ i; - String path = SysUtil.getPath("conf", "server", tableName + ".txt"); - File file = new File(path); - if (!file.exists()) { - tableName = tableName.substring(0, 3); - mapType += 100; - i = mapType; + String pathDir = SysUtil.getPath("conf", "server"); + File mapDir = new File(pathDir); + String prefix = tableName + "_"; + int preLength = prefix.length(); + for(File mapFile : mapDir.listFiles()){ + tableName = mapFile.getName(); + if(!mapFile.getName().startsWith(prefix)){ continue; } - String line; - List key = new ArrayList<>(); - List type = new ArrayList<>(); - int lineNum = 0; - try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){ - LOGGER.info("initMap:{}", tableName); - while ((line = bufferedReader.readLine()) != null) { - T obj = clazz.newInstance(); - String[] prarms = line.split("\\t"); - switch (lineNum) { - case 0: - prarms = StringUtil.fieldHandle(prarms); - key.addAll(Arrays.asList(prarms)); - break; - case 1: - type.addAll(Arrays.asList(prarms)); - break; - default: - dealParams(clazz, mapConf, key, type, obj, prarms); - break; - } - lineNum++; - } - map.put(i, mapConf); - tableName = tableName.substring(0, 3); - } + int mapId = Integer.parseInt(tableName.substring(preLength).split("\\.")[0]); + LOGGER.info("initMap:{}", tableName); + Map mapConf = new HashMap<>(); + readFileToCache(clazz,mapConf,mapFile); + map.put(mapId, mapConf); } return map; }