DOResultTransformer.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.primeton.dams.utils.template;
  2. import lombok.Getter;
  3. import lombok.NonNull;
  4. import lombok.Setter;
  5. import org.apache.commons.beanutils.BeanUtils;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.hibernate.HibernateException;
  8. import org.hibernate.transform.BasicTransformerAdapter;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import javax.persistence.Column;
  12. import java.lang.reflect.Field;
  13. import java.lang.reflect.Modifier;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18. /**
  19. * <pre>
  20. *
  21. * Created by zhaopx.
  22. * User: zhaopx
  23. * Date: 2022/3/1
  24. * Time: 下午6:35
  25. * Vendor: primeton.com
  26. *
  27. * </pre>
  28. *
  29. * @author zhaopx
  30. */
  31. public class DOResultTransformer extends BasicTransformerAdapter {
  32. /**
  33. *
  34. */
  35. private static final long serialVersionUID = 1L;
  36. private static Logger logger = LoggerFactory.getLogger(DOResultTransformer.class);
  37. /**
  38. * 驼峰转下划线
  39. */
  40. private static Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");
  41. @Getter
  42. @Setter
  43. class FieldInfo {
  44. final String fieldName;
  45. final Class<?> fieldType;
  46. public FieldInfo(String fieldName, Class<?> fieldType) {
  47. this.fieldName = fieldName;
  48. this.fieldType = fieldType;
  49. }
  50. }
  51. private final Class resultClass;
  52. private boolean isInitialized;
  53. private final Map<String, FieldInfo> aliasFieldMap = new HashMap<>();
  54. public <T> DOResultTransformer(@NonNull Class<T> resultClass) {
  55. this.resultClass = resultClass;
  56. isInitialized = false;
  57. }
  58. @Override
  59. public Object transformTuple(Object tuple[], String aliases[]) {
  60. Object result = null;
  61. if (!isInitialized) {
  62. initialize(aliases);
  63. }
  64. // 从构造方法进入
  65. try {
  66. result = resultClass.newInstance();
  67. for (int i = 0; i < tuple.length; i++) {
  68. final FieldInfo fieldInfo = aliasFieldMap.get(StringUtils.upperCase(aliases[i]));
  69. if(fieldInfo == null) {
  70. continue;
  71. }
  72. BeanUtils.setProperty(result, fieldInfo.getFieldName(), tuple[i]);
  73. }
  74. } catch (Exception e) {
  75. throw new HibernateException((new StringBuilder()).append("Could not instantiate resultclass: ").append(resultClass.getName()).toString());
  76. }
  77. return result;
  78. }
  79. private void initialize(String aliases[]) {
  80. final Field[] declaredFields = resultClass.getDeclaredFields();
  81. for (Field declaredField : declaredFields) {
  82. // final 和 static 修饰的都不是 bean 字段
  83. if(Modifier.isFinal(declaredField.getModifiers()) || Modifier.isStatic(declaredField.getModifiers())) {
  84. continue;
  85. }
  86. // 字段名称和类型
  87. final String fieldName = declaredField.getName();
  88. final Class<?> fieldType = declaredField.getType();
  89. // 获取字段上的注解
  90. final Column columnAnno = declaredField.getAnnotation(Column.class);
  91. if(columnAnno == null || StringUtils.isBlank(columnAnno.name())) {
  92. // 无注解
  93. aliasFieldMap.put(StringUtils.upperCase(humpToLine(fieldName)), new FieldInfo(fieldName, fieldType));
  94. } else {
  95. // 有注解, 并且 Column name 不为 null
  96. aliasFieldMap.put(StringUtils.upperCase(columnAnno.name()), new FieldInfo(fieldName, fieldType));
  97. }
  98. }
  99. isInitialized = true;
  100. }
  101. /**
  102. * 驼峰转下划线
  103. */
  104. public static String humpToLine(String str) {
  105. Matcher matcher = HUMP_PATTERN.matcher(str);
  106. StringBuffer sb = new StringBuffer();
  107. while (matcher.find()) {
  108. matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
  109. }
  110. matcher.appendTail(sb);
  111. return sb.toString();
  112. }
  113. public boolean equals(Object o) {
  114. if (this == o)
  115. return true;
  116. if (o == null || getClass() != o.getClass())
  117. return false;
  118. DOResultTransformer that = (DOResultTransformer) o;
  119. if (!resultClass.equals(that.resultClass))
  120. return false;
  121. return true;
  122. }
  123. public int hashCode() {
  124. int result = resultClass.hashCode();
  125. result = 31 * result;
  126. return result;
  127. }
  128. }