Skip to content

Commit fc0e8af

Browse files
author
GangCheng
committed
Merge branch 'feat/followup' into 3.x
2 parents 49e74b0 + 228dbd3 commit fc0e8af

File tree

5 files changed

+80
-36
lines changed

5 files changed

+80
-36
lines changed

mybatis-r2dbc-generator/src/main/java/pro/chenggang/project/reactive/mybatis/support/generator/core/MybatisDynamicCodeGenerator.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232

3333
import java.util.ArrayList;
3434
import java.util.List;
35+
import java.util.Objects;
3536
import java.util.Optional;
37+
import java.util.function.Consumer;
3638

3739
/**
3840
* The Mybatis dynamic code generator builder.
@@ -43,6 +45,7 @@
4345
public class MybatisDynamicCodeGenerator {
4446

4547
private final ContextGeneratorFactory contextGeneratorFactory = new ContextGeneratorFactory();
48+
private Consumer<Configuration> configurationCustomizer;
4649

4750
private MybatisDynamicCodeGenerator() {
4851

@@ -105,13 +108,27 @@ public static MybatisDynamicCodeGenerator withYamlConfiguration(String configura
105108
return new MybatisDynamicCodeGenerator(new YamlGeneratorPropertiesLoader(configurationFileName));
106109
}
107110

111+
/**
112+
* Customize configuration mybatis dynamic code generator.
113+
*
114+
* @param configurationCustomizer the configuration customizer
115+
* @return the mybatis dynamic code generator
116+
*/
117+
public MybatisDynamicCodeGenerator customizeConfiguration(Consumer<Configuration> configurationCustomizer) {
118+
this.configurationCustomizer = configurationCustomizer;
119+
return this;
120+
}
121+
108122
/**
109123
* Execute generate action.
110124
*/
111125
public void generate() {
112126
GeneratorProperties generatorProperties = GeneratorPropertiesHolder.getInstance().getGeneratorProperties();
113127
generatorProperties.validate();
114128
Configuration configuration = this.getConfiguration(generatorProperties);
129+
if (Objects.nonNull(this.configurationCustomizer)) {
130+
this.configurationCustomizer.accept(configuration);
131+
}
115132
DefaultShellCallback callback = new DefaultShellCallback(generatorProperties.isOverwrite());
116133
List<String> warnings = new ArrayList<>();
117134
try {

mybatis-r2dbc/src/main/java/pro/chenggang/project/reactive/mybatis/support/r2dbc/executor/placeholder/defaults/DefaultPlaceholderFormatter.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
import com.github.benmanes.caffeine.cache.Cache;
1919
import com.github.benmanes.caffeine.cache.Caffeine;
20+
import com.github.benmanes.caffeine.cache.RemovalCause;
21+
import com.github.benmanes.caffeine.cache.RemovalListener;
22+
import com.github.benmanes.caffeine.cache.Scheduler;
2023
import io.r2dbc.spi.ConnectionMetadata;
2124
import org.apache.ibatis.logging.Log;
2225
import org.apache.ibatis.logging.LogFactory;
2326
import org.apache.ibatis.mapping.BoundSql;
2427
import org.apache.ibatis.mapping.ParameterMapping;
2528
import org.apache.ibatis.util.MapUtil;
29+
import org.checkerframework.checker.nullness.qual.Nullable;
2630
import pro.chenggang.project.reactive.mybatis.support.r2dbc.executor.placeholder.PlaceholderDialect;
2731
import pro.chenggang.project.reactive.mybatis.support.r2dbc.executor.placeholder.PlaceholderDialectRegistry;
2832
import pro.chenggang.project.reactive.mybatis.support.r2dbc.executor.placeholder.PlaceholderFormatter;
@@ -60,6 +64,9 @@ public DefaultPlaceholderFormatter(PlaceholderDialectRegistry placeholderDialect
6064
.maximumSize(sqlCacheMaxSize)
6165
.expireAfterAccess(sqlCacheExpireDuration)
6266
.initialCapacity(10)
67+
.evictionListener(new CacheRemovalListener("eviction", placeholderDialectType.getSimpleName()))
68+
.removalListener(new CacheRemovalListener("removal", placeholderDialectType.getSimpleName()))
69+
.scheduler(Scheduler.systemScheduler())
6370
.build();
6471
this.formattedSqlCache.put(placeholderDialectType, cache);
6572
}
@@ -145,4 +152,20 @@ protected String formatPlaceholderInternal(PlaceholderDialect placeholderDialect
145152
return builder.toString();
146153
}
147154

155+
private static class CacheRemovalListener implements RemovalListener<String, String> {
156+
157+
private final String listenerType;
158+
private final String dialectType;
159+
160+
private CacheRemovalListener(String listenerType, String dialectType) {
161+
this.listenerType = listenerType;
162+
this.dialectType = dialectType;
163+
}
164+
165+
@Override
166+
public void onRemoval(@Nullable String key, @Nullable String value, RemovalCause cause) {
167+
log.debug("Placeholder(" + dialectType + ") cache " + listenerType + " triggered according to " + cause);
168+
log.trace("Key : " + key + "\nValue : " + value);
169+
}
170+
}
148171
}

mybatis-r2dbc/src/main/java/pro/chenggang/project/reactive/mybatis/support/r2dbc/executor/result/handler/DefaultReactiveResultHandler.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,18 @@ private Object applyConstructorAutomapping(ReadableResultWrapper<? extends Reada
558558

559559
private boolean applyColumnOrderBasedConstructorAutomapping(ReadableResultWrapper<? extends Readable> readableResultWrapper, List<Class<?>> constructorArgTypes,
560560
List<Object> constructorArgs, Constructor<?> constructor, boolean foundValues) throws SQLException {
561-
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
562-
Class<?> parameterType = constructor.getParameterTypes()[i];
561+
Class<?>[] parameterTypes = constructor.getParameterTypes();
562+
if (parameterTypes.length > readableResultWrapper.getClassNames().size()) {
563+
throw new ExecutorException(MessageFormat.format(
564+
"Constructor auto-mapping of ''{0}'' failed. " +
565+
"The constructor takes ''{1}'' arguments, but there are only ''{2}'' columns in the result set.",
566+
constructor,
567+
parameterTypes.length,
568+
readableResultWrapper.getClassNames().size()
569+
));
570+
}
571+
for (int i = 0; i < parameterTypes.length; i++) {
572+
Class<?> parameterType = parameterTypes[i];
563573
String columnName = readableResultWrapper.getColumnNames().get(i);
564574
final TypeHandler<?> typeHandler = readableResultWrapper.getTypeHandler(parameterType, columnName);
565575
((TypeHandleContext) this.delegatedTypeHandler).contextWith(parameterType,typeHandler,

mybatis-r2dbc/src/main/java/pro/chenggang/project/reactive/mybatis/support/r2dbc/mapping/R2dbcVendorDatabaseIdProvider.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import io.r2dbc.spi.ConnectionFactory;
1919
import org.apache.ibatis.builder.BuilderException;
20-
import org.apache.ibatis.logging.Log;
21-
import org.apache.ibatis.logging.LogFactory;
2220

2321
import java.sql.SQLException;
2422
import java.util.Map;
@@ -37,41 +35,37 @@
3735
*/
3836
public class R2dbcVendorDatabaseIdProvider implements R2dbcDatabaseIdProvider {
3937

40-
private Properties properties;
38+
private Properties properties;
4139

42-
@Override
43-
public String getDatabaseId(ConnectionFactory connectionFactory) {
44-
if (connectionFactory == null) {
45-
throw new NullPointerException("dataSource cannot be null");
46-
}
47-
try {
48-
return getDatabaseName(connectionFactory);
49-
} catch (SQLException e) {
50-
throw new BuilderException("Error occurred when getting DB product name.", e);
40+
@Override
41+
public String getDatabaseId(ConnectionFactory connectionFactory) {
42+
if (connectionFactory == null) {
43+
throw new NullPointerException("dataSource cannot be null");
44+
}
45+
try {
46+
return getDatabaseName(connectionFactory);
47+
} catch (SQLException e) {
48+
throw new BuilderException("Error occurred when getting DB product name.", e);
49+
}
5150
}
52-
}
5351

54-
@Override
55-
public void setProperties(Properties p) {
56-
this.properties = p;
57-
}
52+
@Override
53+
public void setProperties(Properties p) {
54+
this.properties = p;
55+
}
5856

59-
protected String getDatabaseName(ConnectionFactory connectionFactory) throws SQLException {
60-
String productName = connectionFactory.getMetadata().getName();
61-
if (this.properties != null) {
62-
for (Map.Entry<Object, Object> property : properties.entrySet()) {
63-
if (productName.contains((String) property.getKey())) {
64-
return (String) property.getValue();
57+
protected String getDatabaseName(ConnectionFactory connectionFactory) throws SQLException {
58+
String productName = connectionFactory.getMetadata().getName();
59+
if (properties == null || properties.isEmpty()) {
60+
return productName;
61+
}
62+
for (Map.Entry<Object, Object> property : properties.entrySet()) {
63+
if (productName.contains((String) property.getKey())) {
64+
return (String) property.getValue();
65+
}
6566
}
66-
}
67-
// no match, return null
68-
return null;
67+
// no match, return null
68+
return null;
6969
}
70-
return productName;
71-
}
72-
73-
private static class LogHolder {
74-
private static final Log log = LogFactory.getLog(R2dbcVendorDatabaseIdProvider.class);
75-
}
7670

7771
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<r2dbc-postgresql.version>1.0.7.RELEASE</r2dbc-postgresql.version>
7575
<r2dbc-mssql.version>1.0.2.RELEASE</r2dbc-mssql.version>
7676
<oracle-r2dbc.version>1.2.0</oracle-r2dbc.version>
77-
<mybatis.version>3.5.16</mybatis.version>
77+
<mybatis.version>3.5.19</mybatis.version>
7878
<mybatis-spring.version>3.0.4</mybatis-spring.version>
7979
<mybatis-freemarker.version>1.3.0</mybatis-freemarker.version>
8080
<mybatis-velocity.version>2.3.0</mybatis-velocity.version>
@@ -87,7 +87,7 @@
8787
<junit-jupiter.version>5.10.0</junit-jupiter.version>
8888
<testcontainers.version>1.20.3</testcontainers.version>
8989
<assertj.version>3.24.2</assertj.version>
90-
<byte-buddy.version>1.15.7</byte-buddy.version>
90+
<byte-buddy.version>1.15.11</byte-buddy.version>
9191
<caffeine.version>3.1.8</caffeine.version>
9292
<license-maven-plugin.version>4.3</license-maven-plugin.version>
9393
<slf4j.version>2.0.10</slf4j.version>

0 commit comments

Comments
 (0)