Commit 36459a40 by flying-cattle

完善mysql数据类型转换工具

parent 9b83c3c2
/**
* Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
*
* @Package: com.github.mybatis.fl.convert
* @author: flying-cattle
* @date: 2019年4月9日 下午8:15:25
*/
package com.github.mybatis.fl.convert;
/**
* Copyright: Copyright (c) 2019
*
* @ClassName: DateType.java
* @Description: 数据库时间类型 到 实体类时间类型 对应策略
*
* @version: v1.0.0
* @author: flying-cattle
* @date: 2019年4月9日 下午8:13:13
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* 2019年4月9日 flying-cattle v2.0.1 initialize
*/
public enum DateType {
/**
* @Description: 只使用 java.util.date 代替
*/
ONLY_DATE,
/**
* @Description: 使用 java.sql 包下的
*/
SQL_PACK,
/**
* @Description: 使用 java.time 包下的
* <p>java8 新的时间类型</p>
*/
TIME_PACK
}
/**
* Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
*
* @Package: com.github.mybatis.fl.convert
* @author: flying-cattle
* @date: 2019年4月9日 下午8:15:25
*/
package com.github.mybatis.fl.convert;
/**
* Copyright: Copyright (c) 2019
*
* @ClassName: DbColumnType.java
* @Description: 表字段类型
*
* @version: v1.0.0
* @author: flying-cattle
* @date: 2019年4月9日 下午8:15:25
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* 2019年4月9日 flying-cattle v1.0.0 initialize
*/
public enum DbColumnType implements IColumnType {
// 基本类型
BASE_BYTE("byte", null),
BASE_SHORT("short", null),
BASE_CHAR("char", null),
BASE_INT("int", null),
BASE_LONG("long", null),
BASE_FLOAT("float", null),
BASE_DOUBLE("double", null),
BASE_BOOLEAN("boolean", null),
// 包装类型
BYTE("Byte", null),
SHORT("Short", null),
CHARACTER("Character", null),
INTEGER("Integer", null),
LONG("Long", null),
FLOAT("Float", null),
DOUBLE("Double", null),
BOOLEAN("Boolean", null),
STRING("String", null),
// sql 包下数据类型
DATE_SQL("Date", "java.sql.Date"),
TIME("Time", "java.sql.Time"),
TIMESTAMP("Timestamp", "java.sql.Timestamp"),
BLOB("Blob", "java.sql.Blob"),
CLOB("Clob", "java.sql.Clob"),
// java8 新时间类型
LOCAL_DATE("LocalDate", "java.time.LocalDate"),
LOCAL_TIME("LocalTime", "java.time.LocalTime"),
YEAR("Year", "java.time.Year"),
YEAR_MONTH("YearMonth", "java.time.YearMonth"),
LOCAL_DATE_TIME("LocalDateTime", "java.time.LocalDateTime"),
// 其他杂类
BYTE_ARRAY("byte[]", null),
OBJECT("Object", null),
DATE("Date", "java.util.Date"),
BIG_INTEGER("BigInteger", "java.math.BigInteger"),
BIG_DECIMAL("BigDecimal", "java.math.BigDecimal");
/**
* 类型
*/
private final String type;
/**
* 包路径
*/
private final String pkg;
DbColumnType(final String type, final String pkg) {
this.type = type;
this.pkg = pkg;
}
@Override
public String getType() {
return type;
}
@Override
public String getPkg() {
return pkg;
}
}
/**
* Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* @Package: com.github.mybatis.fl.convert
* @author: flying-cattle
* @date: 2019年4月9日 下午8:09:35
*/
package com.github.mybatis.fl.convert;
/**
* Copyright: Copyright (c) 2019
*
* @ClassName: IColumnType.java
* @Description: 获取实体类字段属性类信息接口
*
* @version: v1.0.0
* @author: flying-cattle
* @date: 2019年4月9日 下午8:09:35
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* 2019年4月9日 flying-cattle v2.0.1 initialize
*/
public interface IColumnType {
/**
* @Description: 获取字段类型
*
* @return 字段类型
*/
String getType();
/**
* @Description: 获取字段类型完整名
*
* @return 字段类型完整名
*/
String getPkg();
}
/**
* Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* @Package: com.github.mybatis.fl.convert
* @author: flying-cattle
* @date: 2019年4月9日 下午8:06:16
*/
package com.github.mybatis.fl.convert;
/**
* Copyright: Copyright (c) 2019
*
* @ClassName: ITypeConvert.java
* @Description: 该类的功能描述
*
* @version: v1.0.0
* @author: flying-cattle
* @date: 2019年4月9日 下午8:06:16
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* 2019年4月9日 flying-cattle v2.0.1 initialize
*/
public interface ITypeConvert {
/**
* @Description: 执行类型转换
*
* @param globalConfig 全局配置
* @param fieldType 字段类型
* @return ignore
*/
IColumnType processTypeConvert(DateType dateType , String fieldType);
}
/**
* Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
*
* @Package: com.github.mybatis.fl.convert
* @author: flying-cattle
* @date: 2019年4月9日 下午8:13:13
*/
package com.github.mybatis.fl.convert;
import com.github.mybatis.fl.convert.ITypeConvert;
/**
* Copyright: Copyright (c) 2019
*
* @ClassName: MySqlTypeConvert.java
* @Description: MYSQL 数据库字段类型转换
*
* @version: v1.0.0
* @author: flying-cattle
* @date: 2019年4月9日 下午8:13:13
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------------*
* 2019年4月9日 flying-cattle v2.0.1 initialize
*/
public class MySqlTypeConvert implements ITypeConvert {
@Override
public IColumnType processTypeConvert(DateType dateType, String fieldType) {
String t = fieldType.toLowerCase();
if (t.contains("char")) {
return DbColumnType.STRING;
} else if (t.contains("bigint")) {
return DbColumnType.LONG;
} else if (t.contains("tinyint(1)")) {
return DbColumnType.BOOLEAN;
} else if (t.contains("int")) {
return DbColumnType.INTEGER;
} else if (t.contains("text")) {
return DbColumnType.STRING;
} else if (t.contains("bit")) {
return DbColumnType.BOOLEAN;
} else if (t.contains("decimal")) {
return DbColumnType.BIG_DECIMAL;
} else if (t.contains("clob")) {
return DbColumnType.CLOB;
} else if (t.contains("blob")) {
return DbColumnType.BLOB;
} else if (t.contains("binary")) {
return DbColumnType.BYTE_ARRAY;
} else if (t.contains("float")) {
return DbColumnType.FLOAT;
} else if (t.contains("double")) {
return DbColumnType.DOUBLE;
} else if (t.contains("json") || t.contains("enum")) {
return DbColumnType.STRING;
} else if (t.contains("date") || t.contains("time") || t.contains("year")) {
switch (dateType) {
case ONLY_DATE:
return DbColumnType.DATE;
case SQL_PACK:
switch (t) {
case "date":
return DbColumnType.DATE_SQL;
case "time":
return DbColumnType.TIME;
case "year":
return DbColumnType.DATE_SQL;
default:
return DbColumnType.TIMESTAMP;
}
case TIME_PACK:
switch (t) {
case "date":
return DbColumnType.LOCAL_DATE;
case "time":
return DbColumnType.LOCAL_TIME;
case "year":
return DbColumnType.YEAR;
default:
return DbColumnType.LOCAL_DATE_TIME;
}
}
}
return DbColumnType.STRING;
}
}
package com.github.mybatis.entity;
package com.github.mybatis.fl.entity;
import java.io.Serializable;
import java.util.List;
......
package com.github.mybatis.entity;
package com.github.mybatis.fl.entity;
import java.io.Serializable;
import lombok.AllArgsConstructor;
......
package com.github.mybatis.entity;
package com.github.mybatis.fl.entity;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult implements Serializable{
......
package com.github.mybatis.entity;
package com.github.mybatis.fl.entity;
import java.io.Serializable;
......
package com.github.mybatis.test;
package com.github.mybatis.fl.test;
import java.sql.SQLException;
import java.util.Date;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.util.EntityInfoUtil;
import com.github.mybatis.util.Generator;
import com.github.mybatis.util.MySqlToJavaUtil;
import com.github.mybatis.fl.entity.BasisInfo;
import com.github.mybatis.fl.util.EntityInfoUtil;
import com.github.mybatis.fl.util.Generator;
import com.github.mybatis.fl.util.MySqlToJavaUtil;
public class TestMain {
//基础信息
......
package com.github.mybatis.util;
package com.github.mybatis.fl.util;
import java.sql.Connection;
import java.sql.DriverManager;
......@@ -8,8 +8,8 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.entity.PropertyInfo;
import com.github.mybatis.fl.entity.BasisInfo;
import com.github.mybatis.fl.entity.PropertyInfo;
public class EntityInfoUtil {
public static BasisInfo getInfo(BasisInfo bi) throws SQLException {
......
package com.github.mybatis.util;
package com.github.mybatis.fl.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.entity.JsonResult;
import com.github.mybatis.fl.entity.BasisInfo;
import com.github.mybatis.fl.entity.JsonResult;
import freemarker.template.Configuration;
import freemarker.template.Template;
......
package com.github.mybatis.util;
package com.github.mybatis.fl.util;
import java.util.List;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.entity.JsonResult;
import com.github.mybatis.entity.PropertyInfo;
import com.github.mybatis.fl.entity.BasisInfo;
import com.github.mybatis.fl.entity.JsonResult;
import com.github.mybatis.fl.entity.PropertyInfo;
public class Generator {
//路径信息
......
package com.github.mybatis.util;
package com.github.mybatis.fl.util;
import com.github.mybatis.fl.convert.DateType;
import com.github.mybatis.fl.convert.MySqlTypeConvert;
public class MySqlToJavaUtil {
......@@ -23,27 +26,7 @@ public class MySqlToJavaUtil {
}
public static String jdbcTypeToJavaType(String sqlType) {
if (sqlType.equalsIgnoreCase("bit")) {
return "Boolean";
} else if (sqlType.equalsIgnoreCase("tinyint")) {
return "Integer";
} else if (sqlType.equalsIgnoreCase("smallint")) {
return "Integer";
} else if (sqlType.equalsIgnoreCase("int")) {
return "Integer";
} else if (sqlType.equalsIgnoreCase("bigint")) {
return "Long";
} else if (sqlType.equalsIgnoreCase("float")) {
return "Float";
} else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric") || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money") || sqlType.equalsIgnoreCase("smallmoney")) {
return "Double";
} else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar") || sqlType.equalsIgnoreCase("text")) {
return "String";
} else if (sqlType.equalsIgnoreCase("datetime") || sqlType.equalsIgnoreCase("date") || sqlType.equalsIgnoreCase("timestamp")) {
return "Date";
} else if (sqlType.equalsIgnoreCase("image")) {
return "Blod";
}
return null;
MySqlTypeConvert typeConvert= new MySqlTypeConvert();
return typeConvert.processTypeConvert(DateType.ONLY_DATE, sqlType).getType();
}
}
......@@ -13,7 +13,6 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
......@@ -26,7 +25,6 @@ import lombok.AllArgsConstructor;
*
*/
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class ${entityName} implements Serializable {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment