|
@@ -25,6 +25,7 @@ import java.sql.JDBCType;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.function.BiFunction;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
/**
|
|
|
* jpa 注解解析器
|
|
@@ -40,15 +41,46 @@ public class JpaAnnotationParser {
|
|
|
|
|
|
private static final List<BiFunction<Class, PropertyDescriptor, JDBCType>> jdbcTypeConvert = new ArrayList<>();
|
|
|
|
|
|
+ private static final List<Class> numberType = Arrays.asList(
|
|
|
+ byte.class, Byte.class
|
|
|
+ , short.class, Short.class
|
|
|
+ , int.class, Integer.class
|
|
|
+ , float.class, Float.class
|
|
|
+ , double.class, Double.class
|
|
|
+ , long.class, Long.class
|
|
|
+ , BigDecimal.class, BigInteger.class
|
|
|
+ );
|
|
|
+
|
|
|
+ private static final List<JDBCType> numberJdbcType = Arrays.asList(
|
|
|
+ JDBCType.TINYINT, JDBCType.DECIMAL, JDBCType.NUMERIC,
|
|
|
+ JDBCType.BIGINT, JDBCType.SMALLINT, JDBCType.INTEGER,
|
|
|
+ JDBCType.DECIMAL, JDBCType.BIT
|
|
|
+ );
|
|
|
+
|
|
|
static {
|
|
|
jdbcTypeMapping.put(String.class, JDBCType.VARCHAR);
|
|
|
|
|
|
+ jdbcTypeMapping.put(Byte.class, JDBCType.TINYINT);
|
|
|
+ jdbcTypeMapping.put(byte.class, JDBCType.TINYINT);
|
|
|
+
|
|
|
+ jdbcTypeMapping.put(Short.class, JDBCType.INTEGER);
|
|
|
+ jdbcTypeMapping.put(short.class, JDBCType.INTEGER);
|
|
|
+
|
|
|
jdbcTypeMapping.put(Integer.class, JDBCType.INTEGER);
|
|
|
jdbcTypeMapping.put(int.class, JDBCType.INTEGER);
|
|
|
+
|
|
|
+ jdbcTypeMapping.put(Character.class, JDBCType.CHAR);
|
|
|
+ jdbcTypeMapping.put(char.class, JDBCType.CHAR);
|
|
|
+
|
|
|
+ jdbcTypeMapping.put(Long.class, JDBCType.BIGINT);
|
|
|
+ jdbcTypeMapping.put(long.class, JDBCType.BIGINT);
|
|
|
+
|
|
|
jdbcTypeMapping.put(Double.class, JDBCType.DECIMAL);
|
|
|
jdbcTypeMapping.put(double.class, JDBCType.DECIMAL);
|
|
|
+
|
|
|
jdbcTypeMapping.put(Float.class, JDBCType.DECIMAL);
|
|
|
jdbcTypeMapping.put(float.class, JDBCType.DECIMAL);
|
|
|
+
|
|
|
jdbcTypeMapping.put(Boolean.class, JDBCType.BIT);
|
|
|
jdbcTypeMapping.put(boolean.class, JDBCType.BIT);
|
|
|
|
|
@@ -89,6 +121,12 @@ public class JpaAnnotationParser {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public static boolean isNumberType(RDBColumnMetaData columnMetaData) {
|
|
|
+ return numberType.contains(columnMetaData.getJavaType())
|
|
|
+ || Number.class.isAssignableFrom(columnMetaData.getJavaType())
|
|
|
+ || numberJdbcType.contains(columnMetaData.getJdbcType());
|
|
|
+ }
|
|
|
+
|
|
|
public static RDBTableMetaData parseMetaDataFromEntity(Class entityClass) {
|
|
|
Table table = AnnotationUtils.findAnnotation(entityClass, Table.class);
|
|
|
if (table == null) {
|
|
@@ -133,11 +171,10 @@ public class JpaAnnotationParser {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- if (columnMetaData.getJdbcType() == JDBCType.DATE) {
|
|
|
- columnMetaData.setValueConverter(dateConvert);
|
|
|
- } else if (columnMetaData.getJdbcType() == JDBCType.TIMESTAMP) {
|
|
|
+ if (columnMetaData.getJdbcType() == JDBCType.DATE
|
|
|
+ || columnMetaData.getJdbcType() == JDBCType.TIMESTAMP) {
|
|
|
columnMetaData.setValueConverter(dateConvert);
|
|
|
- } else if (columnMetaData.getJdbcType() == JDBCType.NUMERIC) {
|
|
|
+ } else if (isNumberType(columnMetaData)) {
|
|
|
columnMetaData.setValueConverter(new NumberValueConverter(columnMetaData.getJavaType()));
|
|
|
}
|
|
|
|
|
@@ -147,6 +184,7 @@ public class JpaAnnotationParser {
|
|
|
return tableMetaData;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
private static <T extends Annotation> T getAnnotation(Class entityClass, PropertyDescriptor descriptor, Class<T> type) {
|
|
|
T ann = null;
|
|
|
try {
|