最近学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;
    }

存入数据如下 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐