-
MySQL支持大量的列类型,它可以被分为3类:数字类型、日期和时间类型以及字符串(字符)类型。Mysql针对字符类型大对象存储的解决方案是Lob对象。
-
@Lob 注解属性将被持久化为 Blog 或 Clob 类型
- Clob(Character Large Ojects)类型是长字符串类型,具体的java.sql.Clob, Character[], char[] 和 java.lang.String 将被持久化为 Clob 类型。
- Blob(Binary Large Objects)类型是字节类型,具体的java.sql.Blob, Byte[], byte[] 和 serializable type 将被持久化为 Blob 类型。
- @Lob 持久化为Blob或者Clob类型,根据get方法的返回值不同,自动进行Clob和Blob的转换。
- 因为这两种类型的数据一般占用的内存空间比较大,所以通常使用延迟加载的方式,与@Basic标记同时使用,设置加载方式为FetchType.LAZY。
在MySQL中Blob和Clob对应的的字段类型有
- tinytext (tintblob)
- text (blob)
- mediumtext (mediumblob)
- longtext (longblob)
一般在java中Clob类型用String声明,Blob用byte[]声明MySQL数据库对应字段,例如:
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "f_task_content", nullable = false, columnDefinition = "Text")
private String taskContent;
Note that if you generate your table from the JPA annotations, you can "control" the type MySQL will use by specifying the length attribute of the Column, for example:
@Lob @Basic(fetch = FetchType.LAZY)
@Column(length=100000)
private byte[] picture;
It all depends on the column type used for the picture column. Depending on your needs, use a:
- TINYBLOB: maximum length of 255 bytes
- BLOB: maximum length of 65,535 bytes
- MEDIUMBLOB: maximum length of 16,777,215 bytes
- LONGBLOB: maximum length of 4,294,967,295 bytes
Depending on the length, you'll get:
0 < length <= 255 --> `TINYBLOB`
255 < length <= 65535 --> `BLOB`
65535 < length <= 16777215 --> `MEDIUMBLOB`
16777215 < length <= 2³¹-1 --> `LONGBLOB`
reference:
[1]. https://blog.csdn.net/cnhome/article/details/42970253
[2]. https://www.jianshu.com/p/fadc24afe33c