xyz.zhouxy.plusone
plusone-commons
diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java
index c727bc7..11a386d 100644
--- a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java
+++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataNotExistException.java
@@ -6,12 +6,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
/**
* 需要时,当查询数据不存在时抛出的异常
*
- *
- * 暂时先这样,后续完善异常体系时可能会更改。
- *
- *
* @author ZhouXY
- * @see xyz.zhouxy.plusone.util.AssertResult
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
public class DataNotExistException extends PlusoneException {
diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataOperationNumberException.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataOperationResultException.java
similarity index 67%
rename from plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataOperationNumberException.java
rename to plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataOperationResultException.java
index b121bdc..79dc910 100644
--- a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataOperationNumberException.java
+++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/exception/DataOperationResultException.java
@@ -4,7 +4,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
- * 需要时,当数据操作的行数不符合预期时抛出的异常
+ * 需要时,当数据操作的结果不符合预期时抛出的异常
*
*
* 暂时先这样,后续完善异常体系时可能会更改。
@@ -14,18 +14,18 @@ import org.springframework.web.bind.annotation.ResponseStatus;
* @see xyz.zhouxy.plusone.util.AssertResult
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-public class DataOperationNumberException extends PlusoneException {
+public class DataOperationResultException extends PlusoneException {
@java.io.Serial
private static final long serialVersionUID = -9220765735990318186L;
public static final int ERROR_CODE = 4110200;
- public DataOperationNumberException() {
- super(ERROR_CODE, "数据操作的行数不符合预期");
+ public DataOperationResultException() {
+ super(ERROR_CODE, "数据操作结果不符合预期");
}
- public DataOperationNumberException(String message) {
+ public DataOperationResultException(String message) {
super(ERROR_CODE, message);
}
}
diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java
index 0e66185..5c08e69 100644
--- a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java
+++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java
@@ -1,10 +1,10 @@
package xyz.zhouxy.plusone.util;
-
import xyz.zhouxy.plusone.exception.DataNotExistException;
-import xyz.zhouxy.plusone.exception.DataOperationNumberException;
+import xyz.zhouxy.plusone.exception.DataOperationResultException;
import java.util.Objects;
+import java.util.function.Supplier;
/**
* 对数据库执行结果进行判断
@@ -17,51 +17,34 @@ public final class AssertResult {
throw new IllegalStateException("Utility class");
}
- public static void update(boolean expression) {
- if (!expression) {
- throw new DataOperationNumberException();
+ public static void isTrue(boolean condition, Supplier e) throws E {
+ if (!condition) {
+ throw e.get();
}
}
- public static void update(boolean expression, String message) {
- if (!expression) {
- throw new DataOperationNumberException(message);
- }
+ public static void equals(T result, T expectedValue) {
+ isTrue(Objects.equals(result, expectedValue), DataOperationResultException::new);
}
- public static void update(Object i, int expectedValue) {
- if (!Objects.equals(i, expectedValue)) {
- throw new DataOperationNumberException();
- }
+ public static void equals(T result, T expectedValue, String msgTemplate, Object... args) {
+ isTrue(!Objects.equals(result, expectedValue),
+ () -> new DataOperationResultException(String.format(msgTemplate, args)));
}
- public static void update(Object i, int expectedValue, String format) {
- if (!Objects.equals(i, expectedValue)) {
- throw new DataOperationNumberException(String.format(format, i));
- }
+ public static void updateOneRow(int i) {
+ equals(i, 1);
}
- public static void exist(boolean expression) {
- if (!expression) {
- throw new DataNotExistException();
- }
- }
-
- public static void exist(boolean expression, String message) {
- if (!expression) {
- throw new DataNotExistException(message);
- }
+ public static void updateOneRow(int i, String format, Object... args) {
+ equals(i, 1, format, args);
}
public static void nonNull(Object obj) {
- if (Objects.isNull(obj)) {
- throw new DataNotExistException();
- }
+ isTrue(Objects.nonNull(obj), DataNotExistException::new);
}
public static void nonNull(Object obj, String message) {
- if (Objects.isNull(obj)) {
- throw new DataNotExistException(message);
- }
+ isTrue(Objects.nonNull(obj), () -> new DataNotExistException(message));
}
}
diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java
index f913a4b..e73ae82 100644
--- a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java
+++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IRepository.java
@@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.domain;
import java.io.Serializable;
+import java.util.Optional;
/**
* Repository 基础接口
@@ -10,7 +11,7 @@ import java.io.Serializable;
*/
public interface IRepository, ID extends Serializable> {
- T find(ID id);
+ Optional find(ID id);
T save(T entity);
diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/ValidatableStringRecord.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/ValidatableStringRecord.java
index 7a0e8ec..a44c675 100644
--- a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/ValidatableStringRecord.java
+++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/ValidatableStringRecord.java
@@ -1,5 +1,7 @@
package xyz.zhouxy.plusone.domain;
+import java.util.Optional;
+
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
@@ -31,4 +33,8 @@ public abstract class ValidatableStringRecord implements IValueObject {
public String toString() {
return value;
}
+
+ public static String getValueOrNull(Optional extends ValidatableStringRecord> s) {
+ return s.map(ValidatableStringRecord::value).orElse(null);
+ }
}
diff --git a/plusone-basic/plusone-basic-infrastructure/pom.xml b/plusone-basic/plusone-basic-infrastructure/pom.xml
index 548d37b..a195a10 100644
--- a/plusone-basic/plusone-basic-infrastructure/pom.xml
+++ b/plusone-basic/plusone-basic-infrastructure/pom.xml
@@ -1,5 +1,7 @@
-
+
4.0.0
xyz.zhouxy
@@ -84,6 +86,11 @@
com.tencentcloudapi
tencentcloud-sdk-java-sms
+
+