查询购物车中的商品
Controller层:
@RequestMapping("list.do")
@ResponseBody
public ServerResponse<CartVo> list(HttpSession session){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.list(user.getId());
}
interface ICartService:
public interface ICartService {
ServerResponse<CartVo> list (Integer userId);
}
Service层:
public ServerResponse<CartVo> list (Integer userId){
CartVo cartVo = this.getCartVoLimit(userId);
return ServerResponse.createBySuccess(cartVo);
}
获取购物车产品数量:
Controller层:
@RequestMapping("get_cart_product_count.do")
@ResponseBody
public ServerResponse<Integer> getCartProductCount(HttpSession session){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createBySuccess(0);
}
return iCartService.getCartProductCount(user.getId());
}
Service层:
public ServerResponse<Integer> getCartProductCount(Integer userId){
if(userId == null){
return ServerResponse.createBySuccess(0);
}
return ServerResponse.createBySuccess(cartMapper.selectCartProductCount(userId));
}
mapper:
int selectCartProductCount(@Param("userId") Integer userId);
<select id="selectCartProductCount" parameterType="int" resultType="int">
select IFNULL(sum(quantity),0) as count from mmall_cart where user_id = #{userId}
</select>