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

苍穹外卖学习笔记(十)

二. 店铺营业状态设置

营业状态数据存储方式:基于Redis的字符串进行存储(1表示营业 0表示打烊)

代码

adminShopController.java

package com.sky.controller.admin;import com.sky.result.Result;
import com.sky.service.ShopService;
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.*;@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "商铺管理")
@Slf4j
public class ShopController {@Autowiredprivate ShopService shopService;/*** 设置商铺状态** @param status* @return*/@PutMapping("/{status}")@ApiOperation(value = "设置商铺状态")public Result setStatus(@PathVariable Integer status) {log.info("设置商铺状态:{}", status == 1 ? "营业中" : "打烊了");shopService.setStatus(status);return Result.success();}/*** 获取商铺状态** @return*/@GetMapping("/status")@ApiOperation(value = "获取商铺状态")public Result<Integer> getStatus() {Integer status = shopService.getStatus();log.info("获取商铺状态:{}", status == 1 ? "营业中" : "打烊了");return Result.success(status);}
}

userShopController.java

package com.sky.controller.user;import com.sky.result.Result;
import com.sky.service.ShopService;
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.*;@RestController("userShopController")
@RequestMapping("/user/shop")
@Api(tags = "商铺管理")
@Slf4j
public class ShopController {@Autowiredprivate ShopService shopService;/*** 获取商铺状态** @return*/@GetMapping("/status")@ApiOperation(value = "获取商铺状态")public Result<Integer> getStatus() {Integer status = shopService.getStatus();log.info("获取商铺状态:{}", status == 1 ? "营业中" : "打烊了");return Result.success(status);}
}

ShopService.java

package com.sky.service;public interface ShopService {/*** 设置商铺状态** @param status*/void setStatus(Integer status);/*** 获取商铺状态** @return*/Integer getStatus();
}

ShopServiceImpl.java

package com.sky.service.impl;import com.sky.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;/*** @author Jie.* @description: TODO* @date 2024/9/22* @version: 1.0*/
@Service
@Slf4j
public class ShopServiceImpl implements ShopService {public static final String SHOP_STATUS = "SHOP_STATUS";@Autowiredprivate RedisTemplate redisTemplate;/*** 设置商铺状态** @param status*/@Overridepublic void setStatus(Integer status) {redisTemplate.opsForValue().set(SHOP_STATUS, status);}/*** 获取商铺状态** @return*/@Overridepublic Integer getStatus() {Integer shopStatus = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS);return shopStatus == null ? 0 : shopStatus;}
}

注意

记得把RedisConfiguration中的value的序列化器注释掉,否则会导致Redis的类型转换问题

Swagger文档优化

WebMvcConfiguration.java

package com.sky.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.sky.interceptor.JwtTokenAdminInterceptor;
import com.sky.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;import java.util.List;/*** 配置类,注册web层相关组件*/
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {@Autowiredprivate JwtTokenAdminInterceptor jwtTokenAdminInterceptor;/*** 注册自定义拦截器** @param registry*/protected void addInterceptors(InterceptorRegistry registry) {log.info("开始注册自定义拦截器...");registry.addInterceptor(jwtTokenAdminInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/employee/login");}/*** 通过knife4j生成接口文档** @return*/@Beanpublic Docket docket1() {ApiInfo apiInfo = new ApiInfoBuilder().title("苍穹外卖项目接口文档").version("2.0").description("苍穹外卖项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("admin").apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin")).paths(PathSelectors.any()).build();return docket;}@Beanpublic Docket docket2() {ApiInfo apiInfo = new ApiInfoBuilder().title("苍穹外卖项目接口文档").version("2.0").description("苍穹外卖项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("user").apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller.user")).paths(PathSelectors.any()).build();return docket;}/*** 设置静态资源映射** @param registry*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}/*** 配置消息转换器** @param converters*/@Overrideprotected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {log.info("配置消息转换器...");MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();converter.setObjectMapper(new JacksonObjectMapper());converters.add(0, converter);}/*** 配置 MyBatis-Plus 的全局设置*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {log.info("配置 MyBatis-Plus 的全局设置...");MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());// 多租户插件
//        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor());// 防止全表更新与删除插件
//        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());return interceptor;}
}

http://www.mrgr.cn/news/34582.html

相关文章:

  • RabbitMQ集群搭建
  • java八股笔记-1-java基础
  • WordPress 2024主题实例镜像
  • go语言中的log 包详解
  • 为什么跳转到外部页面不涉及跨域?
  • Hadoop积累---Hadoop判断job和map的开始和结束(带源码)
  • 什么是反射,反射用途,spring哪些地方用到了反射,我们项目中哪些地方用到了反射
  • Mysql进阶
  • 一篇关于网络的文章
  • 研一奖学金计划2024/9/23有感
  • BEV学习--Nuscenes数据集解读
  • 用Flowise+OneAPI+Ollama做一个在线翻译工作流
  • 如何登录通义灵码,快速开启AI编码之旅?
  • 基于单片机巡迹避障智能小车系统
  • 设计模式相关知识
  • SpringBoot+Aop+注解方式 实现多数据源动态切换
  • vcruntime140_1.dll无法继续执行代码的6种解决方法
  • Undet for sketchup 2023.3注册机 支持草图大师sketchup2021-2022-2023
  • 数据结构之图的遍历
  • MySQL | 使用 HAVING 子句进行高级数据筛选
  • 数据链路层协议 —— 以太网协议
  • 如何快速免费搭建自己的Docker私有镜像源来解决Docker无法拉取镜像的问题(搭建私有镜像源解决群晖Docker获取注册表失败的问题)
  • 全栈开发(四):使用springBoot3+mybatis-plus+mysql开发restful的增删改查接口
  • 代码随想录算法训练营Day11
  • JDK7u21 HashMap版
  • C++之STL—vector容器进阶篇