效果图:
如图,string类型的json字符串,存入数据库,主要就是解析成map,遍历插入,不多说上干货:
@PostMapping(value = "/currentConfig")public String saveSvConfig(@RequestParam("config") String configStr) throws IOException {Integer version = svConfigurationDao.getNewestVersion();Integer versionNow = version+1;SvConfigurationVersion svcv = new SvConfigurationVersion();Date date = new Date();svcv.setCreatetime(date);svcv.setVersion(version+1);svConfigurationVersionRepository.save(svcv);ObjectMapper mapper = new ObjectMapper();Map<String,Object> map = mapper.readValue(configStr, Map.class);SvConfiguration svc = null;for (String key : map.keySet()) {svc = new SvConfiguration();Float value = Float.parseFloat(map.get(key).toString());svc.setKey(key);svc.setValue(value);svc.setVersion(versionNow);svConfigurationRepository.save(svc);System.out.println("Key = " + key + ", Value = " + value);}
// svConfigurationService.saveCurrentConfig(configStr);return "success";}