From 9b5cc35281c967a00aa367cd27849d6c35c26b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB?= <1173628408@qq.com> Date: Thu, 29 Aug 2024 22:12:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/muyu/enums/DataType.java | 81 +++++++++++++++++++--- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/muyu/enums/DataType.java b/src/main/java/com/muyu/enums/DataType.java index 3571cfa..2d66436 100644 --- a/src/main/java/com/muyu/enums/DataType.java +++ b/src/main/java/com/muyu/enums/DataType.java @@ -1,6 +1,10 @@ package com.muyu.enums; +import com.muyu.common.core.utils.StringUtils; +import lombok.extern.log4j.Log4j2; + import java.math.BigDecimal; +import java.util.Arrays; import java.util.Date; /** @@ -9,37 +13,40 @@ import java.util.Date; * @Description 数据类型枚举 * @Version 1.0.0 */ +@Log4j2 public enum DataType { STRING( new String[]{"char", "varchar", "text", "mediumtext", "longtext", "longblob"}, - String.class + String.class, + "String" ), INTEGER( new String[]{"int", "tinyint"}, - Integer.class + Integer.class, + "Integer" ), DATE( new String[]{"date", "datetime","timestamp"}, - Date.class + Date.class, + "Date" ), BIG_DECIMAL( new String[]{"decimal", "double", "float"}, - BigDecimal.class + BigDecimal.class, + "BigDecimal" ), LONG( new String[]{"bigint"}, - Long.class + Long.class, + "Long" ); private String[] sourceType; private Class targetType; - DataType(String[] sourceType, Class targetType) { - this.sourceType = sourceType; - this.targetType = targetType; - } + private String javaType; public String[] getSourceType() { return sourceType; @@ -56,4 +63,60 @@ public enum DataType { public void setTargetType(Class 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; + } + + }