【问题标题】:What is the best way to store base64 string in MYSQL database?在 MYSQL 数据库中存储 base64 字符串的最佳方法是什么?
【发布时间】:2017-03-14 11:01:45
【问题描述】:

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

【问题讨论】:

标签: java html mysql hibernate base64


【解决方案1】:

除非您可以为 Base64 String 定义最大大小,否则存储图像内容的最佳方法不是将其存储为 Base64 String 而是存储为字节数组(因为它可以要大)只需使用 JPA 注释 @Lob(代表大对象)和 JPA 注释 @Basic(fetch = FetchType.LAZY) 定义您的字段,以防您想懒惰地获取它,如下所示:

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

【讨论】:

    【解决方案2】:

    我推荐以下

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

    LONGBLOB 允许保存更大的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2016-01-03
      • 2010-11-25
      • 1970-01-01
      • 2020-04-08
      • 2020-05-03
      • 1970-01-01
      相关资源
      最近更新 更多