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

时间:2023-02-03 07:06:26

I'm working on a small web application where I have to store image of a user into Mysql database. I'm new to the hibernate framework and I'm struck here. I have converted the image into Base64 string. Can anyone suggest me how to store this string into database using hibernate.

我正在开发一个小型的web应用程序,需要将用户的映像存储到Mysql数据库中。我是hibernate框架的新手,我很吃惊。我已将图像转换为Base64字符串。有人能告诉我如何使用hibernate将这个字符串存储到数据库中吗?

1 个解决方案

#1


1  

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字符串,存储图像内容的最好办法不是将它存储为Base64字符串而是作为字节数组(因为它可以大)通过简单地定义你的能量场与JPA注释@Lob(代表大型对象)和JPA注释@Basic(fetch = FetchType.LAZY)如果你想取回它懒洋洋地,作为下一个:

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

#1


1  

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字符串,存储图像内容的最好办法不是将它存储为Base64字符串而是作为字节数组(因为它可以大)通过简单地定义你的能量场与JPA注释@Lob(代表大型对象)和JPA注释@Basic(fetch = FetchType.LAZY)如果你想取回它懒洋洋地,作为下一个:

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