简体   繁体   English

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

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

I'm working on a small web application where I have to store image of a user into Mysql database.我正在开发一个小型 Web 应用程序,我必须将用户的图像存储到 Mysql 数据库中。 I'm new to the hibernate framework and I'm struck here.我是 hibernate 框架的新手,我很震惊。 I have converted the image into Base64 string.我已将图像转换为 Base64 字符串。 Can anyone suggest me how to store this string into database using hibernate.任何人都可以建议我如何使用休眠将此字符串存储到数据库中。

Unless you can define a max size to your Base64 String , the best way to store your image content is not to store it as Base64 String but rather as an array of bytes (since it could be big) by simply defining your field with the JPA annotations @Lob (stands for Large Object) and with the JPA annotation @Basic(fetch = FetchType.LAZY) in case you want to fetch it lazily, as next:除非您可以为Base64 String定义最大大小,否则存储图像内容的最佳方法不是将其存储为Base64 String而是通过简单地使用JPA定义您的字段而将其存储为字节数组(因为它可能很大)注释@Lob (代表大对象)和JPA注释@Lob ( @Basic(fetch = FetchType.LAZY)以防你想懒惰地获取它,如下所示:

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

I recommend the below我推荐以下

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

The LONGBLOB allows to save larger content. LONGBLOB允许保存更大的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM