spring-boot配置

持久层: 配置数据库 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;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值