修改数据结构,并添加数据类型枚举

master
王鑫 2024-08-29 22:12:02 +08:00
parent f0f4fbfa85
commit 9b5cc35281
1 changed files with 72 additions and 9 deletions

View File

@ -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;
}
} }