exception包
GlobalExceptionHandler 。全局的异常处理器。
import com.example.backendmanagebackendlearn.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ServiceException.class)
@ResponseBody
public Result handle(ServiceException se){
return Result.error(se.getCode(), se.getMessage());
}
}
ServiceException 。自定义的异常。
import lombok.Getter;
@Getter
public class ServiceException extends RuntimeException {
private String code;
public ServiceException(String code, String msg) {
super(msg);
this.code = code;
}
}
utils包
CodeGenerator。代码生成器。mabatis-plus的。
import java.util.Collections;
public class CodeGenerator {
public static void main(String[] args) {
generate();
}
private static void generate() {
FastAutoGenerator.create("jdbc:mysql://localhost:3306/qing?serverTimezone=GMT%2b8", "root", "123456")
.globalConfig(builder -> {
builder.author("青哥哥")
.enableSwagger()
.fileOverride()
.outputDir("D:\\代码\\小白做毕设2022\\springboot\\src\\main\\java\\");
})
.packageConfig(builder -> {
builder.parent("com.qingge.springboot")
.moduleName(null)
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\代码\\小白做毕设2022\\springboot\\src\\main\\resources\\mapper\\"));
})
.strategyConfig(builder -> {
builder.entityBuilder().enableLombok();
builder.controllerBuilder().enableHyphenStyle()
.enableRestStyle();
builder.addInclude("t_comment")
.addTablePrefix("t_", "sys_");
})
.execute();
}
}
TokenUtils 。token工具类,获取token的方法,获取当前线程的请求的user
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@Component
public class TokenUtils {
private static IUserService staticUserService;
@Resource
private IUserService userService;
@PostConstruct
public void setUserService() {
staticUserService = userService;
}
public static String genToken(String userId, String sign) {
return JWT.create().withAudience(userId)
.withExpiresAt(DateUtil.offsetHour(new Date(), 2))
.sign(Algorithm.HMAC256(sign));
}
public static User getCurrentUser() {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token");
if (StrUtil.isNotBlank(token)) {
String userId = JWT.decode(token).getAudience().get(0);
return staticUserService.getById(Integer.valueOf(userId));
}
} catch (Exception e) {
return null;
}
return null;
}
}
application.yml
- 服务器的设置
- 数据源的配置,mysql、redis
- 配置允许的文件传输大小
- mybtais的配置
- mybatis-plus的配置
server:
ip: localhost
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/qing?serverTimezone=GMT%2b8
username: root
password: root
redis:
host: 127.0.0.1
port: 6379
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
mybatis:
mapper-locations: classpath:mapper/*.xml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
files:
upload:
path: D:\Projects\springboot-projects\pre\assets\
|