當前位置:編程學習大全網 - 源碼下載 - 購物車只顯示壹次商鋪,然後在顯示該商鋪所有加入購物車的商品。 java邏輯怎麽寫? java邏輯怎麽寫?

購物車只顯示壹次商鋪,然後在顯示該商鋪所有加入購物車的商品。 java邏輯怎麽寫? java邏輯怎麽寫?

問題不太完整,首先購物車有很多種實現方法.常見的有cookie保存和數據庫保存.

其實不管是哪種保存方式,妳的問題的本質解決方案其實就是壹個數據聚合的過程.我們假設妳是用的數據庫來保存購物車.妳可以把購物車的展示拆解為兩個ajax接口.(我只寫偽代碼).

假設妳的購物車表結構如下:

商鋪表結構如下:

接口1(用來查看用戶購物車裏面有哪些商鋪):

public?List<CartShopInfo>?getMyShoppingCartShopList(Integer?userId)?{

String?sql?=?"select?DISTINCT(shop_id),shop_info.shop_name,?count(1)?from?shopping_cart?

left?join?shop_info?on?shop_info.id?=?shopping_cart.shop_id

where?user_id?=group?by?shop_id";

//執行sql,返回數據持久對象,怎麽寫取決於妳用jdbc,mybatis還是其他.

List<CartShopInfo>?cartShopInfoList?=?cartShopInfoDao.execute(sql,userId);

return?cartShopInfoList;

}

接口2(用來查看用戶購物車裏商鋪的全部商品)

public?List<ProductInfo>?getMyShoppingCartProductList(Integer?userId,Integer?shopId)?{

String?sql?=?"select?*?from?product_info?where?id?in?(select?product_id?from?shopping_cart?

where?user_id?=and?shop_id?=?)";

List<ProductInfo>?productList?=?productDao.execute(sql,userId,shopId);

return?productList;

}

如果嫌兩個接口麻煩,可以把接口邏輯合並,返回壹個Map<String,ShopCartInfo>

public?class?ShopCartInfo?{

private?ShopInfo?shopInfo;

private?List<ProductInfo>?productList;

//...?getter?and?setter

}

  • 上一篇:“獨角獸公司”是什麽意思?
  • 下一篇:後臺模板如何前臺模板框架
  • copyright 2024編程學習大全網