我在网上也搜过很多,就是想知道在数据库中的建表语句的字段类型对应Java实体类中属性的类型是什么。

  结果网上一套一套的说法不一,完全不一致,有没有一致点的,不会错的!看我,你就有。

  于是我就无聊到用mybatis-generator插件一一生成对应关系,插件根据数据库建表语句自动生成Java实体类对象。现在开发都是自动生成实体类,我这里也生成后记录一下。

给出数据库图形界面,方便大家理解我在做什么

MySQL中的字段类型对应于Java对象中的数据类型_mysql类型对应java类型

sql如下

CREATE TABLE `testtype` (
  `int_type` int unsigned DEFAULT NULL,
  `int_unsigned` int NOT NULL,
  `bigint_unsigned20` bigint DEFAULT NULL,
  `bigint_unsigned255` bigint DEFAULT NULL,
  `big_int` bigint DEFAULT NULL,
  `big_int_255` bigint DEFAULT NULL,
  `varchar` varchar(255) DEFAULT NULL,
  `bit` bit(20) DEFAULT NULL,
  `bit_64` bit(64) DEFAULT NULL,
  `tiny_int` tinyint DEFAULT NULL,
  `tiny_int_unsigned` tinyint unsigned DEFAULT NULL,
  `small_int` smallint DEFAULT NULL,
  `small_int_unsigned` smallint DEFAULT NULL,
  `binary` binary(255) DEFAULT NULL,
  `blob` blob,
  `char_utf8` char(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `char_utf8mb4` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `char_utf8gbk` char(255) CHARACTER SET gbk COLLATE gbk_chinese_ci DEFAULT NULL,
  `date` date DEFAULT NULL,
  `datetime` datetime DEFAULT NULL,
  `timestamp` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `float_type` float DEFAULT NULL,
  `float_unsigned` float unsigned DEFAULT NULL,
  `decimal` decimal(10,0) DEFAULT NULL,
  `numeric` decimal(10,0) DEFAULT NULL,
  `double_type` double DEFAULT NULL,
  `double_unsigned` double unsigned DEFAULT NULL,
  `integer_type` int DEFAULT NULL,
  `integer_unsigned` int unsigned DEFAULT NULL,
  `text` text,
  `time` time DEFAULT NULL,
  `tinytext` tinytext,
  `year` year DEFAULT NULL,
  `enum_type` enum('1','red') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  PRIMARY KEY (`int_unsigned`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

生成的Java实体类对象如下(篇幅原因,我删掉自动生成的gettersetter):

public class testType {
    private Integer intUnsigned;
    private Integer intType;
    private Long bigintUnsigned20;
    private Long bigintUnsigned255;
    private Long bigInt;
    private Long bigInt255;
    private String varchar;
    private byte[] bit;
    private byte[] bit64;
    private Byte tinyInt;
    private Byte tinyIntUnsigned;
    private Short smallInt;
    private Short smallIntUnsigned;
    private String charUtf8;
    private String charUtf8mb4;
    private String charUtf8gbk;
    private Date date;
    private Date datetime;
    private Date timestamp;
    private Float floatType;
    private Float floatUnsigned;
    private Long decimal;
    private Long numeric;
    private Double doubleType;
    private Double doubleUnsigned;
    private Integer integerType;
    private Integer integerUnsigned;
    private Date time;
    private String tinytext;
    private Date year;
    private String enumType;
}

表我给大家列出来了,帅的人已经点赞、关注、收藏一键三连了,谁偷看一下就溜走?后续有补充,就不重复前面例子里面的类型了,直接看下表就行

MySQL数据类型

Java实体类属性类型

说明

int

Integer

不管是signed还是unsigned,Java实体类型都是Integer

bigint

Long

不管是bigint(xxx)括号多少位,不管signed还是unsigned,Java实体类型都是Long

bit

byte[]

-

tinyint

Byte

不管是signed还是unsigned,Java实体类型都是Byte,在java.lang包下

smallint

Short

不管是signed还是unsigned,Java实体类型都是Short

char

String

不管char是gbk、utf8、utf8mb4等编码类型,Java实体类型都是String

varchar

String

不管char是gbk、utf8、utf8mb4等编码类型,Java实体类型都是String

longvarchar

String

不管char是gbk、utf8、utf8mb4等编码类型,Java实体类型都是String

date

Date

java.util.Date

datetime

Date

java.util.Date

timestamp

Date

java.util.Date

time

Date

java.util.Date

float

Float

不管是signed还是unsigned,Java实体类型都是Float

decimal

Long

-

numeric

Long

-

double

Double

不管是signed还是unsigned,Java实体类型都是Double

tinytext

String

-

text

String

-

year

Date

java.util.Date

enum

String

-

  有些类型插件没有自动转换过来,我就不列举,这里就列举常用的并且插件能转换过来的,这肯定是对的没错。

  实际的映射关系仍然取决于数据库和驱动程序的支持情况以及项目需求,比如NUMERICDECIMAL 映射为 java.math.BigDecimal 类型也是对的

后续设计表规范内容:

1.从8.0.17版本开始,TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT类型的显示宽度将失效。比如bigint(20),如果用navicat直接保存长度20,最终在建表语句被保存为bigint,长度会失效。
2.自增字段类型必须是整型而且必须是unsigned,推荐int或者bigint,并且自增字段必须是主键或者主键的一部分,我个人写物理主键id一般就是bigint unsigned
3.手机号使用varchar(20),不要使用整数。
4.对于精确浮点型数据存储,需要使用decimal,严禁使用floatdouble
5.如无特殊需要,禁止开发人员使用blob
6.日期类型字段不能使用varchar或者char,只能使用datedatetime字段类型存放。
7.所有只需要精确到天的字段全部使用date类型,而不应该使用timestamp或者datetime类型。
8.所有需要精确到时分秒的字段均使用datetime,不要使用timestamp类型,timestamp2038年就过期了。
9.不建议使用enumset类型,使用tinyint替代。
10.仅仅只有单个字符的字段用char(1),比如性别字段。
11.按照规范,每个列定义的时候必须加上comments,我上面举例子偷懒了所以没写。
12.数据库的字符集只能选择utf8mb4,如果需要导出,也需要显式选择utf8mb4作为导出格式。


欢迎一键三连~



有问题请留言,大家一起探讨学习



----------------------Talk is cheap, show me the code-----------------------