应该Spring的@Repository跟随JpaRepository吗?

huangapple go评论64阅读模式
英文:

Should Spring @Repository follow JpaRepository?

问题

我想在 HashMap<> 或 ArrayList<> 中存储数据,而不需要数据库连接。
同时,我想创建一个管理数据的类,用于根据条件更新或获取数据。

在这种情况下,是否适合创建并使用 Repository 而不使用 JpaRepository?
或者是否有其他适当的方式?

@Repository
public interface ProductRepository  {
    private List<Product> productList; // = new ArrayList<>();

    public Product getProductById(int id) { ... };
    public boolean updateProduct() { ... };
}
英文:

I want to store data in HashMap<> or ArrayList<> without database connection.
Also, I want to create a class that manages data such as updating or getting data by conditions.

In this case, is it appropriate to create and use a Repository without using a JpaRepository?
Or is there another way that is appropriate?

@Repository
public interface ProductRepository  {
    private List&lt;Product&gt; productList; // = new ArrayList&lt;&gt;();

    public Product getProductByid(int id) { ... };
    public boolean updateProduct() { ... };
}

答案1

得分: 4

你应该查阅 Spring Data Key Value 文档:https://docs.spring.io/spring-data/keyvalue/docs/current/reference/html/#reference

Spring Data Key Value 适用于使用键值存储的 Spring 应用程序。

这里是一个简单的示例:http://www.henryxi.com/spring-data-keyvalue-example

编辑: 正如 doctore 在评论中正确指出的那样,这个示例更适合你的情况:https://www.baeldung.com/spring-data-key-value

英文:

You should look at Spring Data Key Value documentation: https://docs.spring.io/spring-data/keyvalue/docs/current/reference/html/#reference

Spring Data Key Value is for Spring applications that use key-value stores.

Here is simple example: http://www.henryxi.com/spring-data-keyvalue-example

EDIT: As doctore correctly stated in comment, this example is more suitable for your case: https://www.baeldung.com/spring-data-key-value

huangapple
  • 本文由 发表于 2020年10月11日 18:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定