数据库保存数组数据
最近学Vue,前端有个下拉多选的组件,选完后传给后端的是个字符串数据,我用实体类字符串数组类型是存不到数据库的会报错,解决办法如下:实体类如下public class ProblemCheckRecording implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "id"
·
最近学Vue,前端有个下拉多选的组件,传给后端的是个数组,实体类中字符串类型是接收不了数组的;改用数组来接收,可数据库没有数组类型,是不能直接存到数据库的,解决办法如下:
实体类如下
public class ProblemCheckRecording implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String bSealed;
//用于接收数组,在数据表中并不存在
@TableField(exist = false)
@JsonProperty(value ="problemCheckbox")
private String[] problemCheckbox;
}
controller中使用JSONObject.toJSONString()将字符串数组转存一下,这样就可以存到数据库了
@ApiOperation("update:修改")
@PostMapping("/update")
@Transactional
public Result update(@RequestBody ProblemCheckRecording entity)
{ Result result = new Result();
try {
if(StringUtils.isNotEmpty(entity.getProblemCheckbox()))
{ entity.setBSealed(JSONObject.toJSONString(entity.getProblemCheckbox())); }
baseService.update(entity,plfUser);
}catch (Exception e){
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
return result.error(Result.CODE_ADD_FAILED + ":" + e.getMessage());
}
return result;
}
存入数据如下
更多推荐
已为社区贡献7条内容
所有评论(0)