在 MYSQL 数据库中存储 base64 字符串的最佳方法是什么?

What is the best way to store base64 string in MYSQL database?

我正在开发一个小型 Web 应用程序,我必须在其中将用户图像存储到 Mysql 数据库中。我是 hibernate 框架的新手,我对这里很感兴趣。我已将图像转换为 Base64 字符串。谁能建议我如何使用休眠将此字符串存储到数据库中。

除非您可以为 Base64 String 定义最大尺寸,否则存储图像内容的最佳方式不是将其存储为 Base64 String,而是而不是作为一个字节数组(因为它可能很大),只需使用 JPA 注释 @Lob (stands for Large Object) and with the JPA annotation @Basic(fetch = FetchType.LAZY) 定义您的字段,以防您想延迟获取它,如下所示:

@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] image;

我推荐以下

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "file64", columnDefinition = "LONGBLOB")
private byte[] file64;

LONGBLOB允许保存更大的内容。