spring boot String類型json 存入數據庫

效果圖:

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章