This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 6fee634e6 Refactor admin mapper config (#3918)
6fee634e6 is described below
commit 6fee634e61daab93404665443b68257809f529a2
Author: renzhuyan <[email protected]>
AuthorDate: Tue Sep 6 10:38:49 2022 +0800
Refactor admin mapper config (#3918)
---
.../apache/shenyu/admin/config/MapperConfig.java | 102 ++++++++++++---------
.../mybatis/oracle/OracleSQLUpdateInterceptor.java | 76 +++++++++++++++
.../shenyu/admin/config/MapperConfigTest.java | 15 +--
3 files changed, 145 insertions(+), 48 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
index 4425286ab..ca9465121 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
@@ -18,9 +18,11 @@
package org.apache.shenyu.admin.config;
import org.apache.shenyu.admin.mybatis.oracle.OracleSQLPrepareInterceptor;
+import org.apache.shenyu.admin.mybatis.oracle.OracleSQLUpdateInterceptor;
import
org.apache.shenyu.admin.mybatis.pg.interceptor.PostgreSQLPrepareInterceptor;
import
org.apache.shenyu.admin.mybatis.pg.interceptor.PostgreSQLQueryInterceptor;
import
org.apache.shenyu.admin.mybatis.pg.interceptor.PostgreSqlUpdateInterceptor;
+import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -33,57 +35,73 @@ import org.springframework.context.annotation.Configuration;
public class MapperConfig {
/**
- * Add the plugin to the MyBatis plugin interceptor chain.
- *
- * @return {@linkplain PostgreSQLQueryInterceptor}
+ * The type PostgreSQL.
*/
- @Bean
+ @Configuration
@ConditionalOnProperty(name = "shenyu.database.dialect", havingValue =
"postgresql")
- public PostgreSQLQueryInterceptor postgreSqlQueryInterceptor() {
- return new PostgreSQLQueryInterceptor();
- }
+ static class PostgreSQLConfig {
- /**
- * Add the plugin to the MyBatis plugin interceptor chain.
- *
- * @return {@linkplain PostgreSQLPrepareInterceptor}
- */
- @Bean
- @ConditionalOnProperty(name = "shenyu.database.dialect", havingValue =
"postgresql")
- public PostgreSQLPrepareInterceptor postgreSqlPrepareInterceptor() {
- return new PostgreSQLPrepareInterceptor();
- }
+ /**
+ * Add the plugin to the MyBatis plugin interceptor chain.
+ *
+ * @return {@linkplain PostgreSQLQueryInterceptor}
+ */
+ @Bean
+ @ConditionalOnMissingBean(PostgreSQLQueryInterceptor.class)
+ public PostgreSQLQueryInterceptor postgreSqlQueryInterceptor() {
+ return new PostgreSQLQueryInterceptor();
+ }
- /**
- * Add the plugin to the MyBatis plugin interceptor chain.
- *
- * @return {@linkplain OracleSQLPrepareInterceptor}
- */
- @Bean
- @ConditionalOnProperty(name = "shenyu.database.dialect", havingValue =
"oracle")
- public OracleSQLPrepareInterceptor oracleSqlPrepareInterceptor() {
- return new OracleSQLPrepareInterceptor();
+ /**
+ * Add the plugin to the MyBatis plugin interceptor chain.
+ *
+ * @return {@linkplain PostgreSQLPrepareInterceptor}
+ */
+ @Bean
+ @ConditionalOnMissingBean(PostgreSQLPrepareInterceptor.class)
+ public PostgreSQLPrepareInterceptor postgreSqlPrepareInterceptor() {
+ return new PostgreSQLPrepareInterceptor();
+ }
+
+ /**
+ * Add the plugin to the MyBatis plugin interceptor chain.
+ *
+ * @return {@linkplain PostgreSqlUpdateInterceptor}
+ */
+ @Bean
+ @ConditionalOnMissingBean(PostgreSqlUpdateInterceptor.class)
+ public PostgreSqlUpdateInterceptor postgreSqlUpdateInterceptor() {
+ return new PostgreSqlUpdateInterceptor();
+ }
}
/**
- * Add the plugin to the MyBatis plugin interceptor chain.
- *
- * @return {@linkplain PostgreSqlUpdateInterceptor}
+ * The type OracleSQL.
*/
- @Bean
+ @Configuration
@ConditionalOnProperty(name = "shenyu.database.dialect", havingValue =
"oracle")
- public PostgreSqlUpdateInterceptor oracleSqlUpdateInterceptor() {
- return new PostgreSqlUpdateInterceptor();
- }
+ static class OracleSQLConfig {
- /**
- * Add the plugin to the MyBatis plugin interceptor chain.
- *
- * @return {@linkplain PostgreSqlUpdateInterceptor}
- */
- @Bean
- @ConditionalOnProperty(name = "shenyu.database.dialect", havingValue =
"postgresql")
- public PostgreSqlUpdateInterceptor postgreSqlUpdateInterceptor() {
- return new PostgreSqlUpdateInterceptor();
+ /**
+ * Add the plugin to the MyBatis plugin interceptor chain.
+ *
+ * @return {@linkplain OracleSQLPrepareInterceptor}
+ */
+ @Bean
+ @ConditionalOnMissingBean(OracleSQLPrepareInterceptor.class)
+ public OracleSQLPrepareInterceptor oracleSqlPrepareInterceptor() {
+ return new OracleSQLPrepareInterceptor();
+ }
+
+ /**
+ * Add the plugin to the MyBatis plugin interceptor chain.
+ *
+ * @return {@linkplain OracleSQLUpdateInterceptor}
+ */
+ @Bean
+ @ConditionalOnMissingBean(OracleSQLUpdateInterceptor.class)
+ public OracleSQLUpdateInterceptor oracleSqlUpdateInterceptor() {
+ return new OracleSQLUpdateInterceptor();
+ }
}
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLUpdateInterceptor.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLUpdateInterceptor.java
new file mode 100644
index 000000000..8ba0f98ad
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLUpdateInterceptor.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.mybatis.oracle;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.plugin.Intercepts;
+import org.apache.ibatis.plugin.Invocation;
+import org.apache.ibatis.plugin.Plugin;
+import org.apache.ibatis.plugin.Signature;
+import org.apache.shenyu.common.utils.ReflectUtils;
+
+import java.lang.reflect.Field;
+import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * The 'date_updated' field in the postgreSql library is not automatically
updated.
+ * So this interceptor intercepts the UPDATE statement.
+ * Adds the current time to the 'date_updated' field at run time.
+ */
+@Intercepts({
+ @Signature(type = Executor.class, method = "update", args =
{MappedStatement.class, Object.class})
+})
+public class OracleSQLUpdateInterceptor implements Interceptor {
+
+ private static final List<String> AUTOMATIC_DATES =
ImmutableList.of("dateUpdated");
+
+ @Override
+ public Object intercept(final Invocation invocation) throws Throwable {
+ Object[] args = invocation.getArgs();
+ MappedStatement ms = (MappedStatement) args[0];
+ Object parameter = args[1];
+ Executor executor = (Executor) invocation.getTarget();
+ for (Class<?> superClass = parameter.getClass(); superClass !=
Object.class; superClass = superClass.getSuperclass()) {
+ Arrays.stream(superClass.getDeclaredFields())
+ .filter(f -> matchParam(parameter, f))
+ .forEach(f -> ReflectUtils.setFieldValue(parameter,
f.getName(), new Timestamp(System.currentTimeMillis())));
+ }
+ return executor.update(ms, parameter);
+ }
+
+ private boolean matchParam(final Object parameter, final Field f) {
+ return AUTOMATIC_DATES.contains(f.getName()) &&
Objects.isNull(ReflectUtils.getFieldValue(parameter, f));
+ }
+
+ @Override
+ public Object plugin(final Object target) {
+ return Plugin.wrap(target, this);
+ }
+
+ @Override
+ public void setProperties(final Properties properties) {
+
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/MapperConfigTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/MapperConfigTest.java
index a7fe83b87..1349e379b 100644
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/MapperConfigTest.java
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/MapperConfigTest.java
@@ -32,31 +32,34 @@ import static
org.junit.jupiter.api.Assertions.assertNotNull;
public class MapperConfigTest {
@InjectMocks
- private MapperConfig mapperConfig;
+ private MapperConfig.OracleSQLConfig oracleSQLConfig;
+
+ @InjectMocks
+ private MapperConfig.PostgreSQLConfig postgreSQLConfig;
@Test
public void testPostgreSQLQueryInterceptor() {
- PostgreSQLQueryInterceptor postgreSQLQueryInterceptor =
mapperConfig.postgreSqlQueryInterceptor();
+ PostgreSQLQueryInterceptor postgreSQLQueryInterceptor =
postgreSQLConfig.postgreSqlQueryInterceptor();
assertNotNull(postgreSQLQueryInterceptor);
}
@Test
public void postgreSqlPrepareInterceptorTest() {
- assertNotNull(mapperConfig.postgreSqlPrepareInterceptor());
+ assertNotNull(postgreSQLConfig.postgreSqlPrepareInterceptor());
}
@Test
public void oracleSqlPrepareInterceptorTest() {
- assertNotNull(mapperConfig.oracleSqlPrepareInterceptor());
+ assertNotNull(oracleSQLConfig.oracleSqlPrepareInterceptor());
}
@Test
public void oracleSqlUpdateInterceptorTest() {
- assertNotNull(mapperConfig.oracleSqlUpdateInterceptor());
+ assertNotNull(oracleSQLConfig.oracleSqlUpdateInterceptor());
}
@Test
public void postgreSqlUpdateInterceptorTest() {
- assertNotNull(mapperConfig.postgreSqlUpdateInterceptor());
+ assertNotNull(postgreSQLConfig.postgreSqlUpdateInterceptor());
}
}