持久层: 配置数据库 application:
Spring系列框架的配置
spring:
# Profile配置
profiles:
# 激活Profile配置
active: dev
# 连接数据库的相关配置
datasource:
# 使用的数据库连接池类型
type: com.alibaba.druid.pool.DruidDataSource
# 关于jackson的配置
jackson:
# 响应的JSON中默认包含哪些属性
default-property-inclusion: non_null
Mybatis相关配置
mybatis:
# 用于配置SQL语句的XML文件的位置
mapper-locations: classpath:mapper/*.xml
Knife4j配置
knife4j:
# 是否开启增强模式
enable: true
application-dev
Spring系列框架的配置
spring:
# 连接数据库的配置
datasource:
# 连接数据库的URL
url: jdbc:mysql://localhost:3306/mall_ams?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
# 登录数据库的用户名
username: root
# 登录数据库的密码
password: cheng123
日志相关配置
logging:
# 日志的显示级别
level:
# 根包的日志显示级别
cn.tedu.boot.demo: trace
mybatis配置
@Configuration
@MapperScan("cn.tedu.boot.demo.mapper")
public class MybatisConfig {
}
写mapper.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="cn.tedu.boot.demo.mapper.AdminMapper">
</mapper>
config跨域请求
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
全局异常处理
@RestControllerAdvice
public class GlobalExceptionHandler {
//自定义异常
@ExceptionHandler(ServiceException.class)
public JsonResult<Void> handleServiceException(ServiceException e) {
if (e instanceof UsernameDuplicateException) {
return JsonResult.fail(State.ERR_USERNAME, e.getMessage());
} else {
return JsonResult.fail(State.ERR_INSERT, e.getMessage());
}
}
//常规异常
@ExceptionHandler(BindException.class)
public JsonResult<Void> handleBindException(BindException e) {
BindingResult bindingResult = e.getBindingResult();
String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
return JsonResult.fail(State.ERR_BAD_REQUEST, defaultMessage);
}
}
ex类写自定义异常
//总异常类
public class ServiceException extends RuntimeException {
...
//重写五个方法
}
util密码加密方法(固定格式)
@Component
public class PasswordEncoder {
public String encode(String rawPassword) {
// 加密过程
// 1. 使用MD5算法
// 2. 使用随机的盐值
// 3. 循环5次
// 4. 盐的处理方式为:盐 + 原密码 + 盐 + 原密码 + 盐
// 注意:因为使用了随机盐,盐值必须被记录下来,本次的返回结果使用$分隔盐与密文
String salt = UUID.randomUUID().toString().replace("-", "");
String encodedPassword = rawPassword;
for (int i = 0; i < 5; i++) {
encodedPassword = DigestUtils.md5DigestAsHex(
(salt + encodedPassword + salt + encodedPassword + salt).getBytes());
}
return salt + encodedPassword;
}
public boolean matches(String rawPassword, String encodedPassword) {
String salt = encodedPassword.substring(0, 32);
String newPassword = rawPassword;
for (int i = 0; i < 5; i++) {
newPassword = DigestUtils.md5DigestAsHex(
(salt + newPassword + salt + newPassword + salt).getBytes());
}
newPassword = salt + newPassword;
return newPassword.equals(encodedPassword);
}
}
web包中写JsonResult和枚举类State
package cn.tedu.boot.demo.web;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T> implements Serializable {
// 状态码,例如:200
private Integer state;
// 消息,例如:"登录失败,用户名不存在"
@JsonInclude(JsonInclude.Include.NON_NULL)
private String message;
// 数据
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
private JsonResult() {}
public static JsonResult<Void> ok() {
// JsonResult jsonResult = new JsonResult();
// jsonResult.setState(1);
// return jsonResult;
return ok(null);
}
public static <T> JsonResult<T> ok(T data) {
JsonResult<T> jsonResult = new JsonResult<>();
jsonResult.setState(State.OK.getValue());
jsonResult.setData(data);
return jsonResult;
}
public static JsonResult<Void> fail(State state, String message) {
JsonResult<Void> jsonResult = new JsonResult<>();
jsonResult.setState(state.getValue());
jsonResult.setMessage(message);
return jsonResult;
}
}
package cn.tedu.boot.demo.web;
public enum State {
OK(200),
ERR_USERNAME(201),
ERR_PASSWORD(202),
ERR_BAD_REQUEST(400),
ERR_INSERT(500);
private Integer value;
State(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
}
yml配置文件
Spring系列框架的配置
spring:
# Profile配置
profiles:
# 激活Profile配置
active: dev
# 连接数据库的相关配置
datasource:
# 使用的数据库连接池类型
type: com.alibaba.druid.pool.DruidDataSource
# 关于jackson的配置
jackson:
# 响应的JSON中默认包含哪些属性
default-property-inclusion: non_null
Mybatis相关配置
mybatis:
# 用于配置SQL语句的XML文件的位置
mapper-locations: classpath:mapper/*.xml
Spring系列框架的配置
spring:
# 连接数据库的配置
datasource:
# 连接数据库的URL
url: jdbc:mysql://localhost:3306/mall_ams?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
# 登录数据库的用户名
username: root
# 登录数据库的密码
password: cheng123
日志相关配置
logging:
# 日志的显示级别
level:
# 根包的日志显示级别
cn.tedu.boot.demo: trace
redis配置
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Serializable> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.json());
return redisTemplate;
}
}