使用jdbctemplate将json数据存储/检索到mysql

时间:2017-06-19 07:15:05

标签: mysql json jdbctemplate

我想使用jdbcTemplate将json对象存储/检索到mysql json列中。这是我现有的代码,

@RequestMapping(value="/create/template", method = RequestMethod.POST, consumes = "application/json", produces = "text/html")
public String TemplateCreation(@RequestBody TemplateBojaClass dataFromUser){
    TemplateBojaClass objCreation = dataFromUser;
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.writeValue(new File("D:\\objCreation.getTemplateName()+".json"), objCreation);

}

但我想将json数据存储到表列而不是存储在文件夹文件中。我知道mysql 5.7有 JSON dataType 所以现在我创建了如下表格,

CREATE TABLE `createTemplate` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `templateData` json DEFAULT NULL,
  PRIMARY KEY (`id`)
);

我想将json输入存储到 templateData 列中,如下所示,

@RequestMapping(value="/create/template", method = RequestMethod.POST, consumes = "application/json", produces = "text/html")
public String TemplateCreation(@RequestBody TemplateBojaClass dataFromUser){
    TemplateBojaClass objCreation = dataFromUser;
    ObjectMapper mapper = new ObjectMapper();
     String sql="insert into template_creation(templateData) values(?);";
    jdbcTemplate.update(sql, new Object[]{**How to pass the json parameter here?**});

}

我无法从谷歌找到解决方案。有人帮助我实现这个目标吗?

0 个答案:

没有答案