①List作为参数传递是值传递,还是引用传递
首先说说自己的看法:写了一些代码,觉得确实是引用传递,方法中对集合进行的修改。随后访问集合发现有数据。
但是:后来自己写了一段代码,发现与以上说法相悖。想不出来原因记录于此。
场景:商品添加购物车,如果这么写cartList_cookie中永远加不上数据。按照注解的写肯定没事。debug了一下发现集合在方法中确实是修改了,但是就是想值传递效果一样。
@RequestMapping("/findCartList")
public List<Cart> findCartList(){
String cartListString = CookieUtil.getCookieValue(request, "cartList","UTF-8");
if(cartListString==null || cartListString.equals("")){
cartListString="[]";
}
List<Cart> cartList_cookie=JSON.parseArray(cartListString,Cart.class);
return cartList_cookie;
}
@RequestMapping("/addGoodsToCartList")
public Result addGoodsToCartList(Long itemId,Integer num){
try {
List<Cart> cartList_cookie = findCartList();
//cartList_cookie=cartService.addGoodsToCartList(cartList_cookie, itemId, num);
cartService.addGoodsToCartList(cartList_cookie, itemId, num);
CookieUtil.setCookie(request, response, "cartList", JSON.toJSONString(cartList_cookie),3600*24,"UTF-8");
return new Result(true,"添加购物车成功!");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"添加购物车失败!");
}
}
public List<Cart> addGoodsToCartList(List<Cart> cartList, Long itemId, Integer num) {
/**
* 查询sku信息、有效性验证、无效抛异常
* 获取sellerId,去cartList中找Cart
* 存在:
* 查询订单项是否存在
* 存在:修改熟属性即可,数据有效性验证,如果没有则清除数据
* 不存在:根据sku和num创建订单项并添加
* 不存在:
* 创建一个购物车,添加属性,添加到cartList
*/
TbItem item = itemMapper.selectByPrimaryKey(itemId);
if(item==null){
throw new RuntimeException("数据非法!");
}
if(item.getStatus().equals("0")){
throw new RuntimeException("数据无效!");
}
String sellerId = item.getSellerId();
Cart cart=searchCartBySellerId(cartList,sellerId);
if(cart==null){
cart=new Cart();
cart.setSellerId(sellerId);
cart.setSellerName(item.getSeller());
TbOrderItem orderItem=createOrderItem(item,num);
List orderItemList=new ArrayList<>();
orderItemList.add(orderItem);
cart.setOrderItemList(orderItemList);
cartList.add(cart);
}else{
TbOrderItem orderItem=searchOrderItemByItemId(cart.getOrderItemList(),itemId);
if(orderItem==null){
orderItem=createOrderItem(item,num);
cart.getOrderItemList().add(orderItem);
}else{
orderItem.setNum(orderItem.getNum()+num);
orderItem.setTotalFee(new BigDecimal(orderItem.getNum()*orderItem.getPrice().doubleValue()));
if(orderItem.getNum()<=0){
cart.getOrderItemList().remove(orderItem);
}
if(cart.getOrderItemList().size()==0){
cartList.remove(cart);
}
}
}
return cartList;
}
private TbOrderItem searchOrderItemByItemId(List<TbOrderItem> orderItemList, Long itemId) {
for (TbOrderItem orderItem : orderItemList) {
if(orderItem.getItemId().equals(itemId)){
return orderItem;
}
}
return null;
}
private TbOrderItem createOrderItem(TbItem item, Integer num) {
if(num<0){
throw new RuntimeException("数量非法!");
}
TbOrderItem orderItem = new TbOrderItem();
orderItem.setNum(num);
orderItem.setPrice(item.getPrice());
orderItem.setItemId(item.getId());
orderItem.setTitle(item.getTitle());
orderItem.setGoodsId(item.getGoodsId());
orderItem.setSellerId(item.getSellerId());
orderItem.setPicPath(item.getImage());
orderItem.setTotalFee(new BigDecimal(item.getPrice().doubleValue()*num));
return orderItem;
}
private Cart searchCartBySellerId(List<Cart> cartList, String sellerId) {
for (Cart cart : cartList) {
if(cart.getSellerId().equals(sellerId)){
return cart;
}
}
return null;
}