map中的key值,使用containsKey无法比较
如下代码,为购物车功能,Product为pojo类,查询商品信息后放到购物车,使用containsKey进行比较,商品明明相同,但是给的结果却是不同,求指点
public String addCart(Model model,String pid,HttpSession session) throws Exception{
Product product=infoService.findOne(pid);
//从session中获得购物车的数据
Map<Product,Integer> cart= (Map<Product, Integer>) session.getAttribute("cart");
//如果不存在购物车,则重新创建一个
if(cart==null){
cart=new HashMap<Product,Integer>();
}
//判断购物车中是否有该商品,如果存在则将该商品数加一
if(cart.containsKey(product)){
cart.put(product, cart.get(product)+1);
}else{
//否则购物车数据为1
cart.put(product, 1);
}
//将数据放到session中
session.setAttribute("cart", cart);
return "page/cart";
}