重构 DAO 基础代码。
This commit is contained in:
@@ -4,7 +4,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
/**
|
||||
* 需要时,当数据操作的行数不符合预期时抛出的异常
|
||||
* 需要时,当数据操作的结果不符合预期时抛出的异常
|
||||
*
|
||||
* <p>
|
||||
* 暂时先这样,后续完善异常体系时可能会更改。
|
||||
@@ -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);
|
||||
}
|
||||
}
|
@@ -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,59 +17,34 @@ public final class AssertResult {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static void update(boolean expression) {
|
||||
if (!expression) {
|
||||
throw new DataOperationNumberException();
|
||||
public static <E extends Throwable> void isTrue(boolean condition, Supplier<E> e) throws E {
|
||||
if (!condition) {
|
||||
throw e.get();
|
||||
}
|
||||
}
|
||||
|
||||
public static void update(boolean expression, String message) {
|
||||
if (!expression) {
|
||||
throw new DataOperationNumberException(message);
|
||||
}
|
||||
public static <T> 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 update(Object i, int expectedValue, String format) {
|
||||
if (!Objects.equals(i, expectedValue)) {
|
||||
throw new DataOperationNumberException(String.format(format, i));
|
||||
}
|
||||
public static <T> void equals(T result, T expectedValue, String msgTemplate, Object... args) {
|
||||
isTrue(!Objects.equals(result, expectedValue),
|
||||
() -> new DataOperationResultException(String.format(msgTemplate, args)));
|
||||
}
|
||||
|
||||
public static void updateOneRow(int i) {
|
||||
update(i, 1);
|
||||
equals(i, 1);
|
||||
}
|
||||
|
||||
public static void updateOneRow(Object i, String format) {
|
||||
update(i, 1, format);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user