效果圖:
如圖,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";
}