使用Mysql JSON类型的Spring数据

时间:2022-05-06 16:32:45

We are using spring data with JPA in our project. Out MySQL server version is 5.7.

我们在项目中使用JPA的spring数据。MySQL服务器版本是5.7。

I have two questions:

我有两个问题:

1) Does spring data compatible with persisting objects into the new JSON type on MySQL db? in other words, I would like to have an entity that instead of having multiple columns in its table - it will contain a single column with the JSON type.

1)在MySQL db上,spring数据是否兼容将对象持久化到新的JSON类型中?换句话说,我希望有一个实体,它的表中不包含多个列,而是包含一个具有JSON类型的列。

2) Does spring data repositories are compatible with such mechanism? e.g. (automated code generation for CRUD operations via the repositories interface)?

2) spring数据存储库是否与这种机制兼容?例如(通过存储库接口自动生成CRUD操作的代码)?

1 个解决方案

#1


2  

According with Spring Data Docs Appendix D: Repository query return types, the only supported types are: void, primitives, Wrapper types, T, Iterator, Collection, List, Optional, Stream, Future, CompletableFuture, ListenableFuture, Slice, Page, GeoResult, GeoResults, GeoPage.

根据Spring Data Docs附录D: Repository query return类型,唯一支持的类型是:void、原语、包装器类型、T、迭代器、集合、列表、可选、流、Future、CompletableFuture、ListenableFuture、Slice、Page、GeoResult、GeoResults、GeoPage。

As you can see, for now, it's not supported. One of the ideas behind it I think that it's not a common sense of all databases yet.

正如您现在看到的,它不受支持。我认为它背后的一个想法是它还不是所有数据库的常识。

Obviously, you can use this storing as Json, and create a converter for it:

显然,您可以使用这个存储为Json,并为其创建一个转换器:

  @Column(name = "configuration", nullable = false)
  @Convert(converter = PluginAnalyzerConfigConverter.class)
  private PluginAnalyzerConfig configuration;

and:

和:

public class PluginAnalyzerConfigConverter implements
    AttributeConverter<PluginAnalyzerConfig, String> {

  @Override public String convertToDatabaseColumn(PluginAnalyzerConfig config) {
    Gson parser = new Gson();
    return parser.toJson(config, PluginAnalyzerConfig.class);
  }

  @Override public PluginAnalyzerConfig convertToEntityAttribute(String source) {
    Gson parser = new Gson();
    return parser.fromJson(source, PluginAnalyzerConfig.class);
  }
}

Obviously that without that approach, you will not make usage of Json in a nice way like MySQL is capable of. But I think that there's no problem if you create MySQL specialized queries to make use of it.

显然,如果没有这种方法,就不能像MySQL那样很好地使用Json。但是我认为,如果你创建MySQL专门的查询来使用它,这是没有问题的。

#1


2  

According with Spring Data Docs Appendix D: Repository query return types, the only supported types are: void, primitives, Wrapper types, T, Iterator, Collection, List, Optional, Stream, Future, CompletableFuture, ListenableFuture, Slice, Page, GeoResult, GeoResults, GeoPage.

根据Spring Data Docs附录D: Repository query return类型,唯一支持的类型是:void、原语、包装器类型、T、迭代器、集合、列表、可选、流、Future、CompletableFuture、ListenableFuture、Slice、Page、GeoResult、GeoResults、GeoPage。

As you can see, for now, it's not supported. One of the ideas behind it I think that it's not a common sense of all databases yet.

正如您现在看到的,它不受支持。我认为它背后的一个想法是它还不是所有数据库的常识。

Obviously, you can use this storing as Json, and create a converter for it:

显然,您可以使用这个存储为Json,并为其创建一个转换器:

  @Column(name = "configuration", nullable = false)
  @Convert(converter = PluginAnalyzerConfigConverter.class)
  private PluginAnalyzerConfig configuration;

and:

和:

public class PluginAnalyzerConfigConverter implements
    AttributeConverter<PluginAnalyzerConfig, String> {

  @Override public String convertToDatabaseColumn(PluginAnalyzerConfig config) {
    Gson parser = new Gson();
    return parser.toJson(config, PluginAnalyzerConfig.class);
  }

  @Override public PluginAnalyzerConfig convertToEntityAttribute(String source) {
    Gson parser = new Gson();
    return parser.fromJson(source, PluginAnalyzerConfig.class);
  }
}

Obviously that without that approach, you will not make usage of Json in a nice way like MySQL is capable of. But I think that there's no problem if you create MySQL specialized queries to make use of it.

显然,如果没有这种方法,就不能像MySQL那样很好地使用Json。但是我认为,如果你创建MySQL专门的查询来使用它,这是没有问题的。