缓存菜品,只需要在DishController中修改一下代码即可
/** * 菜品管理 */ @RestController @RequestMapping("/admin/dish") @Api(tags = "菜品相关接口") @Slf4j public class DishController { @Autowired private DishService dishService; @Autowired private RedisTemplate redisTemplate; /** * 新增菜品 * @param dishDTO * @return */ @PostMapping @ApiOperation("新增菜品") public Result save(@RequestBody DishDTO dishDTO){ log.info("新增菜品:{}",dishDTO); dishService.saveWithFlavor(dishDTO); //清楚缓存数据 String key = "dish_" + dishDTO.getCategoryId(); cleanCache(key); return Result.success(); } /** * * @return */ @GetMapping("/page") @ApiOperation("菜品分页查询") public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){ log.info("菜品分页查询:{}",dishPageQueryDTO); PageResult pageResult = dishService.pageQuery(dishPageQueryDTO); return Result.success(pageResult); } /** * 菜品批量删除 * @param ids * @return */ @DeleteMapping @ApiOperation("菜品批量删除") public Result delete(@RequestParam List<Long> ids){ log.info("菜品批量删除:{}",ids); dishService.deleteBatch(ids); //将所有的菜品缓存数据清理掉,所有以dish_开头的key cleanCache("dish_*"); return Result.success(); } /** * 根据id查询菜品 * @param id * @return */ @GetMapping("/{id}") @ApiOperation("根据id查询菜品") public Result<DishVO> getById(@PathVariable Long id){ log.info("根据id查询菜品:{}",id); DishVO dishVO = dishService.getByIdWithFlavor(id); return Result.success(dishVO); } /** * 修改菜品 * @param dishDTO * @return */ @PutMapping @ApiOperation("修改菜品") public Result update(@RequestBody DishDTO dishDTO){ log.info("修改菜品:{}",dishDTO); dishService.updateWithFlavor(dishDTO); //将所有的菜品缓存数据清理掉,所有以dish_开头的key cleanCache("dish_*"); return Result.success(); } /** * 菜品起售停售 * @param status * @param id * @return */ @PostMapping("/status/{status}") @ApiOperation("菜品起售停售") public Result startOrStop(@PathVariable Integer status,Long id){ log.info("菜品起售停售:{},{}",status,id); dishService.startOrStop(status,id); //将所有的菜品缓存数据清理掉,所有以dish_开头的key cleanCache("dish_*"); return Result.success(); } /** * 根据分类id查询菜品 * @param categoryId * @return */ @GetMapping("/list") @ApiOperation("根据分类id查询菜品") public Result<List<Dish>> list(Long categoryId){ List<Dish> list = dishService.list(categoryId); return Result.success(list); } /** * 清理缓存数据 * @param pattern */ private void cleanCache(String pattern){ Set keys = redisTemplate.keys(pattern); redisTemplate.delete(keys); } }
缓存套餐
用到Spring Cache,如果不会的话请看我这篇文章
Spring Cache常用注解-CSDN博客
1、导入Spring Cache 和Redis相关的maven坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
2、在启动类上加入@EnableCaching注解,开启缓存注解功能
@SpringBootApplication @EnableTransactionManagement //开启注解方式的事务管理 @Slf4j @EnableCaching //开启缓存注解功能 public class SkyApplication { public static void main(String[] args) { SpringApplication.run(SkyApplication.class, args); log.info("server started"); } }
3、在用户端接口SetmealController的list 方法上加入@Cacheable注解
@RestController("userSetmealController") @RequestMapping("/user/setmeal") @Api(tags = "C端-套餐浏览接口") public class SetmealController { @Autowired private SetmealService setmealService; /** * 条件查询 * * @param categoryId * @return */ @GetMapping("/list") @ApiOperation("根据分类id查询套餐") @Cacheable(cacheNames = "setmealCache" , key = "#categoryId") //key : setmealCache::100 public Result<List<Setmeal>> list(Long categoryId) { Setmeal setmeal = new Setmeal(); setmeal.setCategoryId(categoryId); setmeal.setStatus(StatusConstant.ENABLE); List<Setmeal> list = setmealService.list(setmeal); return Result.success(list); }
4、在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解
/** * 新增套餐 * @param setmealDTO */ @PostMapping @ApiOperation("新增套餐") @CacheEvict(cacheNames = "setmealCache" , key = "#setmealDTO.categoryId") //key : setmealCache::100 public Result save(@RequestBody SetmealDTO setmealDTO){ log.info("新增套餐:{}",setmealDTO); setmealService.saveWithDish(setmealDTO); return Result.success(); } /** * 批量删除套餐 * @param ids * @return */ @DeleteMapping @ApiOperation("批量删除套餐") @CacheEvict(cacheNames = "setmealCache" , allEntries = true) public Result delete(@RequestParam List<Long> ids){ log.info("批量删除套餐:{}",ids); setmealService.deleteBatch(ids); return Result.success(); } /** * 修改套餐 * @param setmealDTO */ @PutMapping @ApiOperation("修改套餐") @CacheEvict(cacheNames = "setmealCache" , allEntries = true) public Result update(@RequestBody SetmealDTO setmealDTO){ log.info("修改套餐:{}",setmealDTO); setmealService.update(setmealDTO); return Result.success(); } /** * 套餐起售停售 * @param status * @param id * @return */ @PostMapping("/status/{status}") @ApiOperation("套餐起售停售") @CacheEvict(cacheNames = "setmealCache" , allEntries = true) public Result startOrStop(@PathVariable Integer status,Long id){ log.info("套餐起售停售:{},{}",status,id); setmealService.startOrStop(status,id); return Result.success(); }
测试:点击小程序,分类套餐,redis中也有了缓存数据
点击停售,redis的缓存数据全部删除 ,测试成功!
添加购物车
ShoppingCartController代码
@RestController @RequestMapping("/user/shoppingCart") @Slf4j @Api(tags = "C端购物车相关接口") public class ShoppingCartController { @Autowired private ShoppingCartService shoppingCartService; /** * 添加购物车 * @param shoppingCartDTO * @return */ @PostMapping("/add") @ApiOperation("添加购物车") public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){ log.info("添加购物车,商品信息为:{}",shoppingCartDTO); shoppingCartService.addShoppingCart(shoppingCartDTO); return Result.success(); } }
ShoppingCartService接口代码
public interface ShoppingCartService { /** * 添加购物车 * @param shoppingCartDTO */ void addShoppingCart(ShoppingCartDTO shoppingCartDTO); }
ShoppingCartServiceImpl代码
@Service @Slf4j public class ShoppingCartServiceImpl implements ShoppingCartService { @Autowired private ShoppingCartMapper shoppingCartMapper; @Autowired private DishMapper dishMapper; @Autowired private SetmealMapper setmealMapper; /** * 添加购物车 * @param shoppingCartDTO */ public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) { //判断当前加入到购物车中的商品是否已经存在了 ShoppingCart shoppingCart = new ShoppingCart(); BeanUtils.copyProperties(shoppingCartDTO,shoppingCart); Long userId = BaseContext.getCurrentId(); shoppingCart.setUserId(userId); List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart); //如果已经存在了,只需要将数量加一 if(list != null && list.size() > 0){ ShoppingCart cart = list.get(0); cart.setNumber(cart.getNumber()+1); //update shopping_cart set number = ? where id = ? shoppingCartMapper.updateNumberById(cart); }else { //如果不存在,需要插入一条购物车数据 //判断本次添加到购物车的是菜品还是套餐 Long dishId = shoppingCartDTO.getDishId(); if(dishId != null){ //本次添加到购物车的是菜品 Dish dish = dishMapper.getById(dishId); shoppingCart.setName(dish.getName()); shoppingCart.setImage(dish.getImage()); shoppingCart.setAmount(dish.getPrice()); }else{ //本次添加到购物车的是套餐 Long setmealId = shoppingCartDTO.getSetmealId(); Setmeal setmeal = setmealMapper.getById(setmealId); shoppingCart.setName(setmeal.getName()); shoppingCart.setImage(setmeal.getImage()); shoppingCart.setAmount(setmeal.getPrice()); } shoppingCart.setNumber(1); shoppingCart.setCreateTime(LocalDateTime.now()); shoppingCartMapper.insert(shoppingCart); } } }
ShoppingCartMapper接口代码
@Mapper public interface ShoppingCartMapper { /** * 动态条件查询 * @param shoppingCart * @return */ List<ShoppingCart> list(ShoppingCart shoppingCart); /** * 根据id修改商品数量 * @param shoppingCart */ @Update("update shopping_cart set number = #{number} where id = #{id} ") void updateNumberById(ShoppingCart shoppingCart); /** * 插入购物车数据 * @param shoppingCart */ @Insert("insert into shopping_cart (name, image, user_id, dish_id, setmeal_id, dish_flavor, amount, create_time) VALUES " + "(#{name},#{image},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{amount},#{createTime})") void insert(ShoppingCart shoppingCart); }
ShoppingCartMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sky.mapper.ShoppingCartMapper"> <select id="list" resultType="com.sky.entity.ShoppingCart"> select * from shopping_cart <where> <if test="userId != null"> and user_id = #{userId} </if> <if test="setmealId != null"> and setmeal_id = #{setmealId} </if> <if test="dishId != null"> and dish_id = #{dishId} </if> <if test="dishFlavor != null"> and dish_flavor = #{dishFlavor} </if> </where> </select> </mapper>
查看购物车
ShoppingCartController代码
/** * 查看购物车 * @return */ @GetMapping("/list") @ApiOperation("查看购物车") public Result<List<ShoppingCart>> list(){ List<ShoppingCart> list = shoppingCartService.showShoppingCart(); return Result.success(list); }
ShoppingCartService接口代码
/** * 查看购物车 * @return */ List<ShoppingCart> showShoppingCart();
ShoppingCartServiceImpl代码
/** * 查看购物车 * @return */ public List<ShoppingCart> showShoppingCart() { Long userId = BaseContext.getCurrentId(); ShoppingCart shoppingCart = ShoppingCart.builder() .userId(userId) .build(); List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart); return list; }
测试成功显示
清空购物车
ShoppingCartController代码
/** * 清空购物车 * @return */ @DeleteMapping("/clean") @ApiOperation("清空购物车") public Result clean(){ shoppingCartService.cleanShoppingCart(); return Result.success(); }
ShoppingCartService接口代码
/** * 清空购物车 */ void cleanShoppingCart();
ShoppingCartServiceImpl代码
/** * 清空购物车 */ public void cleanShoppingCart() { //获取到当前的微信用户的id Long userId = BaseContext.getCurrentId(); shoppingCartMapper.cleanShoppingCart(userId); }
ShoppingCartMapper接口代码
/** * 根据用户id删除购物车数据 */ @Delete("delete from shopping_cart where user_id = #{userId}") void cleanShoppingCart(Long userId);
功能测试,成功
删除购物车中一个商品
产品原型
接口设计
数据模型
ShoppingCartController代码
/** * 删除购物车中一个商品 * @return */ @PostMapping("/sub") @ApiOperation("删除购物车中一个商品") public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){ log.info("删除购物车中一个商品,商品:{}", shoppingCartDTO); shoppingCartService.subShoppingCart(shoppingCartDTO); return Result.success(); }
ShoppingCartService接口代码
/** * 删除购物车中一个商品 * @param shoppingCartDTO */ void subShoppingCart(ShoppingCartDTO shoppingCartDTO);
ShoppingCartServiceImpl代码
/** * 删除购物车中一个商品 * @param shoppingCartDTO */ public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) { ShoppingCart shoppingCart = new ShoppingCart(); BeanUtils.copyProperties(shoppingCartDTO,shoppingCart); //设置查询条件,查询当前登录用户的购物车数据 shoppingCart.setUserId(BaseContext.getCurrentId()); List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart); if(list != null && list.size() > 0){ shoppingCart = list.get(0); Integer number = shoppingCart.getNumber(); if(number == 1){ //当前商品在购物车中的份数为1,直接删除当前记录 shoppingCartMapper.deleteById(shoppingCart.getId()); }else { //当前商品在购物车中的份数不为1,修改份数即可 shoppingCart.setNumber(shoppingCart.getNumber() - 1); shoppingCartMapper.updateNumberById(shoppingCart); } } }
ShoppingCartMapper接口代码
/** * 根据id删除购物车数据 * @param id */ @Delete("delete from shopping_cart where id = #{id}") void deleteById(Long id);
功能测试,成功