gpt4 book ai didi

java - 如何有效地从服务中调用 Spring 存储库方法?

转载 作者:行者123 更新时间:2023-12-02 00:41:42 25 4
gpt4 key购买 nike

我有一个如下代码块:

@Repository
public class SummaryDao implements ISummaryDao {

public List<Response> reportOfCategory(RequestObject obj){
...
}

public List<Response> reportOfGroup(RequestObject obj){
...
}

public List<Response> reportOfSubGroup(RequestObject obj){
...
}

}

我的第二个存储库:

@Repository
public class ReportDao implements IReportDao {

public List<Response> reportOfCategory(RequestObject obj){
...
}

public List<Response> reportOfGroup(RequestObject obj){
...
}

public List<Response> reportOfSubGroup(RequestObject obj){
...
}

}

然后我从服务类中调用它们,如下所示:

@Service
public class ReportService {

@Autowired
ISummaryDao summaryDao;

@Autowired
IReportDao reportDao;

public List<Response> call(EnumType type, RequestObject obj){

if(type == Category) {
return obj.isSummary ? summaryDao.reportOfCategory : reportDao.reportOfCategory;
}
if(type == Group) {
return obj.isSummary ? summaryDao.reportOfGroup : reportDao.reportOfGroup;
}
if(type == SubGroup) {
return obj.isSummary ? summaryDao.reportOfSubGroup : reportDao.reportOfSubGroup;
}
...
}

如何有效地调用它们?实际上我想逃避 if-else block 和 Enum 比较。

注意:枚举比较可以是 switch-case。

注意:在此代码中,我使用了 Spring-Boot EntityManager。我知道 JPARepository。这个问题与此无关。

最佳答案

我解决了这个问题,如下所示:

我创建了一个 IGeneralDao 来使用通用接口(interface)。

@Repository
public class SummaryDao implements IGeneralDao {

public List<Response> reportOfCategory(RequestObject obj){
...
}

public List<Response> reportOfGroup(RequestObject obj){
...
}

public List<Response> reportOfSubGroup(RequestObject obj){
...
}

}

第二个存储库:

@Repository
public class ReportDao implements IGeneralDao {

public List<Response> reportOfCategory(RequestObject obj){
...
}

public List<Response> reportOfGroup(RequestObject obj){
...
}

public List<Response> reportOfSubGroup(RequestObject obj){
...
}

}

调用它:

@Service
public class ReportService {

@Autowired
IGeneralDao reportDao, summaryDao;

public List<Response> call(EnumType type, RequestObject obj){
IGeneralDao generalDao = isSummary ? summaryDao : reportDao;
switch(type):
case Category:
return generalDao.reportOfCategory;
case Group:
return generalDao.reportOfGroup;
case SubGroup:
return generalDao.reportOfSubGroup;
...
}

逃避枚举比较:

public interface IGeneralDao {
<T> T callReports(RequestDto requestDto);
IGeneralDao setRequestType(EnumType type);
}

第一个存储库:

@Repository
public enum ReportDao implements IGeneralDao {
Category {
@Override
public List<Response> callReports(RequestDto requestDto) {
return ...;
}
},
Group {
@Override
public List<Response> callReports(RequestDto requestDto) {
return ...;
}
},
SubGroup {
@Override
public List<Response> callReports(RequestDto requestDto) {
return ...;
}
};

@Override
public IGenericDao setEnumType(EnumType type) {
return valueOf(type.name());
}
}

第二个存储库:

@Repository
public enum SummaryDao implements IGeneralDao {
Category {
@Override
public Summary callReports(RequestDto requestDto) {
return ...;
}
},
Group {
@Override
public Summary callReports(RequestDto requestDto) {
return ...;
}
},
SubGroup {
@Override
public Summary callReports(RequestDto requestDto) {
return ...;
}
};

@Override
public IGenericDao setEnumType(EnumType type) {
return valueOf(type.name());
}
}

然后从主服务调用它:

@Service
public class ReportService {

@Autowired
IGeneralDao reportDao, summaryDao;

public List<Response> call(EnumType type, RequestObject obj){
IGeneralDao generalDao = isSummary ? summaryDao : reportDao;
return generalDao.setEnumType(type).callReports(obj);
}

关于java - 如何有效地从服务中调用 Spring 存储库方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57936253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com