- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- SpringBoot實(shí)現物品收藏功能
數據庫設計:使用了第三張表來(lái)存儲哪個(gè)用戶(hù)收藏了哪個(gè)物品
寫(xiě)這個(gè)的時(shí)候壓根沒(méi)想到怎么判斷用戶(hù)是否已經(jīng)收藏,一開(kāi)始是前端發(fā)一次請求我后臺就添加數據到數據表中。這樣只是用戶(hù)可以在我的收藏中查看到收藏了的物品,但是點(diǎn)擊進(jìn)去無(wú)法讓收藏按鈕亮起來(lái)。
第一次點(diǎn)擊以后會(huì )亮,之后第二次進(jìn)來(lái)怎么判斷是否收藏這是個(gè)問(wèn)題。
解決:在全局js中寫(xiě)了一個(gè)請求,就是小程序剛進(jìn)入就發(fā)請求獲取當前用戶(hù)收藏信息(是一個(gè)數組)。然后進(jìn)入詳情頁(yè)時(shí)進(jìn)行判斷。并且當用戶(hù)再次收藏或者取消收藏后更改數組信息。當用戶(hù)退出詳情頁(yè)切換到我的界面將這個(gè)數組發(fā)送到后臺更新數據庫
后臺代碼:這個(gè)你就必須去判斷前端返回的數組和你數據庫當前數組有哪些區別,你是增加還是刪除信息,或者既要新增又要刪除
數據庫原有id【1,2,3】
前端返回id【1,4,5】
這個(gè)時(shí)候你既要找出2,3去刪除,又要找出4,5去新增。
@Override public int addGoodsInCar(@RequestBody JSONObject sums) { String openid = sums.getString("openid"); JSONArray gid = sums.getJSONArray("gid"); if (openid==null||gid==null){ return 0; } JSONArray gidf = (JSONArray)gid.clone();//深拷貝 ArrayList<Integer> integers = shoppingCarMapper.selectByOpenid(openid); int a = 1; if (integers.size()==0 ){ ShoppingCar car = new ShoppingCar(); car.setOpenid(openid); car.setGid(gid.getInteger(0)); a = shoppingCarMapper.insert(car); } if (!integers.isEmpty()){ //去重 Iterator<Object> it = gid.iterator(); while(it.hasNext()){ Integer gid1 =(Integer) it.next(); integers.forEach(item -> { if(item == gid1){ it.remove(); } }); } Iterator<Object> it1 = gid.iterator(); while (it1.hasNext() && a==1){ int next = (int)it1.next(); ShoppingCar car = new ShoppingCar(); car.setOpenid(openid); car.setGid(next); a = shoppingCarMapper.insert(car); } Iterator<Integer> integers0 = integers.iterator(); while(integers0.hasNext()){ Integer gid1 =integers0.next(); gidf.forEach(item -> { if(item == gid1){ integers0.remove(); } }); } Iterator<Integer> iterator2 = integers.iterator(); while (iterator2.hasNext() && a==1){ int next = iterator2.next(); ShoppingCar car = new ShoppingCar(); car.setOpenid(openid); car.setGid(next); a = shoppingCarMapper.deleteCar(car); } if (a==0){ return 0; }else { return 1; } } return 0; }
到此這篇關(guān)于SpringBoot實(shí)現物品收藏功能的文章就介紹到這了,更多相關(guān)SpringBoot物品收藏內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站