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

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

Spring boot, storing CRUD operations in abstract service

问题

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

@Entity
@Table(name = "A")
public class A {...}

@Entity
@Table(name = "B")
public class B {...}

还有以下服务:

@Service
public class AServiceImpl implements AService {

    @Autowired
    private ARepository aRepository;
    
    public void create() {...}
    public void update(long id) {...}
    public void doA() {...}

}

@Service
public class BServiceImpl implements BService {

    @Autowired
    private BRepository bRepository;
    
    public void create() {...}
    public void update(long id) {...}
    public void doB() {...}

}

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

英文:

I have two JPA entites like

@Entity
@Table(name = "A")
public class A {...}

@Entity
@Table(name = "B")
public class B {...}

And services like

@Service
public class AServiceImpl implements AService {

    @Autowired
    private ARepository aRepository;
    
    public void create() {...}
    public void update(long id) {...}
    public void doA() {...}

}

@Service
public class BServiceImpl implements BService {

    @Autowired
    private BRepository bRepository;
    
    public void create() {...}
    public void update(long id) {...}
    public void doB() {...}

}

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.

public class RepositoryService<R extends JpaRepository<T, Long>, T> {

  @Autowired R repository;

  public void insert(T object) {
    repository.save(object);
  }

}

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

public abstract class RepositoryService<T>{

  abstract Class getRepositoryClass();

  Map<Class, GenericRepository> repositories;

  @Autowired
  List<GenericRepository> repositoriesList;

  @PostConstruct
  public void setupMap() {
    // Convert List into Map, with the class of Object to be saved.
  }

  public void insert(T object) {
    repositories.get(object.getClass()).save(object);
  }

}
英文:

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

I think you can use Genarics over here.

public class RepositoryService &lt;R extends JpaRepository&lt;T, Long&gt;, T&gt; {

  @Autowired R repository;

  public void insert(T object) {
    repository.save(object);
  }

}

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

public abstract class RepositoryService &lt;T&gt;{

  abstract Class getRepositoryClass();

  Map&lt;Class, GenericRepository&gt; repositories;

  @Autowired
  List&lt;GenericRepository&gt; repositoriesList;

  @PostConstruct
  public void setupMap() {
    // Convert List into Map, with the class of Objest to be saved.
  }

  public void insert(T object) {
    repositories.get(object.getClass()).save(object);
  }

}

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:

确定