一、== 和 equals() 的区别
1. == 运算符
== 运算符用于比较:
- 基本数据类型 (Primitive Types):比较的是值。
- 引用类型 (Reference Types):比较的是内存地址,即是否指向同一个对象。
2025/12/2大约 3 分钟
== 和 equals() 的区别== 运算符== 运算符用于比较:
配置类就是用来创建和管理 Bean 的类。,提前配置好一些 Bean,方便在应用中使用。直接@Autowired 注入即可。
在 Spring Boot 中,相当于以前的 XML 配置文件。
@Bean 注册 Bean。1 ) type(必须) : commit 的类别,只允许使用下面几个标识:- feat : 新功能
/**
* 统一结果封装类
* @param <T> 返回数据类型
*/
@Data
public class Result<T> implements Serializable {
private Integer code; // 状态码
private String msg; // 返回信息
private T data; // 返回数据
private Result() {}
// 成功返回(无数据)
public static <T> Result<T> success() {
Result<T> result = new Result<>();
result.code = 200;
result.msg = "操作成功";
return result;
}
// 成功返回(带数据)
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>();
result.code = 200;
result.msg = "操作成功";
result.data = data;
return result;
}
// 成功返回(自定义消息和数据)
public static <T> Result<T> success(String msg, T data) {
Result<T> result = new Result<>();
result.code = 200;
result.msg = msg;
result.data = data;
return result;
}
// 失败返回(默认消息)
public static <T> Result<T> error() {
Result<T> result = new Result<>();
result.code = 500;
result.msg = "操作失败";
return result;
}
// 失败返回(自定义消息)
public static <T> Result<T> error(String msg) {
Result<T> result = new Result<>();
result.code = 500;
result.msg = msg;
return result;
}
// 失败返回(自定义状态码和消息)
public static <T> Result<T> error(Integer code, String msg) {
Result<T> result = new Result<>();
result.code = code;
result.msg = msg;
return result;
}
// 自定义返回
public static <T> Result<T> build(Integer code, String msg, T data) {
Result<T> result = new Result<>();
result.code = code;
result.msg = msg;
result.data = data;
return result;
}
}
/**
* 全局异常处理器,处理项目中抛出的业务异常
*/
@Slf4j
@RestControllerAdvice // 统一异常拦截(返回 JSON)
public class GlobalExceptionHandler {
/**
* 专门处理我们自定义的业务异常
*/
@ExceptionHandler(BaseException.class)
public Result<Void> handleBaseException(BaseException ex) {
log.warn("业务异常: {}", ex.getMessage());
// code 允许为 null,因此要判断
if (ex.getCode() != null) {
return Result.error(ex.getCode(), ex.getMessage());
}
// 如果没有 code,默认 500
return Result.error(ex.getMessage());
}
/**
* 处理所有未捕获的异常(兜底)
*/
@ExceptionHandler(Exception.class)
public Result<Void> handleException(Exception ex) {
log.error("系统异常: ", ex);
return Result.error(500, "系统异常,请联系管理员");
}
}
在 Web 开发中,如果不处理异常:
解决方案:使用自定义异常 + 全局异常处理器(@RestControllerAdvice)统一响应格式。
统一定一个返回模板,让前后端都用同一种 JSON 格式: