当前位置: 首页 > news >正文

学网站开发在大学网站建设mng

学网站开发在大学,网站建设mng,网站建设中目录是什么意思,舟山注册公司一、缓存菜品 通过缓存的方式提高查询性能 1.1问题说明 大量的用户访问导致数据库访问压力增大#xff0c;造成系统响应慢#xff0c;用户体验差 1.2 实现思路 优先查询缓存#xff0c;如果缓存没有再去查询数据库#xff0c;然后载入缓存 将菜品集合序列化后缓存入red… 一、缓存菜品 通过缓存的方式提高查询性能 1.1问题说明 大量的用户访问导致数据库访问压力增大造成系统响应慢用户体验差 1.2 实现思路 优先查询缓存如果缓存没有再去查询数据库然后载入缓存 将菜品集合序列化后缓存入redis  key为每个分类的id 1.3 代码开发缓存菜品 import com.sky.constant.StatusConstant; import com.sky.entity.Dish; import com.sky.result.Result; import com.sky.service.DishService; import com.sky.vo.DishVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List;RestController(userDishController) RequestMapping(/user/dish) Slf4j Api(tags C端-菜品浏览接口) public class DishController {Autowiredprivate DishService dishService;Autowiredprivate RedisTemplate redisTemplate;/*** 根据分类id查询菜品** param categoryId* return*/GetMapping(/list)ApiOperation(根据分类id查询菜品)public ResultListDishVO list(Long categoryId) {//构造redis中的key规则dish_分类idString key dish_categoryId;//查询redis中是否存在菜品数据ListDishVO list (ListDishVO) redisTemplate.opsForValue().get(key);if (list!nulllist.size()0){//如果存在直接返回无需查询数据库return Result.success(list);}//如果不存在查询数据库将查询到的数据放入redis中Dish dish new Dish();dish.setCategoryId(categoryId);dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品list dishService.listWithFlavor(dish);redisTemplate.opsForValue().set(key,list);return Result.success(list);}}1.4 代码开发清除缓存 为什么要清除缓存 为了保证数据的一致性数据库修改后缓存中的数据并没有发生改变用户再次请求后不会请求到新修改的数据而是过期的数据所以要清除缓存。 什么时候清除缓存 后端修改数据后对缓存进行及时的清除 /*** 统一清除缓存数据* param pattern*/private void cleanCache(String pattern){Set keys redisTemplate.keys(pattern);redisTemplate.delete(keys);}} /*** 启用禁用菜品* 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 // Set keys redisTemplate.keys(dish_); // redisTemplate.delete(keys);cleanCache(dish_*);return Result.success();}二、缓存套餐基于SpringCache 2.1 SpringCache 被写死了没有意义 CachePut(cacheNames userCache,key abc)//如果使用SpringCache缓存数据key的生成userCache::abc //生成的key 与cacheNameskey有关系 动态生成key SpEL表达式 CachePut(cacheNames userCache,key #user.id)//如果使用SpringCache缓存数据key的生成userCache::#user.id 2.2 入门案例 1.CachePut将方法返回值放到缓存中 // CachePut(cacheNames userCache,key #user.id)//如果使用SpringCache缓存数据key的生成userCache::abcPostMapping // CachePut(cacheNames userCache,key #result.id)//对象导航 // CachePut(cacheNames userCache,key #p0.id)//#p0代表第一个参数 // CachePut(cacheNames userCache,key #a0.id)CachePut(cacheNames userCache,key #root.args[0].id)//#root.args[0]代表第一个参数public User save(RequestBody User user){userMapper.insert(user);return user;} 2.Cacheable在方法执行前先查询缓存如果缓存中有该数据直接返回缓存中的数据。如果没有在方法执行后再将返回值放入缓存中 GetMappingCacheable(cacheNames userCache,key #id)public User getById(Long id){User user userMapper.getById(id);return user;} Cacheable 不能使用#result.id这种方式设置key的值  3.CacheEvict 将一条或者多条数据从缓存中删除 删除单条数据 CacheEvict(cacheNames userCache,key #id )DeleteMappingpublic void deleteById(Long id){userMapper.deleteById(id);} 删除多条数据allEntries true CacheEvict(cacheNames userCache,allEntries true)DeleteMapping(/delAll)public void deleteAll(){userMapper.deleteAll();}2.3实现思路 2.4代码开发 import com.sky.constant.StatusConstant; import com.sky.entity.Setmeal; import com.sky.result.Result; import com.sky.service.SetmealService; import com.sky.vo.DishItemVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List;RestController(userSetmealController) RequestMapping(/user/setmeal) Api(tags C端-套餐浏览接口) public class SetmealController {Autowiredprivate SetmealService setmealService;/*** 条件查询** param categoryId* return*/Cacheable(cacheNames setmealCache,key #categoryId)//key :setmealCache::100GetMapping(/list)ApiOperation(根据分类id查询套餐)public ResultListSetmeal list(Long categoryId) {Setmeal setmeal new Setmeal();setmeal.setCategoryId(categoryId);setmeal.setStatus(StatusConstant.ENABLE);ListSetmeal list setmealService.list(setmeal);return Result.success(list);}/*** 根据套餐id查询包含的菜品列表** param id* return*/GetMapping(/dish/{id})ApiOperation(根据套餐id查询包含的菜品列表)public ResultListDishItemVO dishList(PathVariable(id) Long id) {ListDishItemVO list setmealService.getDishItemById(id);return Result.success(list);} }import com.github.pagehelper.Page; import com.sky.dto.SetmealDTO; import com.sky.dto.SetmealPageQueryDTO; import com.sky.result.PageResult; import com.sky.result.Result; import com.sky.service.SetmealService; import com.sky.vo.SetmealVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.web.bind.annotation.*;import java.util.List;/*** 套餐管理*/ RestController RequestMapping(/admin/setmeal) Api(tags 套餐管理相关接口) Slf4j public class SetmealController {Autowiredprivate SetmealService setmealService;CacheEvict(cacheNames setmealCache,key setmealDTO.categoryId)PostMappingApiOperation(新增套餐接口)public Result save(RequestBody SetmealDTO setmealDTO){log.info(新增套餐{},setmealDTO);setmealService.saveWithDish(setmealDTO);return Result.success();}/*** 套餐分页查询* param setmealPageQueryDTO* return*/GetMapping(/page)ApiOperation(套餐分页查询)public ResultPageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){log.info(套餐分页查询{},setmealPageQueryDTO);PageResult pageResult setmealService.pageQuery(setmealPageQueryDTO);return Result.success(pageResult);}/*** 批量删除套餐* param ids* return*/CacheEvict(cacheNames setmealCache,allEntries true)DeleteMappingApiOperation(批量删除菜品)public Result delete(RequestParam ListLong ids){log.info(批量删除套餐{},ids);setmealService.deleteBatch(ids);return Result.success();}/*** 根据id查询套餐用于修改页面回显数据** param id* return*/GetMapping(/{id})ApiOperation(根据id查询套餐)public ResultSetmealVO getById(PathVariable Long id) {SetmealVO setmealVO setmealService.getByIdWithDish(id);return Result.success(setmealVO);}/*** 修改套餐** param setmealDTO* return*/CacheEvict(cacheNames setmealCache,allEntries true)PutMappingApiOperation(修改套餐)public Result update(RequestBody SetmealDTO setmealDTO) {setmealService.update(setmealDTO);return Result.success();}/*** 套餐起售停售* param status* param id* return*/CacheEvict(cacheNames setmealCache,allEntries true)PostMapping(/status/{status})ApiOperation(套餐起售停售)public Result startOrStop(PathVariable Integer status, Long id) {setmealService.startOrStop(status, id);return Result.success();}}三、添加购物车 3.1需求分析和设计 3.2 代码开发 3.2.1Controller import com.sky.dto.ShoppingCartDTO; import com.sky.result.Result; import com.sky.service.ShoppingCartService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;RestController RequestMapping(/user/shoppingCart) Api(tags C端购物车相关接口) Slf4j public class ShoppingCartController {Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加购物车* param shoppingCartDTO* return*/PostMapping(/add)ApiOperation(添加购物车)public Result add(RequestBody ShoppingCartDTO shoppingCartDTO){log.info(添加购物车商品信息为,shoppingCartDTO);shoppingCartService.addShoppingCart(shoppingCartDTO);return Result.success();}}3.2.2 Serveice层 import com.sky.context.BaseContext; import com.sky.dto.ShoppingCartDTO; import com.sky.entity.Dish; import com.sky.entity.Setmeal; import com.sky.entity.ShoppingCart; import com.sky.mapper.DishMapper; import com.sky.mapper.SetmealMapper; import com.sky.mapper.ShoppingCartMapper; import com.sky.service.ShoppingCartService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.time.LocalDateTime; import java.util.List;Service Slf4j public class ShoppingCartServiceImpl implements ShoppingCartService {Autowiredprivate ShoppingCartMapper shoppingCartMapper;Autowiredprivate DishMapper dishMapper;Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车* param shoppingCartDTO*/Overridepublic void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {//判断当前加入到购物车中的商品是否已经存在了ShoppingCart shoppingCart new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);Long userId BaseContext.getCurrentId();shoppingCart.setUserId(userId);ListShoppingCart list shoppingCartMapper.list(shoppingCart);//如果已经存在了只需要将其数量加1if (list !null list.size()0){ShoppingCart cart list.get(0);cart.setNumber(cart.getNumber()1);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()); // shoppingCart.setNumber(1); // shoppingCart.setCreateTime(LocalDateTime.now()); //}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());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}} }四、查看购物车 4.1 需求分析和设计 4.2 代码开发 4.2.1Controller层 /*** 查看购物车* return*/GetMapping(/list)ApiOperation(查看购物车)public ResultListShoppingCart list(){ListShoppingCart list shoppingCartService.showShoppingCart();return Result.success(list);} 4.2.2 Service层 /*** 查看购物车* return*/Overridepublic ListShoppingCart showShoppingCart() {//获取当前微信用户的idLong userId BaseContext.getCurrentId();ShoppingCart shoppingCart ShoppingCart.builder().userId(userId).build();ListShoppingCart list shoppingCartMapper.list(shoppingCart);return list;} 五、清空购物车 5.1 需求分析和设计 5.2 代码开发 5.2.1 Controller层 /*** 清空购物车* return*/DeleteMapping(/clean)public Result clean(){shoppingCartService.cleanShoppingCart();return Result.success();}5.2.2 Service层 /*** 清空购物车*/Overridepublic void cleanShoppingCart() {Long userId BaseContext.getCurrentId();shoppingCartMapper.deleteByUserId(userId);} 5.2.3 Mapper层 /*** 根据用户id删除购物车数据* param userId*/Delete(delete from shopping_cart where user_id #{userId})void deleteByUserId(Long userId);
http://www.dnsts.com.cn/news/70826.html

相关文章:

  • wordpress 站点地址 wordpress地址株洲信息网
  • 网站拒绝了您的访问凤城市网站建设
  • 求个网站你懂我的意思吗怎么建好网站
  • 《电子商务网站开发与管理》域名解析后怎么建网站
  • 哈尔滨市延寿建设局网站网站子网页设计
  • 建一个快讯网站要多少钱手工制作灯笼视频教程
  • 作风建设 宣讲家网站最轻快的wordpress主題
  • 如何建网站的步骤邢台头条新闻最新
  • php网站中水印怎么做的可以设计房子布局的软件
  • 杭州做网站的网络公司有哪些荥阳网页设计
  • 怎样做卡盟网站株洲网站建设
  • 网站开发更新记录建设银行网站进不去
  • 做网站什么用武宁县建设工程招标公告门户网站
  • 宠物网站建设总结网络规划设计师视频百度网盘
  • 河南省住房和建设厅网站首页wordpress 改登录界面
  • 可以加外链的网站所有外包网站
  • 27岁了想学网站建设wordpress 如何获得数据库数据
  • 青岛建站模板制作网站建设知识点有哪些漏缺
  • 应用软件开发需要学什么网页设计优化网站建设可以吗
  • 亦庄网站建设价格深圳百度推广电话
  • 网站建设应该考虑哪些问题中资建筑信息平台
  • 在线flash相册网站源码郯城县住房和城乡建设局网站
  • wordpress建站 客户端国外网页素材网站
  • 怎样做好网站用户体验家居企业网站建设策划
  • 哈尔滨网站优化推广公司经典广告
  • 做旅游视频网站wordpress htaccess文件
  • 做网站的zk啥中企动力科技股份有限公司深圳分公司
  • 珠海网站设计价格印度做网站
  • 网站页面高度网站前端设计培训
  • 轻量应用服务器搭建网站wordpress制作网站教程