From d08f9db9ac3a88fab2bee82b27d760c8e0b150ef Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Wed, 7 Jan 2026 11:17:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(exception):=20=E5=B0=86=E5=86=85?= =?UTF-8?q?=E9=83=A8=E5=BC=82=E5=B8=B8=E7=B1=BB=E6=8A=BD=E5=8F=96=E4=B8=BA?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=BC=82=E5=B8=B8=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 SimpleServiceSupport 中的静态内部异常类抽取到 common 模块的独立异常类文件中,包括: - IdNotFoundException: ID 未找到异常 - NotComparableException: 不可比较异常 - NotCollectionException: 非集合异常 - NotStringException: 非字符串异常 --- .../common/exception/IdNotFoundException.java | 7 ++ .../exception/NotCollectionException.java | 7 ++ .../exception/NotComparableException.java | 7 ++ .../common/exception/NotStringException.java | 7 ++ .../jpa/service/SimpleServiceSupport.java | 72 ++----------------- 5 files changed, 32 insertions(+), 68 deletions(-) create mode 100644 spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/IdNotFoundException.java create mode 100644 spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotCollectionException.java create mode 100644 spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotComparableException.java create mode 100644 spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotStringException.java diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/IdNotFoundException.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/IdNotFoundException.java new file mode 100644 index 0000000..21b9aad --- /dev/null +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/IdNotFoundException.java @@ -0,0 +1,7 @@ +package com.lanyuanxiaoyao.service.template.common.exception; + +public class IdNotFoundException extends RuntimeException { + public IdNotFoundException(Long id) { + super("ID为 %d 的资源不存在".formatted(id)); + } +} diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotCollectionException.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotCollectionException.java new file mode 100644 index 0000000..b6fb1cc --- /dev/null +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotCollectionException.java @@ -0,0 +1,7 @@ +package com.lanyuanxiaoyao.service.template.common.exception; + +public class NotCollectionException extends RuntimeException { + public NotCollectionException(String variable) { + super("变量 %s 不是集合".formatted(variable)); + } +} diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotComparableException.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotComparableException.java new file mode 100644 index 0000000..d6be277 --- /dev/null +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotComparableException.java @@ -0,0 +1,7 @@ +package com.lanyuanxiaoyao.service.template.common.exception; + +public class NotComparableException extends RuntimeException { + public NotComparableException(String variable) { + super("变量 %s 不能比较".formatted(variable)); + } +} diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotStringException.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotStringException.java new file mode 100644 index 0000000..17a1274 --- /dev/null +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/exception/NotStringException.java @@ -0,0 +1,7 @@ +package com.lanyuanxiaoyao.service.template.common.exception; + +public class NotStringException extends RuntimeException { + public NotStringException(String variable) { + super("变量 %s 不是字符串".formatted(variable)); + } +} diff --git a/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/service/SimpleServiceSupport.java b/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/service/SimpleServiceSupport.java index 20cf6d1..4f95960 100644 --- a/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/service/SimpleServiceSupport.java +++ b/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/service/SimpleServiceSupport.java @@ -1,6 +1,10 @@ package com.lanyuanxiaoyao.service.template.jpa.service; import com.lanyuanxiaoyao.service.template.common.controller.Query; +import com.lanyuanxiaoyao.service.template.common.exception.IdNotFoundException; +import com.lanyuanxiaoyao.service.template.common.exception.NotCollectionException; +import com.lanyuanxiaoyao.service.template.common.exception.NotComparableException; +import com.lanyuanxiaoyao.service.template.common.exception.NotStringException; import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper; import com.lanyuanxiaoyao.service.template.common.service.Page; import com.lanyuanxiaoyao.service.template.common.service.SimpleService; @@ -594,72 +598,4 @@ public abstract class SimpleServiceSupport implemen repository.deleteBatchByIds(ids); } } - - /** - * ID未找到异常 - *

- * 当根据ID查询实体但实体不存在时抛出此异常。 - *

- */ - public static final class IdNotFoundException extends RuntimeException { - /** - * 构造函数(带ID参数) - * - * @param id 实体ID - */ - public IdNotFoundException(Long id) { - super("ID为 %d 的资源不存在".formatted(id)); - } - } - - /** - * 不可比较异常 - *

- * 当尝试对不可比较的字段或值执行比较操作时抛出此异常。 - *

- */ - public static final class NotComparableException extends RuntimeException { - /** - * 构造函数 - * - * @param variable 变量名称 - */ - public NotComparableException(String variable) { - super("变量 %s 不能比较".formatted(variable)); - } - } - - /** - * 非集合异常 - *

- * 当尝试对非集合类型的字段或值执行集合操作时抛出此异常。 - *

- */ - public static final class NotCollectionException extends RuntimeException { - /** - * 构造函数 - * - * @param variable 变量名称 - */ - public NotCollectionException(String variable) { - super("变量 %s 不是集合".formatted(variable)); - } - } - - /** - * 非字符串异常 - *

- * 当尝试对非字符串类型的字段或值执行字符串操作时抛出此异常。 - *

- */ - public static final class NotStringException extends RuntimeException { - /** - * 构造函数 - * - * @param variable 变量名称 - */ - public NotStringException(String variable) { - super("变量 %s 不是字符串".formatted(variable)); - } - } } \ No newline at end of file