Java 将变量传递给映射的 DTO 方法?
Java pass variable into mapped DTO method?
我有 Spring 引导应用程序,其实现包含具有以下功能的方法。该实现使用 2 个 DTO 来绑定数据。有没有一种适当的方法可以将值从 JAY 传递到值 where '10.00' 是硬编码的吗?我的主要问题是“this::convertProfileToProfileCreditDTO”是否可以在此表达式中传递参数?
我已经用Java DTO Object search mechanism?吸取了
如果我尝试在 this::convertProfileToProfileCreditDTO 下方截断的代码中添加参数,则会抱怨 return 类型
错误
convertProfileToProfileCreditDTO(final Profile theProfile, Double JAY)
实施
@Override
public Double testThisParam(Double profileCredit) {
Double JAY = profileCredit;
log.error(String.valueOf(JAY));
return JAY;
}
@Override
public Page<ProfileCreditDTO> findProfileBySelectedParameters(String username, Pageable pageable) {
Page<Profile> searchData= profileRepository.findByAllParameters(username, pageable);
Page<ProfileCreditDTO> searchProfileData=null;
if(searchData != null)
searchProfileData=searchData.map(this::convertProfileToProfileCreditDTO);
return searchProfileData;
}
public ProfileCreditDTO convertProfileToProfileCreditDTO(final Profile theProfile ){
if(theProfile == null)
return null;
ProfileCreditDTO theDTO= new ProfileCreditDTO();
theDTO.setProfile(theProfile);
CreditDTO theCreditDto = profileCreditClient.findClientByProfileId(theProfile.getId(), 10.00);
if(theCreditDto != null )
theDTO.setCredit(theCreditDto);
else {
return null;
}
return theDTO;
}
您始终可以将更多参数传递给 lambda 表达式
searchProfileData = searchData.map(x -> this.convertProfileToProfileCreditDTO(x, JAY));
附带说明一下,如果您想使用 this::
风格保持函数调用简单,您可以创建一个数据对象来携带所需的参数
class MyObject {
Profile theProfile;
Double JAY;
// public constructor with parameters
}
// then construct and use this
MyObject o = new MyObject(theProfile, testThisParam(...));
// and then change parameter of target method
convertProfileToProfileCreditDTO(MyObject myObject) ...
我有 Spring 引导应用程序,其实现包含具有以下功能的方法。该实现使用 2 个 DTO 来绑定数据。有没有一种适当的方法可以将值从 JAY 传递到值 where '10.00' 是硬编码的吗?我的主要问题是“this::convertProfileToProfileCreditDTO”是否可以在此表达式中传递参数?
我已经用Java DTO Object search mechanism?吸取了
如果我尝试在 this::convertProfileToProfileCreditDTO 下方截断的代码中添加参数,则会抱怨 return 类型
convertProfileToProfileCreditDTO(final Profile theProfile, Double JAY)
实施
@Override
public Double testThisParam(Double profileCredit) {
Double JAY = profileCredit;
log.error(String.valueOf(JAY));
return JAY;
}
@Override
public Page<ProfileCreditDTO> findProfileBySelectedParameters(String username, Pageable pageable) {
Page<Profile> searchData= profileRepository.findByAllParameters(username, pageable);
Page<ProfileCreditDTO> searchProfileData=null;
if(searchData != null)
searchProfileData=searchData.map(this::convertProfileToProfileCreditDTO);
return searchProfileData;
}
public ProfileCreditDTO convertProfileToProfileCreditDTO(final Profile theProfile ){
if(theProfile == null)
return null;
ProfileCreditDTO theDTO= new ProfileCreditDTO();
theDTO.setProfile(theProfile);
CreditDTO theCreditDto = profileCreditClient.findClientByProfileId(theProfile.getId(), 10.00);
if(theCreditDto != null )
theDTO.setCredit(theCreditDto);
else {
return null;
}
return theDTO;
}
您始终可以将更多参数传递给 lambda 表达式
searchProfileData = searchData.map(x -> this.convertProfileToProfileCreditDTO(x, JAY));
附带说明一下,如果您想使用 this::
风格保持函数调用简单,您可以创建一个数据对象来携带所需的参数
class MyObject {
Profile theProfile;
Double JAY;
// public constructor with parameters
}
// then construct and use this
MyObject o = new MyObject(theProfile, testThisParam(...));
// and then change parameter of target method
convertProfileToProfileCreditDTO(MyObject myObject) ...