苍穹外卖第一天 搭建前端后端环境 先将代码提交到本地 然后在git上创建仓库 https://gitee.com/lh03062021/sky-take-out.git 点击push 定义远程仓库 把复制好的粘贴上去 就可以推送成功了。 导入sql文件,编译父项目

  1. nginx反向代理,将前端发送的动态请求由nginx转发到后端服务器 好处:负载均衡,提高访问速度,保证后端服务安全 image.png image.png

2.完善登录功能,修改明文密码,改成md5加密(不能解密)

3.使用swagger image.png 2.配置类加入配置 3.设置静态资源映射,否则接口文档页面无法访问 package com.sky.config;

import com.sky.interceptor.JwtTokenAdminInterceptor; 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.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;

/**

  • 配置类,注册web层相关组件 */ @Configuration @Slf4j public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired private JwtTokenAdminInterceptor jwtTokenAdminInterceptor; /**

    • 通过knife4j生成接口文档
    • @return */ @Bean public Docket docket() { ApiInfo apiInfo = new ApiInfoBuilder() .title("苍穹外卖项目接口文档") .version("2.0") .description("苍穹外卖项目接口文档") .build(); Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() .apis(RequestHandlerSelectors.basePackage("com.sky.controller")) .paths(PathSelectors.any()) .build(); return docket; }

    /**

    • 设置静态资源映射
    • @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }

image.png

## - 32集 自定义注解开发,在工作中经常用到insertid,inserttime,updateid,updatetime

  1. 创建枚举型数据 insert或update
/**
 * 数据库操作类型
 */
public enum OperationType {

    /**
     * 更新操作
     */
    UPDATE,

    /**
     * 插入操作
     */
    INSERT

}

@Target:用于描述注解的使用范围 @Rention:表示需要在什么什么级别保存该注释信息,用于描述注解的生命周期。(source<class<runtime) 2.创建注解接口类

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
 //只有数据库更新和插入操作
    OperationType value();

}

3.创建aspect

package com.sky.aspect;


import com.sky.anocation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author DHC
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {


    //定义切点
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.anocation.AutoFill)")
    public void PointCut() {

    }

    @Before("PointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段填充...");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value(); //获得数据库操作类型


        Object[] args = joinPoint.getArgs();
        if (args == null || args.length==0) {
          return ;
        }

        Object entity = args[0];
        Long currentId= BaseContext.getCurrentId();
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date a =new Date();
        String s = simpleDateFormat.format(a);

        if(operationType == OperationType.INSERT){
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, String.class);
                Method insertId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, String.class);
                Method updateId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                setCreateTime.invoke(entity,s);
                insertId.invoke(entity,currentId);
                updateTime.invoke(entity,s);
                updateId.invoke(entity,currentId);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }else if(operationType == OperationType.UPDATE){
            try {
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, String.class);
                Method updateId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                updateTime.invoke(entity,s);
                updateId.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }


}