package com.u.api.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.u.api.mapper.GoodsMapper; import com.u.api.mapper.ShopingCartMapper; import com.u.api.model.ShopingCart; import com.u.api.service.ShopingCartService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class ShopingCartServiceImpl extends ServiceImpl<ShopingCartMapper, ShopingCart> implements ShopingCartService { @Resource private ShopingCartMapper shopingCartMapper; @Resource private GoodsMapper goodsMapper; @Override public int add(ShopingCart shopingCart) { QueryWrapper<ShopingCart> qw = new QueryWrapper<>(); qw.eq("gid",shopingCart.getGid()); qw.eq("uid",shopingCart.getUid()); ShopingCart shopingCart1 = shopingCartMapper.selectOne(qw); int rs = 0; if(shopingCart1 == null){ shopingCart.setNum(1); rs = shopingCartMapper.insert(shopingCart); }else { UpdateWrapper<ShopingCart> updateWrapper = new UpdateWrapper(); updateWrapper.eq("uid",shopingCart.getUid()); updateWrapper.eq("gid",shopingCart.getGid()); updateWrapper.setSql("num = num + 1"); rs = shopingCartMapper.update(null,updateWrapper); } return rs; } @Override public int del(ShopingCart shopingCart) { QueryWrapper<ShopingCart> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("uid",shopingCart.getUid()); queryWrapper.eq("id",shopingCart.getId()); return shopingCartMapper.delete(queryWrapper); } @Override public List<ShopingCart> getShopingCart(String account) { QueryWrapper<ShopingCart> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("uid",account); List<ShopingCart> shopingCarts = shopingCartMapper.selectList(queryWrapper); return shopingCarts; } }
时间: 2024-01-11 11:03:22 浏览: 89
这是一个基于SpringBoot框架开发的购物车服务实现类,下面对代码进行逐行注释:
```
package com.u.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.u.api.mapper.GoodsMapper;
import com.u.api.mapper.ShopingCartMapper;
import com.u.api.model.ShopingCart;
import com.u.api.service.ShopingCartService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class ShopingCartServiceImpl extends ServiceImpl<ShopingCartMapper, ShopingCart> implements ShopingCartService {
@Resource
private ShopingCartMapper shopingCartMapper;
@Resource
private GoodsMapper goodsMapper;
// 添加商品到购物车
@Override
public int add(ShopingCart shopingCart) {
QueryWrapper<ShopingCart> qw = new QueryWrapper<>();
qw.eq("gid",shopingCart.getGid()); // 根据商品id查询购物车中是否有该商品
qw.eq("uid",shopingCart.getUid()); // 根据用户id查询购物车中是否有该用户
ShopingCart shopingCart1 = shopingCartMapper.selectOne(qw); // 查询购物车中是否已经存在该商品
int rs = 0;
if(shopingCart1 == null){ // 如果不存在该商品,则添加一条新纪录
shopingCart.setNum(1); // 设置商品数量为1
rs = shopingCartMapper.insert(shopingCart); // 将商品添加到购物车,并返回添加结果
}else { // 如果已存在该商品,则更新商品数量
UpdateWrapper<ShopingCart> updateWrapper = new UpdateWrapper();
updateWrapper.eq("uid",shopingCart.getUid()); // 根据用户id更新商品数量
updateWrapper.eq("gid",shopingCart.getGid()); // 根据商品id更新商品数量
updateWrapper.setSql("num = num + 1"); // 商品数量加1
rs = shopingCartMapper.update(null,updateWrapper); // 更新商品数量,并返回更新结果
}
return rs; // 返回添加或更新结果
}
// 从购物车中删除商品
@Override
public int del(ShopingCart shopingCart) {
QueryWrapper<ShopingCart> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("uid",shopingCart.getUid()); // 根据用户id查询购物车中是否有该用户
queryWrapper.eq("id",shopingCart.getId()); // 根据购物车id查询要删除的商品
return shopingCartMapper.delete(queryWrapper); // 删除购物车中的商品
}
// 根据用户id获取购物车中的所有商品
@Override
public List<ShopingCart> getShopingCart(String account) {
QueryWrapper<ShopingCart> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("uid",account); // 根据用户id查询购物车中的商品
List<ShopingCart> shopingCarts = shopingCartMapper.selectList(queryWrapper); // 获取购物车中的商品列表
return shopingCarts; // 返回购物车中的所有商品
}
}
```
总体来说,这段代码实现了购物车服务的增、删、查等操作。其中,add方法实现了将商品添加到购物车中的功能;del方法实现了从购物车中删除商品的功能;getShopingCart方法实现了根据用户id获取购物车中的所有商品的功能。
阅读全文