diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisAutoConfiguration.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisAutoConfiguration.java
deleted file mode 100644
index 2191a44..0000000
--- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisAutoConfiguration.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package xyz.zhouxy.plusone.mybatis;
-
-import org.apache.ibatis.session.SqlSessionFactory;
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-@EnableAutoConfiguration
-public class MyBatisAutoConfiguration {
-
- @Bean
- MybatisUtil mybatisUtil(SqlSessionFactory sqlSessionFactory) {
- return MybatisUtil.getInstance()
- .setSqlSessionFactory(sqlSessionFactory);
- }
-}
diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisContextUtil.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisContextUtil.java
new file mode 100644
index 0000000..994d281
--- /dev/null
+++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisContextUtil.java
@@ -0,0 +1,16 @@
+package xyz.zhouxy.plusone.mybatis;
+
+import org.apache.ibatis.session.SqlSessionFactory;
+
+import xyz.zhouxy.plusone.spring.SpringContextHolder;
+
+public final class MyBatisContextUtil {
+
+ private MyBatisContextUtil() {
+ throw new IllegalStateException("Utility class");
+ }
+
+ public static SqlSessionFactory getSqlSessionFactory() {
+ return SpringContextHolder.getContext().getBean(SqlSessionFactory.class);
+ }
+}
diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisPlusConfig.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisPlusConfig.java
index 0ba5c2c..fb7cf82 100644
--- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisPlusConfig.java
+++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MyBatisPlusConfig.java
@@ -10,7 +10,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
+ MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MybatisUtil.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MybatisUtil.java
deleted file mode 100644
index 7a33ff5..0000000
--- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/mybatis/MybatisUtil.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package xyz.zhouxy.plusone.mybatis;
-
-import org.apache.ibatis.session.SqlSessionFactory;
-
-public final class MybatisUtil {
-
- private SqlSessionFactory sqlSessionFactory;
-
- private MybatisUtil() {
- }
-
- MybatisUtil setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
- this.sqlSessionFactory = sqlSessionFactory;
- return this;
- }
-
- private static final class Holder {
- private static final MybatisUtil INSTANCE = new MybatisUtil();
- }
-
- public static MybatisUtil getInstance() {
- return Holder.INSTANCE;
- }
-
- public static SqlSessionFactory getSqlSessionFactory() {
- return MybatisUtil.getInstance().sqlSessionFactory;
- }
-}
diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
index 36b13f6..7a7d9a8 100644
--- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
+++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
@@ -2,25 +2,31 @@ package xyz.zhouxy.plusone.validator;
import java.util.ArrayList;
import java.util.List;
+import java.util.function.Function;
import java.util.function.Predicate;
+import java.util.function.Supplier;
import xyz.zhouxy.plusone.exception.InvalidInputException;
/**
* 校验器
*
- *
可以使用以下方式初始化一个校验器:
+ *
+ * 可以使用以下方式初始化一个校验器:
+ *
*
*
* BaseValidator<Integer> validator = new BaseValidator<>() {
* {
- * ruleFor(value -> Objects.nonNull(value), "value 不能为空");
- * ruleFor(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
+ * withRule(value -> Objects.nonNull(value), "value 不能为空");
+ * withRule(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
* }
* };
*
*
- * 也可以通过继承本类,定义一个校验器(可使用单例模式)。
+ *
+ * 也可以通过继承本类,定义一个校验器(可使用单例模式)。
+ *
*
*
* 然后通过校验器的 {@link #validate} 方法,或
@@ -43,30 +49,40 @@ import xyz.zhouxy.plusone.exception.InvalidInputException;
*/
public abstract class BaseValidator {
- private final List> rules = new ArrayList<>();
+ private final List> rules = new ArrayList<>();
protected BaseValidator() {
}
- protected final void ruleFor(Predicate rule, String errorMessage) {
- this.rules.add(new RuleInfo<>(rule, errorMessage));
+ protected final void withRule(Predicate rule, String errorMessage) {
+ withRule(rule, () -> new InvalidInputException(errorMessage));
+ }
+
+ protected final void withRule(Predicate rule, Supplier exceptionCreator) {
+ withRule(rule, value -> exceptionCreator.get());
+ }
+
+ protected final void withRule(Predicate rule, Function exceptionCreator) {
+ this.rules.add(new RuleInfo<>(rule, exceptionCreator));
}
public void validate(T obj) {
- this.rules.forEach((RuleInfo ruleInfo) -> {
- if (!ruleInfo.rule.test(obj)) {
- throw new InvalidInputException(ruleInfo.message);
- }
- });
+ this.rules.forEach(ruleInfo -> ruleInfo.validate(obj));
}
- protected static class RuleInfo {
- Predicate rule;
- String message;
+ protected static class RuleInfo {
+ private final Predicate rule;
+ private final Function exceptionCreator;
- public RuleInfo(Predicate rule, String message) {
+ private RuleInfo(Predicate rule, Function exceptionCreator) {
this.rule = rule;
- this.message = message;
+ this.exceptionCreator = exceptionCreator;
+ }
+
+ private void validate(T obj) {
+ if (!rule.test(obj)) {
+ throw exceptionCreator.apply(obj);
+ }
}
}
}
diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator2.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator2.java
deleted file mode 100644
index b7ba736..0000000
--- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator2.java
+++ /dev/null
@@ -1,129 +0,0 @@
-package xyz.zhouxy.plusone.validator;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Objects;
-import java.util.function.Function;
-import java.util.function.Predicate;
-
-import cn.hutool.core.exceptions.ValidateException;
-import lombok.AllArgsConstructor;
-import xyz.zhouxy.plusone.constant.RegexConsts;
-import xyz.zhouxy.plusone.util.RegexUtil;
-
-public abstract class BaseValidator2 {
-
- private List> hs = new ArrayList<>();
-
- protected final ValidValueHolder ruleFor(Function getter) {
- ValidValueHolder validValueHolder = new ValidValueHolder<>(getter);
- hs.add(validValueHolder);
- return validValueHolder;
- }
-
- public void validate(T obj) {
- for (var holder : hs) {
- var value = holder.getter.apply(obj);
- for (var rule : holder.rules) {
- if (!rule.condition.test(value)) {
- throw new ValidateException(rule.errMsg);
- }
- }
- }
- }
-}
-
-class ValidValueHolder {
- Function getter;
-
- List> rules = new ArrayList<>();
-
- public ValidValueHolder(Function getter) {
- this.getter = getter;
- }
-
- private void addRule(Predicate