英文:
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<Product> productList; // = new ArrayList<>();
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论