feat: spring boot 2.x封版

This commit is contained in:
wangyu 2024-07-13 13:00:48 +08:00
parent ff4637535a
commit f0b48ba785
3 changed files with 22 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import group.flyfish.fluent.utils.cache.LRUCache;
import group.flyfish.fluent.utils.context.AliasComposite; import group.flyfish.fluent.utils.context.AliasComposite;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -27,6 +28,9 @@ import static group.flyfish.fluent.utils.sql.SqlNameUtils.wrap;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EntityNameUtils { public final class EntityNameUtils {
private static final String JPA_TABLE = "javax.persistence.Table";
private static final String SPRING_DATA_TABLE = "org.springframework.data.relational.core.mapping.Table";
// SerializedLambda 反序列化缓存 // SerializedLambda 反序列化缓存
private static final Map<String, WeakReference<SerializedLambda>> FUNC_CACHE = new ConcurrentHashMap<>(); private static final Map<String, WeakReference<SerializedLambda>> FUNC_CACHE = new ConcurrentHashMap<>();
@ -57,9 +61,18 @@ public final class EntityNameUtils {
*/ */
public static String getTableName(Class<?> entityClass) { public static String getTableName(Class<?> entityClass) {
return TABLE_CACHE.computeIfAbsent(entityClass, k -> { return TABLE_CACHE.computeIfAbsent(entityClass, k -> {
Table table = entityClass.getAnnotation(Table.class); MergedAnnotations annotations = MergedAnnotations.from(entityClass);
if (null != table && StringUtils.hasText(table.name())) { if (annotations.isPresent(JPA_TABLE)) {
return table.name(); String tableName = annotations.get(JPA_TABLE).getString("name");
if (StringUtils.hasText(tableName)) {
return tableName;
}
}
if (annotations.isPresent(SPRING_DATA_TABLE)) {
String tableName = annotations.get(SPRING_DATA_TABLE).getString("name");
if (StringUtils.hasText(tableName)) {
return tableName;
}
} }
return wrap(SqlNameUtils.camelToUnderline(entityClass.getSimpleName())); return wrap(SqlNameUtils.camelToUnderline(entityClass.getSimpleName()));
}); });

View File

@ -11,6 +11,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@ -23,11 +24,11 @@ import javax.sql.DataSource;
* *
* @author wangyu * @author wangyu
*/ */
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@Import({R2dbcFluentSqlAutoConfigure.class, JdbcFluentSqlAutoConfigure.class}) @Import({R2dbcFluentSqlAutoConfigure.class, JdbcFluentSqlAutoConfigure.class})
public class FluentSqlAutoConfiguration { public class FluentSqlAutoConfiguration {
@ConditionalOnClass(DatabaseClient.class) @ConditionalOnClass(DatabaseClient.class)
@AutoConfigureAfter(R2dbcAutoConfiguration.class)
static class R2dbcFluentSqlAutoConfigure { static class R2dbcFluentSqlAutoConfigure {
/** /**
@ -44,6 +45,7 @@ public class FluentSqlAutoConfiguration {
} }
@ConditionalOnClass(DataSource.class) @ConditionalOnClass(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
static class JdbcFluentSqlAutoConfigure { static class JdbcFluentSqlAutoConfigure {
/** /**

View File

@ -1,5 +1,6 @@
package group.flyfish.fluent.mapping; package group.flyfish.fluent.mapping;
import io.r2dbc.spi.ColumnMetadata;
import io.r2dbc.spi.Row; import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata; import io.r2dbc.spi.RowMetadata;
import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.DataRetrievalFailureException;
@ -48,7 +49,8 @@ public class ReactiveSQLMappedRowMapper<T> implements BiFunction<Row, RowMetadat
public T apply(Row row, RowMetadata rowMetadata) { public T apply(Row row, RowMetadata rowMetadata) {
MappingBean<T> bean = descriptor.create(); MappingBean<T> bean = descriptor.create();
for (String column : rowMetadata.getColumnNames()) { for (ColumnMetadata metadata : rowMetadata.getColumnMetadatas()) {
String column = metadata.getName();
try { try {
bean.setValue(column, type -> row.get(column, type)); bean.setValue(column, type -> row.get(column, type));
} catch (Exception ex) { } catch (Exception ex) {