EntityBuilder
This commit is contained in:
@@ -30,6 +30,16 @@ import java.lang.annotation.Target;
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Priority {
|
public @interface Priority {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最高优先级, 其他值必须比此值小
|
||||||
|
*/
|
||||||
|
public static final int HIGHTEST = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最低优先级, 其他值必须比此值大
|
||||||
|
*/
|
||||||
|
public static final int LOWEST = Integer.MIN_VALUE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 优先级值
|
* 优先级值
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import org.redkale.util.AnyValue;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 协议拦截器类, 类似JavaEE中的javax.servlet.Filter <br>
|
* 协议拦截器类, 类似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标记注解@Priority来确定执行的顺序, Priority.value值越大越先执行 <br>
|
* 通过给Filter标记注解@Priority来确定执行的顺序, Priority.value值越大越先执行 <br>
|
||||||
* 如果doFilter方法是非阻塞的,需要在Filter类上标记@NonBlocking
|
* 如果doFilter方法是非阻塞的,需要在Filter类上标记@NonBlocking
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -232,8 +232,7 @@ public interface DataSqlSource extends DataSource {
|
|||||||
if (!rset.next()) {
|
if (!rset.next()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|
if (EntityBuilder.isSimpleType(type)) {
|
||||||
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
|
|
||||||
return (V) formatColumnValue(type, rset.getObject(1));
|
return (V) formatColumnValue(type, rset.getObject(1));
|
||||||
}
|
}
|
||||||
return EntityBuilder.load(type).getObjectValue(rset);
|
return EntityBuilder.load(type).getObjectValue(rset);
|
||||||
@@ -246,8 +245,7 @@ public interface DataSqlSource extends DataSource {
|
|||||||
if (!rset.next()) {
|
if (!rset.next()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|
if (EntityBuilder.isSimpleType(type)) {
|
||||||
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
|
|
||||||
return (V) formatColumnValue(type, rset.getObject(1));
|
return (V) formatColumnValue(type, rset.getObject(1));
|
||||||
}
|
}
|
||||||
return EntityBuilder.load(type).getObjectValue(rset);
|
return EntityBuilder.load(type).getObjectValue(rset);
|
||||||
@@ -257,8 +255,7 @@ public interface DataSqlSource extends DataSource {
|
|||||||
@AsmDepends
|
@AsmDepends
|
||||||
default <V> List<V> nativeQueryList(Class<V> type, String sql, Map<String, Object> params) {
|
default <V> List<V> nativeQueryList(Class<V> type, String sql, Map<String, Object> params) {
|
||||||
return nativeQuery(sql, rset -> {
|
return nativeQuery(sql, rset -> {
|
||||||
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|
if (EntityBuilder.isSimpleType(type)) {
|
||||||
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
|
|
||||||
List<V> list = new ArrayList<>();
|
List<V> list = new ArrayList<>();
|
||||||
while (rset.next()) {
|
while (rset.next()) {
|
||||||
list.add(rset.wasNull() ? null : (V) formatColumnValue(type, rset.getObject(1)));
|
list.add(rset.wasNull() ? null : (V) formatColumnValue(type, rset.getObject(1)));
|
||||||
@@ -272,8 +269,7 @@ public interface DataSqlSource extends DataSource {
|
|||||||
@AsmDepends
|
@AsmDepends
|
||||||
default <V> CompletableFuture<List<V>> nativeQueryListAsync(Class<V> type, String sql, Map<String, Object> params) {
|
default <V> CompletableFuture<List<V>> nativeQueryListAsync(Class<V> type, String sql, Map<String, Object> params) {
|
||||||
return nativeQueryAsync(sql, rset -> {
|
return nativeQueryAsync(sql, rset -> {
|
||||||
if (type == byte[].class || type == String.class || type.isPrimitive() || Number.class.isAssignableFrom(type)
|
if (EntityBuilder.isSimpleType(type)) {
|
||||||
|| (!Map.class.isAssignableFrom(type) && type.getName().startsWith("java."))) {
|
|
||||||
List<V> list = new ArrayList<>();
|
List<V> list = new ArrayList<>();
|
||||||
while (rset.next()) {
|
while (rset.next()) {
|
||||||
list.add(rset.wasNull() ? null : (V) formatColumnValue(type, rset.getObject(1)));
|
list.add(rset.wasNull() ? null : (V) formatColumnValue(type, rset.getObject(1)));
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ import org.redkale.util.*;
|
|||||||
* 可以是实体类,也可以是查询结果的JavaBean类
|
* 可以是实体类,也可以是查询结果的JavaBean类
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
|
* @param <T> T
|
||||||
|
*
|
||||||
* @since 2.8.0
|
* @since 2.8.0
|
||||||
*/
|
*/
|
||||||
public class EntityBuilder<T> {
|
public class EntityBuilder<T> {
|
||||||
@@ -54,7 +56,7 @@ public class EntityBuilder<T> {
|
|||||||
private final Attribute<T, Serializable>[] unconstructorAttributes;
|
private final Attribute<T, Serializable>[] unconstructorAttributes;
|
||||||
|
|
||||||
//key:类字段名
|
//key:类字段名
|
||||||
private final Map<String, Attribute<T, Serializable>> attributeMap;
|
final Map<String, Attribute<T, Serializable>> attributeMap;
|
||||||
|
|
||||||
//key:数据库字段名
|
//key:数据库字段名
|
||||||
private final Map<String, Attribute<T, Serializable>> sqlAttrMap;
|
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));
|
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) {
|
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) {
|
private static <T> EntityBuilder<T> create(Class<T> type) {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import static org.redkale.source.DataNativeSqlInfo.SqlMode.SELECT;
|
|||||||
import org.redkale.source.DataNativeSqlParser;
|
import org.redkale.source.DataNativeSqlParser;
|
||||||
import org.redkale.source.DataSqlMapper;
|
import org.redkale.source.DataSqlMapper;
|
||||||
import org.redkale.source.DataSqlSource;
|
import org.redkale.source.DataSqlSource;
|
||||||
|
import org.redkale.source.EntityBuilder;
|
||||||
import org.redkale.source.Flipper;
|
import org.redkale.source.Flipper;
|
||||||
import org.redkale.source.SourceException;
|
import org.redkale.source.SourceException;
|
||||||
import org.redkale.util.RedkaleClassLoader;
|
import org.redkale.util.RedkaleClassLoader;
|
||||||
@@ -84,7 +85,7 @@ public final class DataSqlMapperBuilder {
|
|||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
|
EntityBuilder.load(entityType);
|
||||||
List<Item> items = new ArrayList<>();
|
List<Item> items = new ArrayList<>();
|
||||||
Map<String, AsmMethodBean> selfMethodBeans = AsmMethodBoost.getMethodBeans(mapperType);
|
Map<String, AsmMethodBean> selfMethodBeans = AsmMethodBoost.getMethodBeans(mapperType);
|
||||||
for (Method method : mapperType.getMethods()) {
|
for (Method method : mapperType.getMethods()) {
|
||||||
@@ -221,6 +222,9 @@ public final class DataSqlMapperBuilder {
|
|||||||
Class[] paramTypes = method.getParameterTypes();
|
Class[] paramTypes = method.getParameterTypes();
|
||||||
List<AsmMethodParam> methodParams = methodBean.getParams();
|
List<AsmMethodParam> methodParams = methodBean.getParams();
|
||||||
List<Integer> insns = new ArrayList<>();
|
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);
|
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, method.getName(), methodBean.getDesc(), methodBean.getSignature(), null)).setDebug(false);
|
||||||
Label l0 = new Label();
|
Label l0 = new Label();
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
|
|||||||
* @param namespace 命名空间
|
* @param namespace 命名空间
|
||||||
* @param events 变更项
|
* @param events 变更项
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onEnvironmentChanged(String namespace, List<ResourceEvent> events) {
|
public void onEnvironmentChanged(String namespace, List<ResourceEvent> events) {
|
||||||
Set<String> sourceRemovedKeys = new HashSet<>();
|
Set<String> sourceRemovedKeys = new HashSet<>();
|
||||||
Properties sourceChangedProps = new Properties();
|
Properties sourceChangedProps = new Properties();
|
||||||
@@ -307,6 +308,7 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
|
|||||||
/**
|
/**
|
||||||
* 服务全部停掉后被调用
|
* 服务全部停掉后被调用
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onServersPostStop() {
|
public void onServersPostStop() {
|
||||||
for (DataSource source : dataSources) {
|
for (DataSource source : dataSources) {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
@@ -343,12 +345,14 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
|
|||||||
*
|
*
|
||||||
* @return CacheSource集合
|
* @return CacheSource集合
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Map<String, CacheSource> getCacheSources() {
|
public Map<String, CacheSource> getCacheSources() {
|
||||||
Map<String, CacheSource> sources = new HashMap<>();
|
Map<String, CacheSource> sources = new HashMap<>();
|
||||||
cacheSources.forEach(v -> sources.put(v.resourceName(), v));
|
cacheSources.forEach(v -> sources.put(v.resourceName(), v));
|
||||||
return sources;
|
return sources;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public CacheSource loadCacheSource(final String sourceName, boolean autoMemory) {
|
public CacheSource loadCacheSource(final String sourceName, boolean autoMemory) {
|
||||||
cacheSourceLock.lock();
|
cacheSourceLock.lock();
|
||||||
try {
|
try {
|
||||||
@@ -402,12 +406,14 @@ public class SourceModuleEngine extends ModuleEngine implements SourceManager {
|
|||||||
*
|
*
|
||||||
* @return DataSource集合
|
* @return DataSource集合
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Map<String, DataSource> getDataSources() {
|
public Map<String, DataSource> getDataSources() {
|
||||||
Map<String, DataSource> sources = new HashMap<>();
|
Map<String, DataSource> sources = new HashMap<>();
|
||||||
dataSources.forEach(v -> sources.put(v.resourceName(), v));
|
dataSources.forEach(v -> sources.put(v.resourceName(), v));
|
||||||
return sources;
|
return sources;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public DataSource loadDataSource(final String sourceName, boolean autoMemory) {
|
public DataSource loadDataSource(final String sourceName, boolean autoMemory) {
|
||||||
dataSourceLock.lock();
|
dataSourceLock.lock();
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user