java数据类型对应postgresql的数据类型

  java8 postgreSQL Oracle
1 LocalDate date  
2 LocalTime time  
3 LocalDateTime timestamp without timezone  
4 OffsetDateTime timestamp with timezone  
5 String varchar  
6 String text  
7 Integer int2  
8 Integer int4  
9 Long int8  
10 Float float4  
11 Double float8  
12 BigDecimal numeric  
13 Boolean bool  
       
       
       
       
       
       

总结java数据类型和mysql、oracle、pgsql数据类型对应关系,附数据库字段类型转java类型代码

https://blog.csdn.net/qq_21187515/article/details/91495237?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-3.control

 

mysql字段类型转java类型的方法代码
public class MySqlTypeConvert implements ITypeConvert {
    public MySqlTypeConvert() {
    }

    /**
     * mysql字段类型转java类型
     * @param fieldType mysql字段类型
     * @return
     */
    public DbColumnType processTypeConvert(String fieldType) {
        String t = fieldType.toLowerCase();
        if (!t.contains("char") && !t.contains("text")) {
            if (t.contains("bigint")) {
                return DbColumnType.LONG;
            } else if (t.contains("int")) {
                return DbColumnType.INTEGER;
            } else if (!t.contains("date") && !t.contains("time") && !t.contains("year")) {
                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 {
                    return !t.contains("json") && !t.contains("enum") ? DbColumnType.STRING : DbColumnType.STRING;
                }
            } else {
                return DbColumnType.DATE;
            }
        } else {
            return DbColumnType.STRING;
        }
    }
}


oracle字段类型转java类型的方法代码
public class OracleTypeConvert implements ITypeConvert {
    public OracleTypeConvert() {
    }

    /**
     * oracle字段类型转java类型
     * @param fieldType oracle字段类型
     * @return
     */
    public DbColumnType processTypeConvert(String fieldType) {
        String t = fieldType.toUpperCase();
        if (t.contains("CHAR")) {
            return DbColumnType.STRING;
        } else if (!t.contains("DATE") && !t.contains("TIMESTAMP")) {
            if (t.contains("NUMBER")) {
                if (t.matches("NUMBER\\(+\\d\\)")) {
                    return DbColumnType.INTEGER;
                } else {
                    return t.matches("NUMBER\\(+\\d{2}+\\)") ? DbColumnType.LONG : DbColumnType.DOUBLE;
                }
            } else if (t.contains("FLOAT")) {
                return DbColumnType.FLOAT;
            } else if (t.contains("clob")) {
                return DbColumnType.CLOB;
            } else if (t.contains("BLOB")) {
                return DbColumnType.OBJECT;
            } else if (t.contains("binary")) {
                return DbColumnType.BYTE_ARRAY;
            } else {
                return t.contains("RAW") ? DbColumnType.BYTE_ARRAY : DbColumnType.STRING;
            }
        } else {
            return DbColumnType.DATE;
        }
    }
}

pgsql字段类型转java类型的方法代码
public class PostgreSqlTypeConvert implements ITypeConvert {
    public PostgreSqlTypeConvert() {
    }

    public DbColumnType processTypeConvert(String fieldType) {
        String t = fieldType.toLowerCase();
        if (!t.contains("char") && !t.contains("text")) {
            if (t.contains("bigint")) {
                return DbColumnType.LONG;
            } else if (t.contains("int")) {
                return DbColumnType.INTEGER;
            } else if (!t.contains("date") && !t.contains("time") && !t.contains("year")) {
                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.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 t.contains("boolean") ? DbColumnType.BOOLEAN : DbColumnType.STRING;
                } else {
                    return DbColumnType.STRING;
                }
            } else {
                return DbColumnType.DATE;
            }
        } else {
            return DbColumnType.STRING;
        }
    }
}

public enum DbColumnType {
    BASE_INT("int", (String)null),
    BASE_BOOLEAN("boolean", (String)null),
    BASE_FLOAT("float", (String)null),
    BASE_DOUBLE("double", (String)null),
    STRING("String", (String)null),
    LONG("Long", (String)null),
    INTEGER("Integer", (String)null),
    FLOAT("Float", (String)null),
    DOUBLE("Double", (String)null),
    BOOLEAN("Boolean", (String)null),
    BYTE_ARRAY("byte[]", (String)null),
    CHARACTER("Character", (String)null),
    OBJECT("Object", (String)null),
    DATE("Date", "java.util.Date"),
    TIME("Time", "java.sql.Time"),
    BLOB("Blob", "java.sql.Blob"),
    CLOB("Clob", "java.sql.Clob"),
    TIMESTAMP("Timestamp", "java.sql.Timestamp"),
    BIG_INTEGER("BigInteger", "java.math.BigInteger"),
    BIG_DECIMAL("BigDecimal", "java.math.BigDecimal"),
    LOCAL_DATE("LocalDate", "java.time.LocalDate"),
    LOCAL_TIME("LocalTime", "java.time.LocalTime"),
    LOCAL_DATE_TIME("LocalDateTime", "java.time.LocalDateTime");

    private final String type;
    private final String pkg;

    private DbColumnType(String type, String pkg) {
        this.type = type;
        this.pkg = pkg;
    }

    public String getType() {
        return this.type;
    }

    public String getPkg() {
        return this.pkg;
    }
}

  

 

注:
这里参考了mybatis-plus-2.0.6.jar的生成实体类的源码,具体可以查看mybatis-plus的源码

 

posted @ 2021-06-10 20:30  奥兰王子  阅读(9535)  评论(1编辑  收藏  举报