diff --git a/src/main/java/aiyh/utils/Util.java b/src/main/java/aiyh/utils/Util.java index 909368b..cc5f158 100644 --- a/src/main/java/aiyh/utils/Util.java +++ b/src/main/java/aiyh/utils/Util.java @@ -2200,7 +2200,20 @@ public class Util extends weaver.general.Util { appender.setName("ayh_cus"); appender.setEncoding("UTF-8"); appender.setDatePattern("'_'yyyyMMdd'.log'"); - appender.setFile(weaver.general.GCONST.getLogPath() + "cus" + File.separator + "util_cus" + File.separator + "cus.log"); + String logPath = GCONST.getLogPath(); + try { + Map map = getProperties2Map("logPathConfig", "cus"); + if (map != null) { + if (map.containsKey("logPath")) { + logPath = "".equals(null2String(map.get("logPath"))) ? logPath : null2String(map.get("logPath")); + } + } + } catch (Exception ignore) { + } + if (!logPath.endsWith(File.separator)) { + logPath = logPath + File.separator; + } + appender.setFile(logPath + "cus" + File.separator + "util_cus" + File.separator + "cus.log"); appender.setThreshold(Priority.DEBUG); appender.setLayout(new PatternLayout("[%-5p] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%F.%M:%L] ==> %m %x %n")); appender.setAppend(true); @@ -2258,7 +2271,21 @@ public class Util extends weaver.general.Util { appender.setName("cus_" + name); appender.setEncoding("UTF-8"); appender.setDatePattern("'_'yyyyMMdd'.log'"); - appender.setFile(weaver.general.GCONST.getLogPath() + "cus" + File.separator + name + File.separator + "cus.log"); + String logPath = GCONST.getLogPath(); + try { + Map map = getProperties2Map("logPathConfig", "cus"); + if (map != null) { + if (map.containsKey("logPath")) { + logPath = "".equals(null2String(map.get("logPath"))) ? logPath : null2String(map.get("logPath")); + } + } + } catch (Exception ignore) { + + } + if (!logPath.endsWith(File.separator)) { + logPath = logPath + File.separator; + } + appender.setFile(logPath + "cus" + File.separator + name + File.separator + "cus.log"); appender.setThreshold(Priority.DEBUG); appender.setLayout(new PatternLayout("[%-5p] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%r] [%F.%M:%L] ==> \n %m %x %n")); appender.setAppend(true); @@ -3294,6 +3321,45 @@ public class Util extends weaver.general.Util { } } + public static String logStr(String logStr, String... args) { + try { + if (Strings.isNullOrEmpty(logStr)) { + return ""; + } + if (args == null || args.length == 0) { + return logStr; + } + String pattern = "\\{}"; + Pattern compile = Pattern.compile(pattern); + Matcher matcher = compile.matcher(logStr); + int n = 0; + while (matcher.find()) { + if (n + 1 > args.length) { + break; + } + logStr = logStr.replaceFirst(pattern, "{" + n++ + "}"); + } + try { + Object arg = args[args.length - 1]; + if (arg instanceof Throwable) { + for (int i = 0; i < args.length - 1; i++) { + pattern = "\\{" + i + "}"; + logStr = logStr.replaceFirst(pattern, Matcher.quoteReplacement(String.valueOf(args[i]))); + } + return logStr + "\n" + getErrString((Throwable) arg); + } + } catch (Exception ignore) { + } + for (int i = 0; i < args.length; i++) { + pattern = "\\{" + i + "}"; + logStr = logStr.replaceFirst(pattern, Matcher.quoteReplacement(String.valueOf(args[i]))); + } + return logStr; + } catch (Exception e) { + return logStr; + } + } + /** *

测试流程工具类

diff --git a/src/main/java/aiyh/utils/annotation/recordset/ResultMap.java b/src/main/java/aiyh/utils/annotation/recordset/ResultMap.java new file mode 100644 index 0000000..82f78ea --- /dev/null +++ b/src/main/java/aiyh/utils/annotation/recordset/ResultMap.java @@ -0,0 +1,25 @@ +package aiyh.utils.annotation.recordset; + +import java.lang.annotation.*; + +/** + *

结果集映射

+ * + *

create: 2023/3/16 10:07

+ * + * @author youHong.ai + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.ANNOTATION_TYPE) +@Documented +public @interface ResultMap { + + /** 原型 */ + String property() default ""; + + /** 数据库字段 */ + String column(); + + /** 原型类型 */ + Class javaType() default String.class; +} diff --git a/src/main/java/aiyh/utils/annotation/recordset/ResultMappings.java b/src/main/java/aiyh/utils/annotation/recordset/ResultMappings.java new file mode 100644 index 0000000..bcad6e7 --- /dev/null +++ b/src/main/java/aiyh/utils/annotation/recordset/ResultMappings.java @@ -0,0 +1,18 @@ +package aiyh.utils.annotation.recordset; + +import java.lang.annotation.*; + +/** + *

结果集映射

+ * + *

create: 2023/3/16 10:07

+ * + * @author youHong.ai + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Documented +public @interface ResultMappings { + + ResultMap[] value(); +} diff --git a/src/main/java/aiyh/utils/excention/CustomerException.java b/src/main/java/aiyh/utils/excention/CustomerException.java index 0d939bc..b0cb25f 100644 --- a/src/main/java/aiyh/utils/excention/CustomerException.java +++ b/src/main/java/aiyh/utils/excention/CustomerException.java @@ -26,6 +26,11 @@ public class CustomerException extends RuntimeException { this.msg = msg; } + public CustomerException(String msg, String... obj) { + super(Util.logStr(msg, obj)); + this.msg = Util.logStr(msg, obj); + } + public CustomerException(String msg, Integer code) { super(msg); this.code = code; @@ -33,7 +38,7 @@ public class CustomerException extends RuntimeException { } public CustomerException(String msg, Integer code, Throwable throwable) { - super(msg); + super(msg, throwable); this.code = code; this.msg = msg; } diff --git a/src/main/java/aiyh/utils/recordset/ResultMapper.java b/src/main/java/aiyh/utils/recordset/ResultMapper.java index 856db07..7e6977d 100644 --- a/src/main/java/aiyh/utils/recordset/ResultMapper.java +++ b/src/main/java/aiyh/utils/recordset/ResultMapper.java @@ -553,49 +553,45 @@ public class ResultMapper { Class propertyType = propertyDescriptor.getPropertyType(); Object value = null; String fieldName = propertyDescriptor.getName(); - - Field declaredField = o.getClass().getDeclaredField(fieldName); if (Strings.isNullOrEmpty(fieldName)) { fieldName = propertyDescriptor.getDisplayName(); } + Field declaredField = o.getClass().getDeclaredField(fieldName); + if (method.isAnnotationPresent(Associations.class)) { Association association = searchAssociation(method, fieldName, false); if (association != null) { - if (association.property().equals(fieldName)) { - Object cassociationValue = association(rs, association, method); - if (cassociationValue == null) { - continue; - } - if (paramType.containsKey(declaredField.getType())) { - cassociationValue = paramType.get(declaredField.getType()).apply(String.valueOf(cassociationValue)); - } - try { - propertyDescriptor.getWriteMethod().invoke(o, cassociationValue); - } catch (Exception e) { - Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value); - if (value != null) { - Util.getLogger().error("查询数据类型: " + value.getClass()); - } - } + Object cassociationValue = association(rs, association, method); + if (cassociationValue == null) { continue; } + if (paramType.containsKey(declaredField.getType())) { + cassociationValue = paramType.get(declaredField.getType()).apply(String.valueOf(cassociationValue)); + } + try { + propertyDescriptor.getWriteMethod().invoke(o, cassociationValue); + } catch (Exception e) { + Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value); + if (value != null) { + Util.getLogger().error("查询数据类型: " + value.getClass()); + } + } + continue; } } if (method.isAnnotationPresent(CollectionMappings.class)) { CollectionMapping collectionMapping = searchCollection(method, fieldName, false); if (collectionMapping != null) { - if (fieldName.equals(collectionMapping.property()) && !"".equals(collectionMapping.property())) { - Object collection = collection(rs, collectionMapping, method); - try { - propertyDescriptor.getWriteMethod().invoke(o, collection); - } catch (Exception e) { - Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value); - if (value != null) { - Util.getLogger().error("查询数据类型: " + value.getClass()); - } + Object collection = collection(rs, collectionMapping, method); + try { + propertyDescriptor.getWriteMethod().invoke(o, collection); + } catch (Exception e) { + Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value); + if (value != null) { + Util.getLogger().error("查询数据类型: " + value.getClass()); } - continue; } + continue; } } TypeHandler typeHandler = ResultMapper.typeHandler.get(propertyType); @@ -651,6 +647,7 @@ public class ResultMapper { Association[] mappings = annotation.value(); Association mapping = null; for (Association item : mappings) { + Util.getLogger().info("column: " + item.column() + " ===property: " + item.property() + " ====fieldName: " + filedName); String property = isMap ? item.column() : item.property(); if (isMap ? filedName.equalsIgnoreCase(property) : filedName.equals(property)) { mapping = item; @@ -663,6 +660,7 @@ public class ResultMapper { Id id = annotation.id(); String column = annotation.column(); String columnValue = rs.getString(column); + Util.getLogger().info(Arrays.toString(rs.getColumnName())); if (Objects.isNull(columnValue) || "".equals(columnValue)) { columnValue = rs.getString(column.toUpperCase()); } diff --git a/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java b/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java index e67da90..333eaa4 100644 --- a/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java +++ b/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java @@ -21,218 +21,218 @@ import java.util.concurrent.TimeUnit; */ public class RsThreadLocalManager { - - private static final ConcurrentHashMap> rsMap = new ConcurrentHashMap<>(); - - private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); - - public RsThreadLocalManager() { - startMonitor(); - } - - /** - *

定时清除过期的rs对象

- * 2022/12/21 22:02 - * ************************************************************ - * - * @author youHong.ai ****************************************** - */ - private void startMonitor() { - executor.scheduleAtFixedRate(this::checkExpireRs, 0, 2, TimeUnit.MINUTES); - } - - /** - *

checkExpireRs 检查清除过期的rs对象

- * 2022/12/21 22:02 - * ************************************************************ - * - * @author youHong.ai ****************************************** - */ - private void checkExpireRs() { - Iterator>> iterator = rsMap.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry> entity = iterator.next(); - Map map = entity.getValue(); - Iterator> mapIterator = map.entrySet().iterator(); - while (mapIterator.hasNext()) { - Map.Entry mapEntity = mapIterator.next(); - RsThreadLocalMap value = mapEntity.getValue(); - if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) { - mapIterator.remove(); - } - } - if (map.isEmpty()) { - iterator.remove(); - } - } - } - - /** - *

setRecordSet 设置当前线程的rs对象

- * 2022/12/21 22:03 - * ************************************************************ - * - * @author youHong.ai ****************************************** - */ - public void setRecordSet(String className) { - RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(new RecordSet(), getExpireTime()); - setRecordSetOrTrans(className, rsThreadLocalMap); - } - - private void setRecordSetOrTrans(String className, RsThreadLocalMap rsThreadLocalMap) { - Map map = rsMap.get(Thread.currentThread().getId()); - if (Objects.isNull(map)) { - map = new ConcurrentHashMap<>(); - } - map.put(className, rsThreadLocalMap); - rsMap.put(Thread.currentThread().getId(), map); - } - - /** - *

setRecordSet 设置当前线程的事务对象

- * 2022/12/21 22:03 - * ************************************************************ - * - * @author youHong.ai ****************************************** - */ - public void setRecordSetTrans(String className) { - RecordSetTrans recordSetTrans = new RecordSetTrans(); - recordSetTrans.setAutoCommit(false); - RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(recordSetTrans, -1L); - setRecordSetOrTrans(className, rsThreadLocalMap); - } - - /** - *

getExpireTime 获取过期时间

- * 2022/12/21 22:06 - * ************************************************************ - * - * @return Long - * @author youHong.ai ****************************************** - */ - public Long getExpireTime() { - return System.currentTimeMillis() + 1000L * 60 * 3; - } - - - /** - *

getRs 获取当前线程的rs对象

- * 2022/12/21 22:04 - * ************************************************************ - * - * @return RecordSet - * @author youHong.ai ****************************************** - */ - public RecordSet getRs(String className) { - Map map = rsMap.get(Thread.currentThread().getId()); - if (Objects.isNull(map)) { - return null; - } - RsThreadLocalMap rsThreadLocalMap = map.get(className); - if (Objects.isNull(rsThreadLocalMap)) { - return null; - } - rsThreadLocalMap.setExpireTime(getExpireTime()); - return rsThreadLocalMap.getRecordSet(); - } - - /** - *

getTrans 获取当前线程的事务rs

- * 2022/12/21 22:04 - * ************************************************************ - * - * @return RecordSetTrans 事务rs - * @author youHong.ai ****************************************** - */ - public RecordSetTrans getTrans(String className) { - Map map = rsMap.get(Thread.currentThread().getId()); - if (Objects.isNull(map)) { - return null; - } - RsThreadLocalMap rsThreadLocalMap = map.get(className); - if (Objects.isNull(rsThreadLocalMap)) { - return null; - } - rsThreadLocalMap.setExpireTime(-1L); - return rsThreadLocalMap.getRecordSetTrans(); - } - - public boolean commit(String className) { - RecordSetTrans recordSetTrans = getRecordSetTrans(className); - return recordSetTrans.commit(); - } - - private RecordSetTrans getRecordSetTrans(String className) { - Map map = rsMap.get(Thread.currentThread().getId()); - if (Objects.isNull(map)) { - throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); - } - RsThreadLocalMap rsThreadLocalMap = map.get(className); - if (Objects.isNull(rsThreadLocalMap)) { - throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); - } - rsThreadLocalMap.setExpireTime(getExpireTime()); - RecordSetTrans recordSetTrans = rsThreadLocalMap.getRecordSetTrans(); - return recordSetTrans; - } - - - public boolean rollback(String className) { - RecordSetTrans recordSetTrans = getRecordSetTrans(className); - return recordSetTrans.rollback(); - } - - - /** - *

remove 删除当前线程的rs对象

- * 2022/12/21 22:05 - * ************************************************************ - * - * @author youHong.ai ****************************************** - */ - public void remove() { - rsMap.remove(Thread.currentThread().getId()); - } - - - static class RsThreadLocalMap { - private RecordSet recordSet; - - private RecordSetTrans recordSetTrans; - private Long expireTime; - - public RsThreadLocalMap(RecordSet recordSet, Long expireTime) { - this.recordSet = recordSet; - this.expireTime = expireTime; - } - - public RsThreadLocalMap(RecordSetTrans recordSetTrans, Long expireTime) { - this.recordSetTrans = recordSetTrans; - this.expireTime = expireTime; - } - - public RecordSetTrans getRecordSetTrans() { - return recordSetTrans; - } - - public void setRecordSetTrans(RecordSetTrans recordSetTrans) { - this.recordSetTrans = recordSetTrans; - } - - public RecordSet getRecordSet() { - return recordSet; - } - - public void setRecordSet(RecordSet recordSet) { - this.recordSet = recordSet; - } - - public Long getExpireTime() { - return expireTime; - } - - public void setExpireTime(Long expireTime) { - this.expireTime = expireTime; - } - } + + private static final ConcurrentHashMap> rsMap = new ConcurrentHashMap<>(); + + private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + + public RsThreadLocalManager() { + startMonitor(); + } + + /** + *

定时清除过期的rs对象

+ * 2022/12/21 22:02 + * ************************************************************ + * + * @author youHong.ai ****************************************** + */ + private void startMonitor() { + executor.scheduleAtFixedRate(this::checkExpireRs, 0, 2, TimeUnit.MINUTES); + } + + /** + *

checkExpireRs 检查清除过期的rs对象

+ * 2022/12/21 22:02 + * ************************************************************ + * + * @author youHong.ai ****************************************** + */ + private void checkExpireRs() { + Iterator>> iterator = rsMap.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry> entity = iterator.next(); + Map map = entity.getValue(); + Iterator> mapIterator = map.entrySet().iterator(); + while (mapIterator.hasNext()) { + Map.Entry mapEntity = mapIterator.next(); + RsThreadLocalMap value = mapEntity.getValue(); + if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) { + mapIterator.remove(); + } + } + if (map.isEmpty()) { + iterator.remove(); + } + } + } + + /** + *

setRecordSet 设置当前线程的rs对象

+ * 2022/12/21 22:03 + * ************************************************************ + * + * @author youHong.ai ****************************************** + */ + public void setRecordSet(String className) { + RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(new RecordSet(), getExpireTime()); + setRecordSetOrTrans(className, rsThreadLocalMap); + } + + private void setRecordSetOrTrans(String className, RsThreadLocalMap rsThreadLocalMap) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + map = new ConcurrentHashMap<>(); + } + map.put(className, rsThreadLocalMap); + rsMap.put(Thread.currentThread().getId(), map); + } + + /** + *

setRecordSet 设置当前线程的事务对象

+ * 2022/12/21 22:03 + * ************************************************************ + * + * @author youHong.ai ****************************************** + */ + public void setRecordSetTrans(String className) { + RecordSetTrans recordSetTrans = new RecordSetTrans(); + recordSetTrans.setAutoCommit(false); + RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(recordSetTrans, -1L); + setRecordSetOrTrans(className, rsThreadLocalMap); + } + + /** + *

getExpireTime 获取过期时间

+ * 2022/12/21 22:06 + * ************************************************************ + * + * @return Long + * @author youHong.ai ****************************************** + */ + public Long getExpireTime() { + return System.currentTimeMillis() + 1000L * 60 * 3; + } + + + /** + *

getRs 获取当前线程的rs对象

+ * 2022/12/21 22:04 + * ************************************************************ + * + * @return RecordSet + * @author youHong.ai ****************************************** + */ + public RecordSet getRs(String className) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + return null; + } + RsThreadLocalMap rsThreadLocalMap = map.get(className); + if (Objects.isNull(rsThreadLocalMap)) { + return null; + } + rsThreadLocalMap.setExpireTime(getExpireTime()); + return rsThreadLocalMap.getRecordSet(); + } + + /** + *

getTrans 获取当前线程的事务rs

+ * 2022/12/21 22:04 + * ************************************************************ + * + * @return RecordSetTrans 事务rs + * @author youHong.ai ****************************************** + */ + public RecordSetTrans getTrans(String className) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + return null; + } + RsThreadLocalMap rsThreadLocalMap = map.get(className); + if (Objects.isNull(rsThreadLocalMap)) { + return null; + } + rsThreadLocalMap.setExpireTime(-1L); + return rsThreadLocalMap.getRecordSetTrans(); + } + + public boolean commit(String className) { + RecordSetTrans recordSetTrans = getRecordSetTrans(className); + return recordSetTrans.commit(); + } + + private RecordSetTrans getRecordSetTrans(String className) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); + } + RsThreadLocalMap rsThreadLocalMap = map.get(className); + if (Objects.isNull(rsThreadLocalMap)) { + throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); + } + rsThreadLocalMap.setExpireTime(getExpireTime()); + RecordSetTrans recordSetTrans = rsThreadLocalMap.getRecordSetTrans(); + return recordSetTrans; + } + + + public boolean rollback(String className) { + RecordSetTrans recordSetTrans = getRecordSetTrans(className); + return recordSetTrans.rollback(); + } + + + /** + *

remove 删除当前线程的rs对象

+ * 2022/12/21 22:05 + * ************************************************************ + * + * @author youHong.ai ****************************************** + */ + public void remove() { + rsMap.remove(Thread.currentThread().getId()); + } + + + static class RsThreadLocalMap { + private RecordSet recordSet; + + private RecordSetTrans recordSetTrans; + private Long expireTime; + + public RsThreadLocalMap(RecordSet recordSet, Long expireTime) { + this.recordSet = recordSet; + this.expireTime = expireTime; + } + + public RsThreadLocalMap(RecordSetTrans recordSetTrans, Long expireTime) { + this.recordSetTrans = recordSetTrans; + this.expireTime = expireTime; + } + + public RecordSetTrans getRecordSetTrans() { + return recordSetTrans; + } + + public void setRecordSetTrans(RecordSetTrans recordSetTrans) { + this.recordSetTrans = recordSetTrans; + } + + public RecordSet getRecordSet() { + return recordSet; + } + + public void setRecordSet(RecordSet recordSet) { + this.recordSet = recordSet; + } + + public Long getExpireTime() { + return expireTime; + } + + public void setExpireTime(Long expireTime) { + this.expireTime = expireTime; + } + } } diff --git a/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandQuanJinSaveAction.java b/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandQuanJinSaveAction.java index d3b8e29..e4aaed4 100644 --- a/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandQuanJinSaveAction.java +++ b/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandQuanJinSaveAction.java @@ -1,7 +1,6 @@ package weaver.youhong.ai.yihong.formmode.stagediagram; import aiyh.utils.Util; -import com.alibaba.fastjson.JSON; import org.apache.log4j.Logger; import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew; import weaver.youhong.ai.yihong.formmode.stagediagram.service.ModeExpandSaveService; @@ -26,7 +25,6 @@ public class ModeExpandQuanJinSaveAction extends AbstractModeExpandJavaCodeNew { Map result = new HashMap<>(8); ModeExpandSaveService service = new ModeExpandSaveService(); try { - log.info("自定义接口保存动作参数: " + JSON.toJSONString(param)); service.updateStageDiagramView(param, "STAGE_DIAGRAM_AMOUNT_TABLE_QJ", "STAGE_DIAGRAM_PROJECT_ID_FIELD_QJ", "STAGE_DIAGRAM_AMOUNT_FIELD_QJ", "MAPPING_CONFIG_MARK_QJ"); diff --git a/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandSaveAction.java b/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandSaveAction.java index a7735e0..ee609c3 100644 --- a/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandSaveAction.java +++ b/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/ModeExpandSaveAction.java @@ -1,7 +1,6 @@ package weaver.youhong.ai.yihong.formmode.stagediagram; import aiyh.utils.Util; -import com.alibaba.fastjson.JSON; import org.apache.log4j.Logger; import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew; import weaver.youhong.ai.yihong.formmode.stagediagram.service.ModeExpandSaveService; @@ -26,7 +25,6 @@ public class ModeExpandSaveAction extends AbstractModeExpandJavaCodeNew { Map result = new HashMap<>(8); ModeExpandSaveService service = new ModeExpandSaveService(); try { - log.info("自定义接口保存动作参数: " + JSON.toJSONString(param)); service.updateStageDiagramView(param, "STAGE_DIAGRAM_AMOUNT_TABLE", "STAGE_DIAGRAM_PROJECT_ID_FIELD", "STAGE_DIAGRAM_AMOUNT_FIELD", "MAPPING_CONFIG_MARK"); diff --git a/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/service/ModeExpandSaveService.java b/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/service/ModeExpandSaveService.java index f327ddd..f59097f 100644 --- a/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/service/ModeExpandSaveService.java +++ b/src/main/java/weaver/youhong/ai/yihong/formmode/stagediagram/service/ModeExpandSaveService.java @@ -2,7 +2,7 @@ package weaver.youhong.ai.yihong.formmode.stagediagram.service; import aiyh.utils.Util; import aiyh.utils.tool.cn.hutool.core.lang.Assert; -import com.alibaba.fastjson.JSON; +import aiyh.utils.tool.cn.hutool.core.util.StrUtil; import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; import weaver.soa.workflow.request.Property; @@ -41,17 +41,14 @@ public class ModeExpandSaveService { RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo"); Map mainTableValue = getMainTableValue(requestInfo); String projectId = mainTableValue.get(stageDiagramProjectFieldName); - log.info("主表数据:" + JSON.toJSONString(mainTableValue)); // 获取当前模块id /* ******************* 查询台账中对应的project是否存在, 台账中不存在则插入,存在则更新 ******************* */ StageNodeInfo nodeInfo = mapper.selectStageNodeInfoByProjectId(projectId); - log.info("查询到的nodeInFo数据:" + JSON.toJSONString(nodeInfo)); if (Objects.isNull(nodeInfo)) { // 不存在项目信息在台账中插入项目信息到台账信息中 Integer dataId = Util.getModeDataId("uf_stage_node_info", 1); // 查询配置表信息 String nodeName = (String) currentNodeConfig.get("nodeName"); - log.info("节点名称: " + nodeName); String amount = mapper.selectAmountByProjectId(projectId, Util.getCusConfigValueNullOrEmpty(projectIdField, ""), Util.getCusConfigValueNullOrEmpty(amountTable, ""), @@ -76,6 +73,9 @@ public class ModeExpandSaveService { } /* ******************* 更新其他字段 ******************* */ String onlyMark = Util.getCusConfigValue(mappingConfigMark); + if (StrUtil.isBlank(onlyMark)) { + return; + } StageUpdateFieldConfig stageUpdateFieldConfig = mapper.selectConfigUpdate(onlyMark); if (stageUpdateFieldConfig == null) { return; diff --git a/src/main/resources/WEB-INF/prop/prop2map/logPathConfig.properties b/src/main/resources/WEB-INF/prop/prop2map/logPathConfig.properties new file mode 100644 index 0000000..281dfb1 --- /dev/null +++ b/src/main/resources/WEB-INF/prop/prop2map/logPathConfig.properties @@ -0,0 +1 @@ +cus.logPath=/applog/tongweb/ecology_log/ \ No newline at end of file diff --git a/src/test/java/youhong/ai/mymapper/MyMapper.java b/src/test/java/youhong/ai/mymapper/MyMapper.java new file mode 100644 index 0000000..0792792 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/MyMapper.java @@ -0,0 +1,54 @@ +package youhong.ai.mymapper; + + +import aiyh.utils.annotation.recordset.SqlMapper; +import aiyh.utils.excention.BindingException; +import youhong.ai.mymapper.proxy.MyMapperProxy; + +import java.lang.reflect.Proxy; + +/** + *

myMapper工具类

+ * + *

create: 2023/3/15 14:04

+ * + * @author youHong.ai + */ +public class MyMapper { + + + /** + *

获取代理对象

+ * + * @param tClass 需要代理的对象的class + * @param autoCommit 是否自动提交,true -自动提交 false - 不自动提交开启事务 + * @param 范型 + * @return 代理对象 + */ + @SuppressWarnings("all") + public static T getMapper(Class tClass, boolean autoCommit) { + if (tClass == null) { + throw new BindingException("class is null!"); + } + if (tClass.getAnnotation(SqlMapper.class) == null) { + throw new BindingException("can not find SqlMapper annotation!"); + } + return (T) Proxy.newProxyInstance( + tClass.getClassLoader(), + new Class[]{tClass}, + new MyMapperProxy(autoCommit) + ); + } + + /** + *

获取代理对象

+ * + * @param tClass 需要代理的对象的class + * @param 范型 + * @return 代理对象 + */ + @SuppressWarnings("all") + public static T getMapper(Class tClass) { + return getMapper(tClass, true); + } +} diff --git a/src/test/java/youhong/ai/mymapper/ParseSqlTest.java b/src/test/java/youhong/ai/mymapper/ParseSqlTest.java index 5f8c72e..3afc2c0 100644 --- a/src/test/java/youhong/ai/mymapper/ParseSqlTest.java +++ b/src/test/java/youhong/ai/mymapper/ParseSqlTest.java @@ -112,4 +112,11 @@ public class ParseSqlTest extends BaseTest { SqlDefinition parse = parseSqlUtil.parse(sql, param); System.out.println(JSON.toJSONString(parse)); } + + + @Test + public void testArrayClass() { + String[] strs = new String[0]; + System.out.println(strs.getClass().getComponentType()); + } } diff --git a/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java b/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java index 27c0979..ee83d76 100644 --- a/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java +++ b/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java @@ -46,4 +46,14 @@ public class CommandConsTant { public static final String WHEN = "when"; public static final String OTHERWISE = "otherwise"; public static final String BIND = "bind"; + + public static final String SELECT_SQL = "select"; + + public static final String UPDATE_SQL = "update"; + + public static final String DELETE_SQL = "delete"; + + public static final String INSERT_SQL = "insert"; + + } diff --git a/src/test/java/youhong/ai/mymapper/command/entity/SqlDefinition.java b/src/test/java/youhong/ai/mymapper/command/entity/SqlDefinition.java index 74274ec..44c9732 100644 --- a/src/test/java/youhong/ai/mymapper/command/entity/SqlDefinition.java +++ b/src/test/java/youhong/ai/mymapper/command/entity/SqlDefinition.java @@ -19,4 +19,5 @@ import java.util.List; public class SqlDefinition { private String sql; private List args; + } diff --git a/src/test/java/youhong/ai/mymapper/proxy/MapperProxyExecute.java b/src/test/java/youhong/ai/mymapper/proxy/MapperProxyExecute.java new file mode 100644 index 0000000..d86ae84 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/proxy/MapperProxyExecute.java @@ -0,0 +1,226 @@ +package youhong.ai.mymapper.proxy; + +import aiyh.utils.Util; +import aiyh.utils.annotation.recordset.*; +import aiyh.utils.excention.CustomerException; +import aiyh.utils.tool.cn.hutool.core.util.StrUtil; +import org.apache.log4j.Logger; +import weaver.conn.RecordSet; +import weaver.conn.RecordSetTrans; +import weaver.conn.constant.DBConstant; +import youhong.ai.mymapper.command.constant.CommandConsTant; +import youhong.ai.mymapper.command.entity.SqlDefinition; +import youhong.ai.mymapper.proxy.entity.SqlAndExecuteType; +import youhong.ai.mymapper.proxy.entity.SqlDbTypeMapping; +import youhong.ai.mymapper.util.ParseSqlUtil; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +/** + *

代理执行

+ * + *

create: 2023/3/15 14:34

+ * + * @author youHong.ai + */ +public class MapperProxyExecute { + + private final Logger logger = Util.getLogger(); + private final List> SELECT_LIST = Arrays.asList(Select.class, SelectOracle.class); + private final List> UPDATE_LIST = Arrays.asList(Update.class, UpdateOracle.class); + private final List> DELETE_LIST = Arrays.asList(Delete.class, DeleteOracle.class); + private final List> INSERT_LIST = Arrays.asList(Insert.class, InsertOracle.class); + + private static final Map INSERT_MAP = new HashMap<>(); + private static final Map SELECT_MAP = new HashMap<>(); + private static final Map DELETE_MAP = new HashMap<>(); + private static final Map UPDATE_MAP = new HashMap<>(); + + static { + INSERT_MAP.put(DBConstant.DB_TYPE_ORACLE, + SqlDbTypeMapping.builder() + .getSql(method -> method.getAnnotation(InsertOracle.class).value()) + .isCustomer(method -> method.getAnnotation(InsertOracle.class).custom()) + .build()); + SELECT_MAP.put(DBConstant.DB_TYPE_ORACLE, + SqlDbTypeMapping.builder() + .getSql(method -> method.getAnnotation(SelectOracle.class).value()) + .isCustomer(method -> method.getAnnotation(SelectOracle.class).custom()) + .build()); + DELETE_MAP.put(DBConstant.DB_TYPE_ORACLE, + SqlDbTypeMapping.builder() + .getSql(method -> method.getAnnotation(DeleteOracle.class).value()) + .isCustomer(method -> method.getAnnotation(DeleteOracle.class).custom()) + .build()); + UPDATE_MAP.put(DBConstant.DB_TYPE_ORACLE, + SqlDbTypeMapping.builder() + .getSql(method -> method.getAnnotation(UpdateOracle.class).value()) + .isCustomer(method -> method.getAnnotation(UpdateOracle.class).custom()) + .build()); + + } + + public Object execute(RecordSet rs, Method method, Object[] args) { + SqlAndExecuteType sqlAndExecuteType = null; + try { + sqlAndExecuteType = getSqlStr(rs.getDBType(), method, args); + } catch (NullPointerException e) { + logger.error(Util.getErrString(e)); + throw new CustomerException("该方法没有正确添加注解!请检查是否正确添加注解!@Select、@Update、@Insert、@Delete、@BatchUpdate、@BatchInsert、@BatchDelete"); + } + if (sqlAndExecuteType == null || StrUtil.isBlank(sqlAndExecuteType.getSql())) { + throw new CustomerException("can not find sql string and sql annotation in method: " + + method.getDeclaringClass().getName() + "." + method.getName()); + } + Map sqlParam = getSqlParam(method, args); + ParseSqlUtil parseSqlUtil = new ParseSqlUtil(); + SqlDefinition parse = parseSqlUtil.parse(sqlAndExecuteType.getSql(), sqlParam); + sqlAndExecuteType.setMethod(method); + sqlAndExecuteType.setArgs(parse.getArgs()); + sqlAndExecuteType.setSql(parse.getSql()); + sqlAndExecuteType.setRecordSet(rs); + return SqlExecutor.execute(sqlAndExecuteType); + } + + public Object execute(RecordSetTrans rs, Method method, Object[] args) { + SqlAndExecuteType sql = getSqlStr(rs.getDBType(), method, args); + return null; + } + + private SqlAndExecuteType getSqlStr(String dbType, Method method, Object[] args) { + Annotation[] annotations = method.getAnnotations(); + for (Annotation annotation : annotations) { + if (SELECT_LIST.contains(annotation.getClass())) { + // select 处理 + return selectSql(dbType, method, args); + } + if (UPDATE_LIST.contains(annotation.getClass())) { + // update 处理 + return updateSql(dbType, method, args); + } + if (DELETE_LIST.contains(annotation.getClass())) { + // delete 处理 + return deleteSql(dbType, method, args); + } + if (INSERT_LIST.contains(annotation.getClass())) { + // insert 处理 + return insertSql(dbType, method, args); + } + } + + return null; + } + + private SqlAndExecuteType insertSql(String dbType, Method method, Object[] args) { + SqlDbTypeMapping mapping = new SqlDbTypeMapping(); + mapping.setIsCustomerDefaultSql(funMethod -> funMethod.getAnnotation(Insert.class).custom()); + mapping.setGetDefaultSql(funMethod -> funMethod.getAnnotation(Insert.class).value()); + mapping.setMappingMap(INSERT_MAP); + String sql = getSql(dbType, method, args, mapping); + SqlAndExecuteType sqlAndExecuteType = new SqlAndExecuteType(); + sqlAndExecuteType.setSql(sql); + sqlAndExecuteType.setType(CommandConsTant.INSERT_SQL); + return sqlAndExecuteType; + } + + private SqlAndExecuteType deleteSql(String dbType, Method method, Object[] args) { + SqlDbTypeMapping mapping = new SqlDbTypeMapping(); + mapping.setIsCustomerDefaultSql(funMethod -> funMethod.getAnnotation(Delete.class).custom()); + mapping.setGetDefaultSql(funMethod -> funMethod.getAnnotation(Delete.class).value()); + mapping.setMappingMap(DELETE_MAP); + String sql = getSql(dbType, method, args, mapping); + SqlAndExecuteType sqlAndExecuteType = new SqlAndExecuteType(); + sqlAndExecuteType.setSql(sql); + sqlAndExecuteType.setType(CommandConsTant.DELETE_SQL); + return sqlAndExecuteType; + } + + private SqlAndExecuteType updateSql(String dbType, Method method, Object[] args) { + SqlDbTypeMapping mapping = new SqlDbTypeMapping(); + mapping.setIsCustomerDefaultSql(funMethod -> funMethod.getAnnotation(Update.class).custom()); + mapping.setGetDefaultSql(funMethod -> funMethod.getAnnotation(Update.class).value()); + mapping.setMappingMap(UPDATE_MAP); + String sql = getSql(dbType, method, args, mapping); + SqlAndExecuteType sqlAndExecuteType = new SqlAndExecuteType(); + sqlAndExecuteType.setSql(sql); + sqlAndExecuteType.setType(CommandConsTant.UPDATE_SQL); + return sqlAndExecuteType; + } + + private SqlAndExecuteType selectSql(String dbType, Method method, Object[] args) { + SqlDbTypeMapping mapping = new SqlDbTypeMapping(); + mapping.setIsCustomerDefaultSql(funMethod -> funMethod.getAnnotation(Select.class).custom()); + mapping.setGetDefaultSql(funMethod -> funMethod.getAnnotation(Select.class).value()); + mapping.setMappingMap(SELECT_MAP); + String sql = getSql(dbType, method, args, mapping); + SqlAndExecuteType sqlAndExecuteType = new SqlAndExecuteType(); + sqlAndExecuteType.setSql(sql); + sqlAndExecuteType.setType(CommandConsTant.SELECT_SQL); + return sqlAndExecuteType; + } + + + private String getSql(String dbType, + Method method, + Object[] args, + SqlDbTypeMapping mapping) { + Function getDefaultSql = mapping.getGetDefaultSql(); + Function isCustomerDefaultSql = mapping.getIsCustomerDefaultSql(); + Map mappingMap = mapping.getMappingMap(); + if (!mappingMap.containsKey(dbType)) { + if (isCustomerDefaultSql.apply(method)) { + return getSqlByParam(method, args); + } + return getDefaultSql.apply(method); + } + SqlDbTypeMapping sqlDbTypeMapping = mappingMap.get(dbType); + Function isCustomer = sqlDbTypeMapping.getIsCustomer(); + if (isCustomer.apply(method)) { + return getSqlByParam(method, args); + } + return sqlDbTypeMapping.getGetSql().apply(method); + } + + + private String getSqlByParam(Method method, Object[] args) { + Parameter[] parameters = method.getParameters(); + for (int i = 0; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + if (parameter.isAnnotationPresent(SqlString.class)) { + Object sqlObj = args[i]; + if (sqlObj instanceof String) { + return String.valueOf(sqlObj); + } + } + } + throw new CustomerException("can not find sql string and sql annotation in method: " + + method.getDeclaringClass().getName() + "." + method.getName()); + } + + + private Map getSqlParam(Method method, Object[] args) { + Map map = new HashMap<>(); + Parameter[] parameters = method.getParameters(); + for (int i = 0; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + if (parameter.isAnnotationPresent(SqlString.class)) { + continue; + } + if (parameter.isAnnotationPresent(ParamMapper.class)) { + ParamMapper annotation = parameter.getAnnotation(ParamMapper.class); + String name = annotation.value(); + map.put(name, args[i]); + } + map.put("var" + i, args[i]); + map.put("param" + i, args[i]); + } + return map; + } +} diff --git a/src/test/java/youhong/ai/mymapper/proxy/MyMapperProxy.java b/src/test/java/youhong/ai/mymapper/proxy/MyMapperProxy.java new file mode 100644 index 0000000..f576d57 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/proxy/MyMapperProxy.java @@ -0,0 +1,87 @@ +package youhong.ai.mymapper.proxy; + +import aiyh.utils.Util; +import aiyh.utils.recordset.RsThreadLocalManager; +import org.apache.log4j.Logger; +import weaver.conn.RecordSet; +import weaver.conn.RecordSetTrans; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + *

代理类

+ * + *

create: 2023/3/15 14:01

+ * + * @author youHong.ai + */ +public class MyMapperProxy implements InvocationHandler { + + public static final String SQL_LOG = "sql_log"; + private final RsThreadLocalManager rsManager = new RsThreadLocalManager(); + + private final MapperProxyExecute mapperProxyExecute = new MapperProxyExecute(); + private Logger logger = null; + + private final boolean autoCommit; + + public MyMapperProxy(boolean autoCommit) { + this.autoCommit = autoCommit; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) { + if (logger == null) { + logger = Util.getLogger(SQL_LOG); + + } + if (this.autoCommit) { + return invokeRs(proxy, method, args, null); + } + return invokeTrans(proxy, method, args, null); + } + + /** + *

事务的代理对象

+ * + * @param proxy 代理对象 + * @param method 执行方法 + * @param args 方法参数 + * @return 执行结果 + */ + public Object invokeTrans(Object proxy, Method method, Object[] args, String name) { + String mapperKey = "myMapper:" + method.getDeclaringClass().getName(); + if (!"".equals(name) && null != name) { + mapperKey += "." + name; + } + RecordSet rs = rsManager.getRs(mapperKey); + if (rs == null) { + rsManager.setRecordSet(mapperKey); + rs = rsManager.getRs(mapperKey); + } + return mapperProxyExecute.execute(rs, method, args); + } + + /** + *

代理对象

+ * + * @param proxy 代理对象 + * @param method 执行方法 + * @param args 方法参数 + * @return 执行结果 + */ + public Object invokeRs(Object proxy, Method method, Object[] args, String name) { + String mapperKey = "myMapper:" + method.getDeclaringClass().getName(); + if (!"".equals(name) && null != name) { + mapperKey += "." + name; + } + RecordSetTrans rs = rsManager.getTrans(mapperKey); + if (rs == null) { + rsManager.setRecordSetTrans(mapperKey); + rs = rsManager.getTrans(mapperKey); + } + return mapperProxyExecute.execute(rs, method, args); + } + +} diff --git a/src/test/java/youhong/ai/mymapper/proxy/SqlExecutor.java b/src/test/java/youhong/ai/mymapper/proxy/SqlExecutor.java new file mode 100644 index 0000000..7dec420 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/proxy/SqlExecutor.java @@ -0,0 +1,99 @@ +package youhong.ai.mymapper.proxy; + +import aiyh.utils.annotation.MethodRuleNo; +import aiyh.utils.excention.CustomerException; +import aiyh.utils.tool.cn.hutool.core.collection.CollectionUtil; +import weaver.conn.RecordSet; +import youhong.ai.mymapper.command.constant.CommandConsTant; +import youhong.ai.mymapper.proxy.entity.SqlAndExecuteType; +import youhong.ai.mymapper.util.ResultMapperUtil; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +/** + *

sql执行器

+ * + *

create: 2023/3/15 17:33

+ * + * @author youHong.ai + */ +public class SqlExecutor { + + + private static final Map> EXECUTE_MAP = new HashMap<>(); + + + /* ******************* 初始化参数 ******************* */ static { + Class valueRuleMethodClass = SqlExecutor.class; + Method[] methods = valueRuleMethodClass.getDeclaredMethods(); + SqlExecutor factory = new SqlExecutor(); + for (Method method : methods) { + method.setAccessible(true); + if (method.isAnnotationPresent(MethodRuleNo.class)) { + MethodRuleNo annotation = method.getAnnotation(MethodRuleNo.class); + String value = annotation.name(); + EXECUTE_MAP.put(value, sqlDefinition -> { + try { + return method.invoke(factory, sqlDefinition); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + }); + } + } + } + + public static Object execute(SqlAndExecuteType sqlDefinition) { + String executeType = sqlDefinition.getExecuteType(); + if (!EXECUTE_MAP.containsKey(executeType)) { + throw new CustomerException("Unsupported sql execution type!" + executeType); + } + Function fun = EXECUTE_MAP.get(executeType); + return fun.apply(sqlDefinition); + } + + + @MethodRuleNo(name = CommandConsTant.SELECT_SQL, desc = "查询sql执行方法") + private Object executeSelect(SqlAndExecuteType sqlDefinition) { + ResultMapperUtil resultMapperUtil = new ResultMapperUtil(); + if (!sqlDefinition.isTrans()) { + RecordSet recordSet = sqlDefinition.getRecordSet(); + List args = sqlDefinition.getArgs(); + if (CollectionUtil.isEmpty(args)) { + recordSet.executeQuery(sqlDefinition.getSql()); + } else { + recordSet.executeQuery(sqlDefinition.getSql(), args); + } + return resultMapperUtil.parseResult(sqlDefinition.getMethod(), recordSet); + } else { + // 事务查询 + } + return null; + } + + @MethodRuleNo(name = CommandConsTant.INSERT_SQL, desc = "新增sql执行方法") + private Object executeInsert(SqlAndExecuteType sqlDefinition) { + + return null; + } + + + @MethodRuleNo(name = CommandConsTant.UPDATE_SQL, desc = "更新sql执行方法") + private Object executeUpdate(SqlAndExecuteType sqlDefinition) { + + return null; + } + + @MethodRuleNo(name = CommandConsTant.DELETE_SQL, desc = "删除sql执行方法") + private Object executeDelete(SqlAndExecuteType sqlDefinition) { + + return null; + } + + +} diff --git a/src/test/java/youhong/ai/mymapper/proxy/entity/SqlAndExecuteType.java b/src/test/java/youhong/ai/mymapper/proxy/entity/SqlAndExecuteType.java new file mode 100644 index 0000000..f5d2c3e --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/proxy/entity/SqlAndExecuteType.java @@ -0,0 +1,40 @@ +package youhong.ai.mymapper.proxy.entity; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import weaver.conn.RecordSet; +import weaver.conn.RecordSetTrans; + +import java.lang.reflect.Method; +import java.util.List; + +/** + *

sql字符串和执行类型

+ * + *

create: 2023/3/15 16:48

+ * + * @author youHong.ai + */ +@Getter +@Setter +@ToString +public class SqlAndExecuteType { + + private String sql; + + + private String type; + + private List args; + + private String executeType; + + private Method method; + + private RecordSet recordSet; + + private RecordSetTrans recordSetTrans; + + private boolean trans; +} diff --git a/src/test/java/youhong/ai/mymapper/proxy/entity/SqlDbTypeMapping.java b/src/test/java/youhong/ai/mymapper/proxy/entity/SqlDbTypeMapping.java new file mode 100644 index 0000000..d12db92 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/proxy/entity/SqlDbTypeMapping.java @@ -0,0 +1,31 @@ +package youhong.ai.mymapper.proxy.entity; + +import lombok.*; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.function.Function; + +/** + *

sql类型映射

+ * + *

create: 2023/3/15 15:33

+ * + * @author youHong.ai + */ +@Getter +@Setter +@ToString +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class SqlDbTypeMapping { + + + private Function getDefaultSql; + private Function isCustomerDefaultSql; + private Function isCustomer; + private Function getSql; + + private Map mappingMap; +} diff --git a/src/test/java/youhong/ai/mymapper/util/Builder.java b/src/test/java/youhong/ai/mymapper/util/Builder.java new file mode 100644 index 0000000..e1daa96 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/util/Builder.java @@ -0,0 +1,93 @@ +package youhong.ai.mymapper.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + *

构造者模式

+ * + *

create: 2022-11-26 16:46

+ * + * @author youHong.ai + */ + +public class Builder { + + private Supplier constructor; + + private T target; + + private final List> propertiesInjects = new ArrayList<>(); + + private Builder(Supplier constructor) { + this.constructor = constructor; + } + + private Builder(T instance) { + this.target = instance; + } + + + /** + *

获取建造者对象

+ * + * @param constructor 传入需要创造的对象的构造方法 + * @param 需要创造的对象的范型 + * @return 建造者对象 + */ + public static Builder builder(Supplier constructor) { + return new Builder(constructor); + } + + public static Builder startSet(T instance) { + return new Builder(instance); + } + + /** + *

设置值方法

+ * + * @param setFun 对象字段的set方法 + * @param value 需要设置的值 + * @param 值的范型 + * @return 当前建造者对象本省 + */ + public Builder with(PropertiesInject setFun, V value) { + Consumer c = instance -> setFun.accept(instance, value); + propertiesInjects.add(c); + return this; + } + + + /** + *

创建出目标对象

+ * + * @return 目标对象 + */ + public T build() { + T instance; + if (this.target == null) { + instance = this.constructor.get(); + } else { + instance = target; + } + this.propertiesInjects.forEach(inject -> inject.accept(instance)); + return instance; + } + + public void endSet() { + T instance; + if (this.target == null) { + instance = this.constructor.get(); + } else { + instance = target; + } + this.propertiesInjects.forEach(inject -> inject.accept(instance)); + } + + @FunctionalInterface + public interface PropertiesInject { + void accept(T setFun, V value); + } +} diff --git a/src/test/java/youhong/ai/mymapper/util/ResultMapperUtil.java b/src/test/java/youhong/ai/mymapper/util/ResultMapperUtil.java new file mode 100644 index 0000000..6f27b03 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/util/ResultMapperUtil.java @@ -0,0 +1,639 @@ +package youhong.ai.mymapper.util; + +import aiyh.utils.Util; +import aiyh.utils.annotation.recordset.*; +import aiyh.utils.excention.CustomerException; +import aiyh.utils.tool.cn.hutool.core.util.ArrayUtil; +import aiyh.utils.tool.cn.hutool.core.util.StrUtil; +import org.jetbrains.annotations.Nullable; +import weaver.conn.RecordSet; +import weaver.conn.constant.DBConstant; +import youhong.ai.mymapper.proxy.MyMapperProxy; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.*; +import java.util.*; + +/** + *

结果集映射

+ * + *

create: 2023/3/15 18:15

+ * + * @author youHong.ai + */ +public class ResultMapperUtil { + public Object parseResult(Method method, RecordSet recordSet) { + Class returnType = method.getReturnType(); + if (void.class.equals(returnType)) { + return null; + } + if (returnType.isArray()) { + return getArray(recordSet, method); + } + if (returnType.isPrimitive()) { + if (recordSet.next()) { + return getPrimitive(recordSet, method); + } + return null; + } + if (Map.class.isAssignableFrom(returnType)) { + return getMap(recordSet, method, method.getGenericReturnType()); + } + if (List.class.isAssignableFrom(returnType)) { + return getList(recordSet, method); + } + if (recordSet.next()) { + return getBean(recordSet, method); + } + return null; + } + + private Object getPrimitive(RecordSet recordSet, Method method) { + Object value = null; + if (method.isAnnotationPresent(ResultMappings.class)) { + ResultMappings annotation = method.getAnnotation(ResultMappings.class); + ResultMap[] resultMaps = annotation.value(); + if (ArrayUtil.isEmpty(resultMaps)) { + throw new CustomerException("ResultMappings must have a value of ResultMap!"); + } + if (resultMaps.length > 1) { + throw new CustomerException("Multiple ResultMaps exist. A single method has only one return value"); + } + ResultMap resultMap = resultMaps[0]; + String column = resultMap.column(); + value = recordSet.getString(column); + if (Objects.isNull(value)) { + value = recordSet.getString(column.toUpperCase()); + } + } + if (value == null) { + value = recordSet.getString(1); + } + return primitiveParseValue(method.getReturnType(), value); + } + + private Object getArray(RecordSet recordSet, Method method) { + Class returnType = method.getReturnType(); + Class componentType = returnType.getComponentType(); + if (componentType.isArray()) { + throw new CustomerException("can not definition conversion rule of " + returnType); + } + List result = new ArrayList<>(); + if (componentType.isPrimitive()) { + while (recordSet.next()) { + result.add(getPrimitive(recordSet, method)); + } + } else if (Map.class.isAssignableFrom(componentType)) { + while (recordSet.next()) { + result.add(getMap(recordSet, method, method.getGenericReturnType())); + } + } else { + while (recordSet.next()) { + result.add(getBean(recordSet, method)); + } + } + if (result.size() > 0) { + Object array = Array.newInstance(componentType, result.size()); + for (int i = 0; i < result.size(); i++) { + Array.set(array, i, result.get(i)); + } + return array; + } + return null; + } + + private Object getMap(RecordSet recordSet, Method method, Type genericReturnType) { + Class keyClass = null; + Class valueClass = null; + if (genericReturnType instanceof ParameterizedType) { + ParameterizedType genericType = (ParameterizedType) genericReturnType; + Type[] types = genericType.getActualTypeArguments(); + for (int i = 0; i < types.length; i++) { + if (i == 0) { + keyClass = (Class) types[i]; + } + if (i == 2) { + valueClass = (Class) types[i]; + } + } + } + ResultMap[] resultMaps = null; + if (method.isAnnotationPresent(ResultMappings.class)) { + ResultMappings annotation = method.getAnnotation(ResultMappings.class); + resultMaps = annotation.value(); + if (ArrayUtil.isEmpty(resultMaps)) { + throw new CustomerException("ResultMappings must have a value of ResultMap!"); + } + } + Map result = new HashMap<>(); + if (keyClass == null && valueClass == null) { + if (resultMaps == null) { + getMapForRs(recordSet, result, method); + } else { + getMapForResultMaps(recordSet, resultMaps, result, method); + } + return result; + } + if (!String.class.equals(keyClass)) { + throw new CustomerException("result map key must be String! actually be " + keyClass); + } + if (resultMaps == null) { + getMapForRs(recordSet, result, method); + } else { + getMapForResultMaps(recordSet, resultMaps, result, method); + } + for (Map.Entry entry : result.entrySet()) { + entry.setValue(primitiveParseValue(valueClass, entry.getValue())); + } + return result; + } + + + private void getMapForResultMaps(RecordSet recordSet, ResultMap[] resultMaps, + Map result, Method method) { + for (ResultMap resultMap : resultMaps) { + String property = resultMap.property(); + Class aClass = resultMap.javaType(); + String column = resultMap.column(); + String value = recordSet.getString(column); + if (Objects.isNull(value)) { + value = recordSet.getString(column.toUpperCase()); + } + AssociationAndCollectionResult association = getAssociation(method, column, recordSet); + if (association.isParse()) { + Object parseValue = association.getValue(); + result.put(property, parseValue); + continue; + } + AssociationAndCollectionResult collection = getCollection(method, column, recordSet); + if (collection.isParse()) { + Object parseValue = collection.getValue(); + result.put(property, parseValue); + continue; + } + result.put(property, primitiveParseValue(aClass, value)); + } + } + + private void getMapForRs(RecordSet recordSet, Map result, Method method) { + String[] columnName = recordSet.getColumnName(); + if (columnName == null || columnName.length == 0) { + throw new CustomerException("can not read columnName array for recordSet"); + } + for (String column : columnName) { + String value = recordSet.getString(column); + if (Objects.isNull(value)) { + value = recordSet.getString(column.toUpperCase()); + } + AssociationAndCollectionResult association = getAssociation(method, column, recordSet); + if (association.isParse()) { + Object parseValue = association.getValue(); + result.put(column, parseValue); + result.put(column.toUpperCase(), parseValue); + result.put(column.toLowerCase(), parseValue); + result.put(Util.toCamelCase(column), parseValue); + continue; + } + AssociationAndCollectionResult collection = getCollection(method, column, recordSet); + if (collection.isParse()) { + Object parseValue = collection.getValue(); + result.put(column, parseValue); + result.put(column.toUpperCase(), parseValue); + result.put(column.toLowerCase(), parseValue); + result.put(Util.toCamelCase(column), parseValue); + continue; + } + result.put(column, value); + result.put(column.toUpperCase(), value); + result.put(column.toLowerCase(), value); + result.put(Util.toCamelCase(column), value); + } + } + + + private Object getList(RecordSet recordSet, Method method) { + List result = new ArrayList<>(); + Type genericReturnType = method.getGenericReturnType(); + if (genericReturnType instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericReturnType; + Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); + if (actualTypeArguments.length >= 1) { + Type actualTypeArgument = actualTypeArguments[0]; + if (actualTypeArgument instanceof ParameterizedType) { + Type rawType = ((ParameterizedType) actualTypeArgument).getRawType(); + Class aClass = null; + try { + aClass = Class.forName(rawType.getTypeName()); + } catch (ClassNotFoundException e) { + throw new CustomerException("can not get generic type!", e); + } + if (aClass.isArray() || List.class.isAssignableFrom(aClass)) { + throw new CustomerException("Unsupported type!List<{}>", aClass.getName()); + } + if (aClass.isPrimitive()) { + // 普通类型 + while (recordSet.next()) { + Object primitive = getPrimitive(recordSet, method); + result.add(primitive); + } + return result; + } + if (Map.class.isAssignableFrom(aClass)) { + // map处理 + while (recordSet.next()) { + Object map = getMap(recordSet, method, rawType); + result.add(map); + } + return result; + } + // java bean 类型 + while (recordSet.next()) { + Object bean = getBean(recordSet, method); + result.add(bean); + } + return result; + } + } else { + throw new CustomerException("can not get generic type!"); + } + } + throw new CustomerException("Return value of position type, unable to obtain method return generic type."); + } + + private Object getBean(RecordSet recordSet, Method method) { + Class returnType = method.getReturnType(); + BeanInfo beanInfo = null; + try { + beanInfo = Introspector.getBeanInfo(returnType, Object.class); + } catch (IntrospectionException e) { + throw new CustomerException(e); + } + Constructor constructor = null; + try { + constructor = returnType.getConstructor(); + } catch (NoSuchMethodException e) { + throw new CustomerException("can not get no argument constructor on " + returnType); + } + Object obj = null; + try { + obj = constructor.newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new CustomerException("can not initialization bean of " + returnType); + } + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor item : propertyDescriptors) { + String name = item.getName(); + if (StrUtil.isBlank(name)) { + name = item.getDisplayName(); + } + Method writeMethod = item.getWriteMethod(); + Class propertyType = item.getPropertyType(); + AssociationAndCollectionResult association = getAssociation(method, name, recordSet); + if (association.isParse()) { + Object value = association.getValue(); + if (value != null) { + try { + writeMethod.invoke(obj, value); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new CustomerException("invoke writeMethod fail ! {}.{}, target arg type:{}, value arg type: {}", + returnType.getName(), writeMethod.getName(), propertyType.getName(), value.getClass().getName()); + } + } + continue; + } + AssociationAndCollectionResult collection = getCollection(method, name, recordSet); + if (collection.isParse()) { + Object value = collection.getValue(); + try { + writeMethod.invoke(obj, value); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new CustomerException("invoke writeMethod fail ! {}.{}, target arg type:{}, value arg type: {}", + returnType.getName(), writeMethod.getName(), propertyType.getName(), value.getClass().getName()); + } + continue; + } + Field declaredField = null; + try { + declaredField = returnType.getDeclaredField(name); + } catch (NoSuchFieldException e) { + throw new CustomerException("no such field {} in {}", name, returnType.getName()); + } + String columnName = name; + Object value; + if (declaredField.isAnnotationPresent(SqlDbFieldAnn.class)) { + columnName = declaredField.getAnnotation(SqlDbFieldAnn.class).value(); + } + boolean caseConversion = true; + + if (DBConstant.DB_TYPE_ORACLE.equals(recordSet.getDBType())) { + caseConversion = false; + if (declaredField.isAnnotationPresent(SqlOracleDbFieldAnn.class)) { + columnName = declaredField.getAnnotation(SqlOracleDbFieldAnn.class).value(); + } + value = recordSet.getString(columnName); + if (value != null) { + Object parseValue = primitiveParseValue(propertyType, value); + try { + writeMethod.invoke(obj, parseValue); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new CustomerException("invoke writeMethod fail ! {}.{}, target arg type:{}, value arg type: {}", + returnType.getName(), writeMethod.getName(), propertyType.getName(), + parseValue == null ? "" : + parseValue.getClass().getName()); + } + continue; + } + } + if (method.isAnnotationPresent(CaseConversion.class)) { + caseConversion = method.getAnnotation(CaseConversion.class).value(); + } + if (caseConversion) { + value = recordSet.getString(Util.toUnderlineCase(columnName)); + Object parseValue = primitiveParseValue(propertyType, value); + try { + writeMethod.invoke(obj, parseValue); + continue; + } catch (IllegalAccessException | InvocationTargetException e) { + throw new CustomerException("invoke writeMethod fail ! {}.{}, target arg type:{}, value arg type: {}", + returnType.getName(), writeMethod.getName(), propertyType.getName(), + parseValue == null ? "" : + parseValue.getClass().getName()); + } + } + value = recordSet.getString(columnName); + if (value != null) { + Object parseValue = primitiveParseValue(propertyType, value); + try { + writeMethod.invoke(obj, parseValue); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new CustomerException("invoke writeMethod fail ! {}.{}, target arg type:{}, value arg type: {}", + returnType.getName(), writeMethod.getName(), propertyType.getName(), + parseValue == null ? "" : parseValue.getClass().getName()); + } + } + + } + return obj; + } + + + private AssociationAndCollectionResult getAssociation(Method method, String column, RecordSet recordSet) { + AssociationAndCollectionResult result = new AssociationAndCollectionResult(); + if (method.isAnnotationPresent(Associations.class)) { + Associations associations = method.getAnnotation(Associations.class); + Association[] associationArray = associations.value(); + for (Association item : associationArray) { + if (column.equalsIgnoreCase(item.property())) { + getAssociationAndCollectionResult(method, item.column(), recordSet, result, item, null); + } + } + } + return result; + } + + private AssociationAndCollectionResult getCollection(Method method, String column, + RecordSet recordSet) { + AssociationAndCollectionResult result = new AssociationAndCollectionResult(); + if (method.isAnnotationPresent(CollectionMappings.class)) { + CollectionMappings collectionMappings = method.getAnnotation(CollectionMappings.class); + CollectionMapping[] mappingArray = collectionMappings.value(); + for (CollectionMapping item : mappingArray) { + if (column.equalsIgnoreCase(item.property())) { + getAssociationAndCollectionResult(method, item.column(), recordSet, result, null, item); + } + } + } + return result; + } + + @Nullable + private void getAssociationAndCollectionResult(Method method, String column, + RecordSet recordSet, + AssociationAndCollectionResult result, + Association association, + CollectionMapping collectionMapping) { + String select; + Id id; + String property; + if (association != null) { + select = association.select(); + id = association.id(); + property = association.property(); + } else { + select = collectionMapping.select(); + id = collectionMapping.id(); + property = collectionMapping.property(); + } + String value = recordSet.getString(column); + MyMapperProxy myMapperProxy = new MyMapperProxy(true); + if (StrUtil.isBlank(value)) { + value = recordSet.getString(column.toUpperCase()); + } + Object parseValue = primitiveParseValue(id.value(), value); + if (id.methodId() == -1) { + // 自定义查询方法路径 + customerSqlPathParseResult(method, result, collectionMapping, select, id, property, myMapperProxy, parseValue); + } else { + currentClassMethodParseResult(method, result, collectionMapping, id, property, myMapperProxy, parseValue); + if (!result.isParse()) { + if (collectionMapping == null) { + throw new CustomerException(Util.logStr("can not find AssociationMethod annotation in in class [{}] value of [{}]", method.getDeclaringClass(), id.methodId())); + } else { + throw new CustomerException(Util.logStr("can not find CollectionMethod annotation in in class [{}] value of [{}]", method.getDeclaringClass(), id.methodId())); + } + } + } + } + + private static void currentClassMethodParseResult(Method method, + AssociationAndCollectionResult result, + CollectionMapping collectionMapping, + Id id, String property, + MyMapperProxy myMapperProxy, + Object parseValue) { + Method[] methods = method.getDeclaringClass().getMethods(); + for (Method innerMethod : methods) { + if (innerMethod.isAnnotationPresent(AssociationMethod.class) || innerMethod.isAnnotationPresent(CollectionMethod.class)) { + int methodId = -1; + if (collectionMapping == null) { + AssociationMethod methodAnnotation = innerMethod.getAnnotation(AssociationMethod.class); + methodId = methodAnnotation.value(); + } else { + CollectionMethod methodAnnotation = innerMethod.getAnnotation(CollectionMethod.class); + methodId = methodAnnotation.value(); + } + if (methodId != id.methodId()) { + continue; + } + Class returnType = innerMethod.getReturnType(); + if (collectionMapping == null) { + if (List.class.isAssignableFrom(returnType)) { + throw new CustomerException("can not set result, Associations annotation not support result of list! Do you want to use @CollectionMappings!"); + } + } else { + if (!List.class.isAssignableFrom(returnType)) { + throw new CustomerException("can not set result, CollectionMappings annotation only support result of list! Do you want to use @Associations!"); + } + } + Object o = myMapperProxy.invokeRs(null, innerMethod, new Object[]{parseValue}, method.getName()); + result.setParse(true); + result.setValue(o); + result.setProperty(property); + } + } + } + + private static void customerSqlPathParseResult(Method method, + AssociationAndCollectionResult result, + CollectionMapping collectionMapping, + String select, + Id id, String property, + MyMapperProxy myMapperProxy, + Object parseValue) { + int i = select.lastIndexOf("."); + String selectClass = select.substring(0, i); + Class aClass = null; + try { + aClass = Class.forName(selectClass); + } catch (ClassNotFoundException e) { + throw new CustomerException("can not find class of " + selectClass); + } + Method associationMethod = null; + try { + associationMethod = aClass.getMethod(select.substring(i + 1), id.value()); + } catch (NoSuchMethodException e) { + throw new CustomerException("can not find method of " + select); + } + Class returnType = associationMethod.getReturnType(); + if (collectionMapping == null) { + if (List.class.isAssignableFrom(returnType)) { + throw new CustomerException("can not set result, Associations annotation not support result of list! Do you want to use @CollectionMappings!"); + } + } else { + if (!List.class.isAssignableFrom(returnType)) { + throw new CustomerException("can not set result, CollectionMappings annotation only support result of list! Do you want to use @Associations!"); + } + } + Object o = myMapperProxy.invokeRs(null, associationMethod, new Object[]{parseValue}, method.getName()); + result.setParse(true); + result.setValue(o); + result.setProperty(property); + } + + + private Object primitiveParseValue(Class aclass, Object obj) { + if (Number.class.isAssignableFrom(aclass)) { + return parseNumber(aclass, obj); + } + if (CharSequence.class.isAssignableFrom(aclass)) { + return parseCharSequence(aclass, obj); + } + if (Character.class.isAssignableFrom(aclass) || char.class.isAssignableFrom(aclass)) { + return obj == null ? null : String.valueOf(obj).charAt(0); + } + throw new CustomerException("can not definition conversion rule of " + aclass); + } + + private CharSequence parseCharSequence(Class aclass, Object obj) { + if (String.class.isAssignableFrom(aclass)) { + return obj == null ? null : String.valueOf(obj); + } + throw new CustomerException("can not definition conversion rule of " + aclass); + } + + /** + *

数字类型转换

+ * + * @param aclass 类型 + * @param obj 待转换值 + * @return 转换结果 + */ + public static Number parseNumber(Class aclass, Object obj) { + String value = Util.null2String(obj); + if ("".equals(value)) { + return null; + } + if (int.class.isAssignableFrom(aclass) || Integer.class.isAssignableFrom(aclass)) { + return Integer.parseInt(value); + } + if (double.class.isAssignableFrom(aclass) || Double.class.isAssignableFrom(aclass)) { + return Double.parseDouble(value); + } + if (float.class.isAssignableFrom(aclass) || Float.class.isAssignableFrom(aclass)) { + return Float.parseFloat(value); + } + if (long.class.isAssignableFrom(aclass) || Long.class.isAssignableFrom(aclass)) { + return Long.parseLong(value); + } + if (byte.class.isAssignableFrom(aclass) || Byte.class.isAssignableFrom(aclass)) { + return Byte.parseByte(value); + } + if (short.class.isAssignableFrom(aclass) || Short.class.isAssignableFrom(aclass)) { + return Short.parseShort(value); + } + throw new CustomerException("can not definition conversion rule of " + aclass); + } + + + /** + *

获取范型类型

+ * + * @param type 类型 + * @return 范型 + */ + public Class getRawType(Type type) { + if (type instanceof Class) { + return (Class) type; + } else if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + Type rawType = parameterizedType.getRawType(); + return (Class) rawType; + } else if (type instanceof GenericArrayType) { + Type componentType = ((GenericArrayType) type).getGenericComponentType(); + return Array.newInstance(getRawType(componentType), 0).getClass(); + } else if (type instanceof TypeVariable) { + return Object.class; + } else if (type instanceof WildcardType) { + return getRawType(((WildcardType) type).getUpperBounds()[0]); + } else { + String className = type == null ? "null" : type.getClass().getName(); + throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + type + "> is of type " + className); + } + } + + static class AssociationAndCollectionResult { + private boolean isParse; + + private Object value; + + private String property; + + public String getProperty() { + return property; + } + + public void setProperty(String property) { + this.property = property; + } + + public boolean isParse() { + return isParse; + } + + public void setParse(boolean parse) { + isParse = parse; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + } +} diff --git a/src/test/java/youhong/ai/taibao/TestTaiBao.java b/src/test/java/youhong/ai/taibao/TestTaiBao.java index 4070bf1..0437d5c 100644 --- a/src/test/java/youhong/ai/taibao/TestTaiBao.java +++ b/src/test/java/youhong/ai/taibao/TestTaiBao.java @@ -1,5 +1,6 @@ package youhong.ai.taibao; +import aiyh.utils.Util; import basetest.BaseTest; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; @@ -9,16 +10,18 @@ import com.cloudstore.dev.api.util.Util_DataCache; import org.junit.Test; import weaver.conn.RecordSet; import weaver.general.BaseBean; -import weaver.general.Util; +import weaver.general.GCONST; import weaver.hrm.User; import weaver.workflow.request.todo.OfsSettingObject; import weaver.workflow.request.todo.RequestUtil; import weaver.workflow.webservices.WorkflowBaseInfo; import weaver.workflow.webservices.WorkflowRequestInfo; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** *

测试

@@ -32,6 +35,21 @@ public class TestTaiBao extends BaseTest { @Test public void test() { + String logPath = GCONST.getLogPath(); + try { + Map map = Util.getProperties2Map("logPathConfig", "cus"); + if (map != null) { + if (map.containsKey("logPath")) { + logPath = "".equals(Util.null2String(map.get("logPath"))) ? logPath : Util.null2String(map.get("logPath")); + } + } + } catch (Exception ignore) { + + } + if (!logPath.endsWith(File.separator)) { + logPath = logPath + File.separator; + } + System.out.println(logPath); FunctionListService functionListService = new FunctionListService(); System.out.println(JSON.toJSONString(functionListService.getFunctionList(new User(90)))); } diff --git a/src/test/java/youhong/ai/utiltest/GenericTest.java b/src/test/java/youhong/ai/utiltest/GenericTest.java new file mode 100644 index 0000000..35b9269 --- /dev/null +++ b/src/test/java/youhong/ai/utiltest/GenericTest.java @@ -0,0 +1,58 @@ +package youhong.ai.utiltest; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + + +public class GenericTest { + + private final List> map = new ArrayList<>(); + + public static void main(String[] args) throws SecurityException, NoSuchFieldException { + // 获取Class实例 + Class class1 = GenericTest.class; + // 根据属性名取得该属性对应的Field对象 + Field mapField = class1.getDeclaredField("map"); + // 示范第一个方法:直接通过getType()取出Field的类型,只对普通类型的Field有效 + Class class2 = mapField.getType(); + // 输出查看 + System.out.println("属性名为map的属性类型为:" + class2); + + // 示范第二种方法: + Type mapMainType = mapField.getGenericType(); + // 为了确保安全转换,使用instanceof + if (mapMainType instanceof ParameterizedType) { + // 执行强制类型转换 + ParameterizedType parameterizedType = (ParameterizedType) mapMainType; + // 获取基本类型信息,即Map + Type basicType = parameterizedType.getRawType(); + System.out.println("基本类型为:" + basicType); + // 获取泛型类型的泛型参数 + Type[] types = parameterizedType.getActualTypeArguments(); + for (int i = 0; i < types.length; i++) { + Type type = types[i]; + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedTypeInner = (ParameterizedType) type; + System.out.println(parameterizedTypeInner.getRawType()); + Type[] actualTypeArguments = parameterizedTypeInner.getActualTypeArguments(); + for (Type actualTypeArgument : actualTypeArguments) { + Class clazz = (Class) actualTypeArgument; + System.out.println(clazz.getSimpleName()); + System.out.println("泛型类型是:" + actualTypeArgument.getTypeName()); + } + continue; + } + Class clazz = (Class) type; + System.out.println(clazz.getSimpleName()); + System.out.println("第" + (i + 1) + "个泛型类型是:" + types[i].getTypeName()); + } + } else { + System.out.println("获取泛型类型出错!"); + } + } + +} \ No newline at end of file