【问题标题】:How to save @Lob data from spring in to a Postgres database?如何将 @Lob 数据从 spring 保存到 Postgres 数据库中?
【发布时间】:2020-12-05 10:39:17
【问题描述】:

我想在 Spring 中使用 @Lob 注释将编码图像保存到 Postgres 数据库中。测试应用程序时没有错误,我浏览图像然后保存它。但是当我打开数据库而不是图像列中的 base64 编码图像时,我只有几个数字(41417、41418 等)。

这是我课堂上的部分代码。

public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private String name;

@Lob
private String imageBase64;

这是我用来将对象保存到数据库中的函数。

public Product saveProduct(Product product, MultipartFile image) throws IOException {
    if (image != null && !image.getName().isEmpty()) {
        byte[] bytes = image.getBytes();
        String base64Image = String.format("data:%s;base64,%s", image.getContentType(), Base64.getEncoder().encodeToString(bytes));
        product.setImageBase64(base64Image);
    }
    return this.productRepository.save(product);
}

但是当它保存到数据库中时,它看起来像这样(列 image_base64)。

【问题讨论】:

    标签: java spring postgresql spring-data-jpa spring-data


    【解决方案1】:

    您的@Lob确实正确保存。您看到的值 (41417) 表示其 OID。

    要查看大对象的内容,您可以使用 Postgres 的 lo_get 函数(假设您使用的是 9.4+ 版本):

    SELECT lo_get(cast(image_base64 as bigint)) FROM products WHERE id = 1;
    

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 2011-10-26
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2021-02-02
      • 2019-01-11
      相关资源
      最近更新 更多