EntityBuilder

This commit is contained in:
redkale
2024-01-12 14:34:18 +08:00
parent 076c260793
commit ef802bb1ce
6 changed files with 36 additions and 13 deletions

View File

@@ -30,6 +30,16 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface Priority {
/**
* 最高优先级, 其他值必须比此值小
*/
public static final int HIGHTEST = Integer.MAX_VALUE;
/**
* 最低优先级, 其他值必须比此值大
*/
public static final int LOWEST = Integer.MIN_VALUE;
/**
* 优先级值
*

View File

@@ -11,7 +11,7 @@ import org.redkale.util.AnyValue;
/**
* 协议拦截器类, 类似JavaEE中的javax.servlet.Filter <br>
* javax.servlet.Filter方法doFilter是同步操作此Filter.doFilter则是异步操作方法return前需要调用Response.nextEvent()方可执行下一个Filter <br>
* javax.servlet.Filter方法doFilter是同步操作此Filter.doFilter则是异步操作方法return前必须调用Response.nextEvent() <br>
* 通过给Filter标记注解&#064;Priority来确定执行的顺序, Priority.value值越大越先执行 <br>
* 如果doFilter方法是非阻塞的需要在Filter类上标记&#064;NonBlocking
*

View File

@@ -232,8 +232,7 @@ public interface DataSqlSource extends DataSource {
if (!rset.next()) {
return null;
}
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
if (EntityBuilder.isSimpleType(type)) {
return (V) formatColumnValue(type, rset.getObject(1));
}
return EntityBuilder.load(type).getObjectValue(rset);
@@ -246,8 +245,7 @@ public interface DataSqlSource extends DataSource {
if (!rset.next()) {
return null;
}
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
if (EntityBuilder.isSimpleType(type)) {
return (V) formatColumnValue(type, rset.getObject(1));
}
return EntityBuilder.load(type).getObjectValue(rset);
@@ -257,8 +255,7 @@ public interface DataSqlSource extends DataSource {
@AsmDepends
default <V> List<V> nativeQueryList(Class<V> type, String sql, Map<String, Object> params) {
return nativeQuery(sql, rset -> {
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
if (EntityBuilder.isSimpleType(type)) {
List<V> list = new ArrayList<>();
while (rset.next()) {
list.add(rset.wasNull() ? null : (V) formatColumnValue(type, rset.getObject(1)));
@@ -272,8 +269,7 @@ public interface DataSqlSource extends DataSource {
@AsmDepends
default <V> CompletableFuture<List<V>> nativeQueryListAsync(Class<V> type, String sql, Map<String, Object> params) {
return nativeQueryAsync(sql, rset -> {
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
if (EntityBuilder.isSimpleType(type)) {
List<V> list = new ArrayList<>();
while (rset.next()) {
list.add(rset.wasNull() ? null : (V) formatColumnValue(type, rset.getObject(1)));

View File

@@ -17,6 +17,8 @@ import org.redkale.util.*;
* 可以是实体类也可以是查询结果的JavaBean类
*
* @author zhangjx
* @param <T> T
*
* @since 2.8.0
*/
public class EntityBuilder<T> {
@@ -54,7 +56,7 @@ public class EntityBuilder<T> {
private final Attribute<T, Serializable>[] unconstructorAttributes;
//key类字段名
private final Map<String, Attribute<T, Serializable>> attributeMap;
final Map<String, Attribute<T, Serializable>> attributeMap;
//key数据库字段名
private final Map<String, Attribute<T, Serializable>> sqlAttrMap;
@@ -81,8 +83,13 @@ public class EntityBuilder<T> {
attributeMap.forEach((k, v) -> sqlAttrMap.put(getSQLColumn(null, k), v));
}
public static boolean isSimpleType(Class type) {
return type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."));
}
public static <T> EntityBuilder<T> load(Class<T> type) {
return cacheMap.computeIfAbsent(type, t -> create(t));
return cacheMap.computeIfAbsent(type, EntityBuilder::create);
}
private static <T> EntityBuilder<T> create(Class<T> type) {

View File

@@ -33,6 +33,7 @@ import static org.redkale.source.DataNativeSqlInfo.SqlMode.SELECT;
import org.redkale.source.DataNativeSqlParser;
import org.redkale.source.DataSqlMapper;
import org.redkale.source.DataSqlSource;
import org.redkale.source.EntityBuilder;
import org.redkale.source.Flipper;
import org.redkale.source.SourceException;
import org.redkale.util.RedkaleClassLoader;
@@ -84,7 +85,7 @@ public final class DataSqlMapperBuilder {
} catch (Throwable t) {
t.printStackTrace();
}
EntityBuilder.load(entityType);
List<Item> items = new ArrayList<>();
Map<String, AsmMethodBean> selfMethodBeans = AsmMethodBoost.getMethodBeans(mapperType);
for (Method method : mapperType.getMethods()) {
@@ -221,7 +222,10 @@ public final class DataSqlMapperBuilder {
Class[] paramTypes = method.getParameterTypes();
List<AsmMethodParam> methodParams = methodBean.getParams();
List<Integer> insns = new ArrayList<>();
if (!EntityBuilder.isSimpleType(componentTypes[0])) {
EntityBuilder.load(componentTypes[0]);
}
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, method.getName(), methodBean.getDesc(), methodBean.getSignature(), null)).setDebug(false);
Label l0 = new Label();
mv.visitLabel(l0);

View File

@@ -146,6 +146,7 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
* @param namespace 命名空间
* @param events 变更项
*/
@Override
public void onEnvironmentChanged(String namespace, List<ResourceEvent> events) {
Set<String> sourceRemovedKeys = new HashSet<>();
Properties sourceChangedProps = new Properties();
@@ -307,6 +308,7 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
/**
* 服务全部停掉后被调用
*/
@Override
public void onServersPostStop() {
for (DataSource source : dataSources) {
if (source == null) {
@@ -343,12 +345,14 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
*
* @return CacheSource集合
*/
@Override
public Map<String, CacheSource> getCacheSources() {
Map<String, CacheSource> sources = new HashMap<>();
cacheSources.forEach(v -> sources.put(v.resourceName(), v));
return sources;
}
@Override
public CacheSource loadCacheSource(final String sourceName, boolean autoMemory) {
cacheSourceLock.lock();
try {
@@ -402,12 +406,14 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
*
* @return DataSource集合
*/
@Override
public Map<String, DataSource> getDataSources() {
Map<String, DataSource> sources = new HashMap<>();
dataSources.forEach(v -> sources.put(v.resourceName(), v));
return sources;
}
@Override
public DataSource loadDataSource(final String sourceName, boolean autoMemory) {
dataSourceLock.lock();
try {