@RequestMapping("obtainDishes") @ResponseBody public Object obtainDishes(String cartname){ List<Map> list=JSONObject.parseObject(cartname,List.class); List<Dishes> getList=new ArrayList<>(); for (Map ma : list) { Dishes dishes=dishesMapper.findList(ma); BigDecimal money1=new BigDecimal(dishes.getMoney()).multiply(new BigDecimal(ma.get("num").toString())); dishes.setMoney1(money1); dishes.setNum(ma.get("num").toString()); getList.add(dishes); } return getList; }
时间: 2024-04-02 10:37:49 浏览: 163
这段代码是一个基于Spring MVC框架编写的后端接口,用于获取购物车中的菜品列表。其中,@RequestMapping注解表示该方法与前端的/obtainDishes路径对应,@ResponseBody注解表示返回结果是一个JSON对象。该方法接受一个cartname参数,表示购物车中的菜品列表,将其转换成List<Map>对象。接着,循环遍历每一个Map对象,将其作为参数调用dishesMapper的findList方法获取对应的Dishes对象。然后,根据购物车中的数量计算出该道菜品的总价,并将其设置到Dishes对象的money1属性中。最后,将Dishes对象添加到getList列表中,并返回给前端。
相关问题
@RequestMapping("/getPage") @ResponseBody public Page<TbServiceAccept> getPage(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Page<TbServiceAccept> page, TbServiceAccept tbServiceAccept) { Map<String, Object> map = new HashMap<String, Object>(); map.put("createtimeGt",tbServiceAccept.getCreatetimeGt()); map.put("createtimeLt",tbServiceAccept.getCreatetimeLt()); map.put("clientphone",tbServiceAccept.getClientphone()); map.put("receiver",tbServiceAccept.getReceiver()); map.put("crmnumber",tbServiceAccept.getCrmnumber()); page= tbServiceAcceptService.findByPage(map, page); for (TbServiceAccept tbServiceAccept1 : page.getItems()) { List<Map<String, Object>> usermap = tbUserInfoService.findCustom("select * from tb_User_Info where (username = #{p[0]} or phone =#{p[0]}) and isDelete = 0", new Object[]{tbServiceAccept1.getId()}); if (!usermap.isEmpty()) { tbServiceAccept1.setJson(new JSONObject().fluentPut("khPhone", usermap.get(0).get("khPhone"))); } } return page;怎么改,不要findCustom改为list并且不用map方法
您可以将`findCustom`方法替换为`list`方法,并且不使用`Map`来存储参数。下面是修改后的代码:
```java
@RequestMapping("/getPage")
@ResponseBody
public Page<TbServiceAccept> getPage(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Page<TbServiceAccept> page, TbServiceAccept tbServiceAccept) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("createtimeGt", tbServiceAccept.getCreatetimeGt());
map.put("createtimeLt", tbServiceAccept.getCreatetimeLt());
map.put("clientphone", tbServiceAccept.getClientphone());
map.put("receiver", tbServiceAccept.getReceiver());
map.put("crmnumber", tbServiceAccept.getCrmnumber());
page = tbServiceAcceptService.findByPage(map, page);
for (TbServiceAccept tbServiceAccept1 : page.getItems()) {
List<TbUserInfo> userInfos = tbUserInfoService.list("select * from tb_User_Info where (username = ? or phone = ?) and isDelete = 0", new Object[]{tbServiceAccept1.getId(), tbServiceAccept1.getId()});
if (!userInfos.isEmpty()) {
TbUserInfo userInfo = userInfos.get(0);
tbServiceAccept1.setJson(new JSONObject().fluentPut("khPhone", userInfo.getKhPhone()));
}
}
return page;
}
```
这里使用`list`方法代替了`findCustom`方法,并直接传入参数数组作为查询条件。同时,使用`TbUserInfo`对象来存储查询结果,无需再使用`Map`。
get(`/video_list`, {param: {page: this.page}}).后端接受请求@RequestMapping(value = "/video_list", method = RequestMethod.GET) public R videoList(@RequestParam(name = "param") Map<String, Object> param报ERROR Failed to convert value of type 'java.lang.String' to required type 'java.util.Map';解决案例
这个错误是因为前端传递的参数是字符串类型,而后端接口需要的是Map类型。可以尝试在前端传递参数时将其转化为JSON对象,然后在后端接口中使用@RequestBody注解将其转化为Map类型。具体解决方案如下:
1. 前端代码:
```
const params = {
page: this.page
};
axios.get('/video_list', { params: { param: JSON.stringify(params) } }).then(res => {
// 处理返回结果
}).catch(err => {
// 处理错误
});
```
2. 后端代码:
```
@RequestMapping(value = "/video_list", method = RequestMethod.GET)
public R videoList(@RequestParam(name = "param") String param) {
Map<String, Object> paramMap = JSON.parseObject(param, new TypeReference<Map<String, Object>>() {});
// 处理参数
}
```
这样就可以解决这个错误了。
阅读全文