Spring Boot,将CRUD操作存储在抽象服务中

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

Spring boot, storing CRUD operations in abstract service

问题

我有两个 JPA 实体,如下所示:

  1. @Entity
  2. @Table(name = "A")
  3. public class A {...}
  4. @Entity
  5. @Table(name = "B")
  6. public class B {...}

还有以下服务:

  1. @Service
  2. public class AServiceImpl implements AService {
  3. @Autowired
  4. private ARepository aRepository;
  5. public void create() {...}
  6. public void update(long id) {...}
  7. public void doA() {...}
  8. }
  9. @Service
  10. public class BServiceImpl implements BService {
  11. @Autowired
  12. private BRepository bRepository;
  13. public void create() {...}
  14. public void update(long id) {...}
  15. public void doB() {...}
  16. }

因此,在方法createupdate中,我有仅由仓库不同的代码。我想将 CRUD 方法存储在其他一些基本服务中,但实际上不太理解如何管理仓库继承。有任何帮助,谢谢。

英文:

I have two JPA entites like

  1. @Entity
  2. @Table(name = "A")
  3. public class A {...}
  4. @Entity
  5. @Table(name = "B")
  6. public class B {...}

And services like

  1. @Service
  2. public class AServiceImpl implements AService {
  3. @Autowired
  4. private ARepository aRepository;
  5. public void create() {...}
  6. public void update(long id) {...}
  7. public void doA() {...}
  8. }
  9. @Service
  10. public class BServiceImpl implements BService {
  11. @Autowired
  12. private BRepository bRepository;
  13. public void create() {...}
  14. public void update(long id) {...}
  15. public void doB() {...}
  16. }

So, in methods create and update I have code what differs only by repository. I would like to store crud methods in some other, basic service, but do not really understand how could I manage repository inheritance.
Any help, please.

答案1

得分: 2

I think you can use Genarics over here.

  1. public class RepositoryService<R extends JpaRepository<T, Long>, T> {
  2. @Autowired R repository;
  3. public void insert(T object) {
  4. repository.save(object);
  5. }
  6. }

How to effectively use these repositories, that details you can refer to this Baeldung article - https://www.baeldung.com/spring-autowire-generics

Update: This is an alternate approach you can try out

  1. public abstract class RepositoryService<T>{
  2. abstract Class getRepositoryClass();
  3. Map<Class, GenericRepository> repositories;
  4. @Autowired
  5. List<GenericRepository> repositoriesList;
  6. @PostConstruct
  7. public void setupMap() {
  8. // Convert List into Map, with the class of Object to be saved.
  9. }
  10. public void insert(T object) {
  11. repositories.get(object.getClass()).save(object);
  12. }
  13. }
英文:

IMP: This code is compiling, I havent tested it.

I think you can use Genarics over here.

  1. public class RepositoryService &lt;R extends JpaRepository&lt;T, Long&gt;, T&gt; {
  2. @Autowired R repository;
  3. public void insert(T object) {
  4. repository.save(object);
  5. }
  6. }

How to effectively use these reporitories, that details you can refer to this baeldung article - https://www.baeldung.com/spring-autowire-generics

Update: This is an alternate approach you can try out

  1. public abstract class RepositoryService &lt;T&gt;{
  2. abstract Class getRepositoryClass();
  3. Map&lt;Class, GenericRepository&gt; repositories;
  4. @Autowired
  5. List&lt;GenericRepository&gt; repositoriesList;
  6. @PostConstruct
  7. public void setupMap() {
  8. // Convert List into Map, with the class of Objest to be saved.
  9. }
  10. public void insert(T object) {
  11. repositories.get(object.getClass()).save(object);
  12. }
  13. }

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

发表评论

匿名网友

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

确定