从配置文件取值:Config
Config类用于获取config.properties
和该文件import的配置文件的值,构造方法如下所示:
1 2 3 4 5 6 7 8 9 10 11 12
| private Config() { this.loadProperties("/config.properties"); String importproperties = ConvertUtil.convertToString(properties.getProperty("import.propertiesfile")); if (!importproperties.isEmpty()) { String[] arrays = importproperties.split(";"); for (String temp : arrays) { this.loadProperties(temp); } } }
|
其中加载配置文件方法如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private void loadProperties(String configpath) { InputStream inputStream = null; try { inputStream = getClass().getResourceAsStream(new String(configpath)); properties.load(inputStream); } catch (Exception exception) { System.out.println("Can't read the properties file. "); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { throw new BusinessException(e); } } }
|
我们可以通过getInstance()
方法来实例化Config对象,进而调用getValue/getIntValue
方法来获取配置文件的值。
取配置参数(从数据库):ParameterCache
数据库中有一张表CBOSYSPARAM,在其中可以配置一些系统参数值(系统层面上的),比如最大重试次数、最大重推数量等等。通过ParameterCache.getValue
来获取,其逻辑如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static String getValue(String code, String defaultValue) { try { String value = cache.get(code, new Callable<String>() { @Override public String call() throws Exception { ICboSysParamService cboSysParamService = (ICboSysParamService) SpringUtil.getBean("cboSysParamService"); StringBufferProxy sql = new StringBufferProxy(); sql.appendSingle(" code = '{0}' ", code); List<CboSysParamEntity> listEntity = cboSysParamService.queryByWhere(sql.toString()); if (listEntity.size() > 0) { return ConvertUtil.convertToString(listEntity.get(0).getParamvalue()); } else { return defaultValue; } } }); return value; } catch (Exception e) { throw new BusinessException(e); } }
|
从数据库取值:DatacodeCache
数据库中有两张表CBOITEMS和CBOITEMDETAILS,对应一些编码值,ITEMS表存储编码名(消费者状态、是否有效等),DETAILS表存储编码对应的代码值列表(-1代表无效,1代表有效),
转换显示值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public String getValueDynamic(String itemCode, String fieldName, String value, String inCol, String outCol) { List<Map<String, Object>> mapList = null; if (this.itemDetailCache.containsKey(itemCode)) { this.itemDetailCache.put(itemCode, mapList); } else { mapList = this.itemDetailCache.get(itemCode); } if (mapList == null || mapList.size() <= 0) { return ""; } Optional<Map<String, Object>> resultOptional = mapList.parallelStream() .filter(dataMap -> { String tempValue = ConvertUtil.convertToString(dataMap.get(inCol)); return tempValue.equals(value); }) .findFirst(); if (resultOptional.isPresent()) { Map<String, Object> resultDataMap = resultOptional.get(); if (resultDataMap != null) { return ConvertUtil.convertToString(resultDataMap.get(outCol)); } } return "";
|
上述方法就可以实现将itemCode
对应的代码值列表中的inCol
的值转化成outCol
对应的值。
重构了批量转换显示值和无field的方法。
获取代码明细集合
根据itemCode找到要查的表对应的service方法,然后查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public List<Map<String, Object>> getCodeListMap(String itemCode, String sqlWhere, String order) { CboItemsEntity item = this.getItemEntity(itemCode); if (item == null) { return new ArrayList<Map<String, Object>>(); } IBasicService<?, ?> service = this.getServiceInstance(item); StringBufferProxy sql = new StringBufferProxy(); if (item.getTargettable().equals(CboItemDetailsEntity.tableName)) { sql.appendLineSingle(" itemid = '{0}'", item.getId()); } else { sql.appendLineSingle(" 1=1 "); } if (!StringUtil.isEmpty(item.getTargetcondition())) { sql.appendSingle(" and {0} ", item.getTargetcondition()); } if (!StringUtil.isEmpty(sqlWhere.trim())) { sql.appendSingle(" and {0} ", sqlWhere); } if (!StringUtil.isEmpty(order)) { sql.appendSingle(" order by {0} ", order); } List<String> filedList = new ArrayList<String>(); filedList.add(item.getIdcol() + " as id"); filedList.add(item.getCodecol() + " as code"); filedList.add(item.getNamecol() + " as name"); List<Map<String, Object>> mapList = service.queryMapFieldsByWhere(sql.toString(), filedList); return mapList; }
|
重构了不含order
和不含sqlWhere
的方法
加载编码明细并保存到缓存中
loadItemDetailCache
方法
调用getCodeListMap
方法,将itemCode
对应的编码明细查询出来并放入itemDetailCache
中
获取代码对象实体与service实例
- getItemEntity:根据
itemCode
获取代码对象实体
- getServiceInstance:根据
itemCode
获取服务实例