修改数据结构,并添加数据类型枚举
parent
f0f4fbfa85
commit
9b5cc35281
|
@ -1,6 +1,10 @@
|
||||||
package com.muyu.enums;
|
package com.muyu.enums;
|
||||||
|
|
||||||
|
import com.muyu.common.core.utils.StringUtils;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,37 +13,40 @@ import java.util.Date;
|
||||||
* @Description 数据类型枚举
|
* @Description 数据类型枚举
|
||||||
* @Version 1.0.0
|
* @Version 1.0.0
|
||||||
*/
|
*/
|
||||||
|
@Log4j2
|
||||||
public enum DataType {
|
public enum DataType {
|
||||||
|
|
||||||
STRING(
|
STRING(
|
||||||
new String[]{"char", "varchar", "text", "mediumtext", "longtext", "longblob"},
|
new String[]{"char", "varchar", "text", "mediumtext", "longtext", "longblob"},
|
||||||
String.class
|
String.class,
|
||||||
|
"String"
|
||||||
),
|
),
|
||||||
INTEGER(
|
INTEGER(
|
||||||
new String[]{"int", "tinyint"},
|
new String[]{"int", "tinyint"},
|
||||||
Integer.class
|
Integer.class,
|
||||||
|
"Integer"
|
||||||
),
|
),
|
||||||
DATE(
|
DATE(
|
||||||
new String[]{"date", "datetime","timestamp"},
|
new String[]{"date", "datetime","timestamp"},
|
||||||
Date.class
|
Date.class,
|
||||||
|
"Date"
|
||||||
),
|
),
|
||||||
BIG_DECIMAL(
|
BIG_DECIMAL(
|
||||||
new String[]{"decimal", "double", "float"},
|
new String[]{"decimal", "double", "float"},
|
||||||
BigDecimal.class
|
BigDecimal.class,
|
||||||
|
"BigDecimal"
|
||||||
),
|
),
|
||||||
LONG(
|
LONG(
|
||||||
new String[]{"bigint"},
|
new String[]{"bigint"},
|
||||||
Long.class
|
Long.class,
|
||||||
|
"Long"
|
||||||
);
|
);
|
||||||
|
|
||||||
private String[] sourceType;
|
private String[] sourceType;
|
||||||
|
|
||||||
private Class<?> targetType;
|
private Class<?> targetType;
|
||||||
|
|
||||||
DataType(String[] sourceType, Class<?> targetType) {
|
private String javaType;
|
||||||
this.sourceType = sourceType;
|
|
||||||
this.targetType = targetType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] getSourceType() {
|
public String[] getSourceType() {
|
||||||
return sourceType;
|
return sourceType;
|
||||||
|
@ -56,4 +63,60 @@ public enum DataType {
|
||||||
public void setTargetType(Class<?> targetType) {
|
public void setTargetType(Class<?> targetType) {
|
||||||
this.targetType = targetType;
|
this.targetType = targetType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getJavaType() {
|
||||||
|
return javaType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJavaType(String javaType) {
|
||||||
|
this.javaType = javaType;
|
||||||
|
}
|
||||||
|
|
||||||
|
DataType(String[] sourceType, Class<?> targetType, String javaType) {
|
||||||
|
this.sourceType = sourceType;
|
||||||
|
this.targetType = targetType;
|
||||||
|
this.javaType = javaType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取枚举的Class类型
|
||||||
|
* @param sqlType sql数据类型
|
||||||
|
* @return class类
|
||||||
|
*/
|
||||||
|
public static Class<?> getTargetClass(String sqlType) {
|
||||||
|
Class<?> targetClass = null;
|
||||||
|
for (DataType dataType : values()) {
|
||||||
|
if (sqlType != null && Arrays.asList(dataType.getSourceType()).contains(sqlType.toLowerCase())) {
|
||||||
|
targetClass = dataType.getTargetType();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (targetClass == null) {
|
||||||
|
log.info("SQL字段类型异常,sqlType ---> {}", sqlType);
|
||||||
|
throw new RuntimeException(String.format("SQL字段类型异常,sqlType ---> {}", sqlType));
|
||||||
|
}
|
||||||
|
return targetClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取枚举的java类型
|
||||||
|
* @param sqlType sql数据类型
|
||||||
|
* @return java类型
|
||||||
|
*/
|
||||||
|
public static String getJavaType(String sqlType) {
|
||||||
|
String javaType = null;
|
||||||
|
for (DataType dataType : values()) {
|
||||||
|
if (sqlType != null && Arrays.asList(dataType.getSourceType()).contains(sqlType.toLowerCase())) {
|
||||||
|
javaType = dataType.getJavaType();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(javaType)) {
|
||||||
|
log.info("SQL字段类型异常,sqlType ---> {}", sqlType);
|
||||||
|
throw new RuntimeException(String.format("SQL字段类型异常,sqlType ---> {}", sqlType));
|
||||||
|
}
|
||||||
|
return javaType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue