【发布时间】:2020-12-14 20:00:26
【问题描述】:
我带着一个困惑来到你面前,也许你可以向我澄清。
我现在正在学习如何使用 Spring MVC 的 3 层架构(使用 Repository、Service、Controllers),但我不明白以下内容以及我应该如何去做:
-服务层,这里,我没看懂,接口中定义的方法应该和JPA给我们的或者在那个实体上自定义的方法一样(比如Product,使用 getBrand 等方法)或者我们应该为每个实体使用什么方法,我们怎么知道该使用什么?
-从找到的示例中观察到的另一件事是,对于 Product 实体,在 Service 中使用了一个新类 ProductData,具有我们愿意查看的那些实例,是否可以这样做或继续使用我们的实体?如果是,在服务方法中,我们应该使用 ProductData 而不是 Product 实体? 示例:
public ProductData findById(Integer id){
ProductEntity product = productRepo.findById(id);
ProductData data = new ProductData(product.getId(), product.getName(), product.getCeva());
return data;}
-如果我们在Service中使用ProductData,我们也应该在controller中使用它,对吧?
-例如,在 MVC 中,实用程序类的用途是什么,我们应该何时以及如何使用它们?
感谢您的耐心和帮助,我一直在谷歌上搜索这些东西,但我没有找到任何结论,只是个人喜好......
【问题讨论】:
标签: java spring model-view-controller service layer