前端傳對象數組到後臺List進行類型轉換

很多情況下,後端需要從前端接受數據進行處理,這裏寫到幾種接收數據的方法:

/**
 * 根據多個id獲取之前上傳的數據,這裏接受List需要使用RequestParam註解,否則會報異常
 * 如果傳遞的是對象,則需要使用@RequestBody List<Bean> bean,來進行接收
 */
@ApiOperation("根據id獲取之前上傳的數據,需要傳ids")
@PostMapping("/getBeanById")
public Result getFileById(@RequestParam(value ="ids") List<String> ids, HttpServletRequest request, HttpServletResponse response){
    Map map = Maps.newHashMap();
    for(String id : ids){
        String url = fileGetService.openlist(id);
        map.put(id, url);
    }
    return Result.success("獲取數據成功", map);
}



/**
 * 保存
 */
@ApiOperation("保存信息")
@ApiImplicitParam(name = "teams", value = "jsonArray('格式':'JSONArray')",  dataType = "String")
@PostMapping("/add")
public Result add(@RequestParam("teams") String teams){
    // 第一種方式
    JSONArray jsonArray= JSON.parseArray(teams);
    List<Team> str = jsonArray.toJavaList(Team.class);
    
    // 第二種方式
    List<Team>  str = JSON.parseArray(teams, Team.class);
    
    // 處理其他業務......
}



// mapper.xml可以這樣寫
<insert id="insertXtTeamManagelist"  useGeneratedKeys="true" keyProperty="id">
    insert into team
    <trim prefix="(" suffix=")" suffixOverrides=",">
        create_by,
        del_flag,
        remarks
    </trim>
    values
    <foreach collection="Team" item="item" index="index"
             separator=",">
        (
        #{item.createBy},
        #{item.delFlag},
        #{item.remarks}
        )
    </foreach>
</insert>


 

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