gpt4 book ai didi

java - Dao实现中如何进行以下操作?

转载 作者:行者123 更新时间:2023-12-01 19:38:18 25 4
gpt4 key购买 nike

我从以下代码中得到了一个数组响应。

我能够返回上面的数组结果,但是如何使用该数组中的值之一返回 json 对象?我对 Java、springboot 和 hibernate 非常陌生。任何帮助将不胜感激!

目标计划 Controller

 @RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {

try {
logger.info("get recurring amount by accountid:->", accountID);
AccountsDTO[] goalPlan = goalPlanService.getCalculateRecurringAmount(accountID);
return new ResponseEntity<>(goalPlan, HttpStatus.OK);
}catch(Exception ex) {
logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
ErrorDTO errors = new ErrorDTO();
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());

return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);
}
}

这是 GoalPlanDaoImplementation

@Autowired
private GoalPlanRepository goalPlanRepository;


@Override
public List<Accounts> getCalculateRecurringAmount(String accountID) {
// TODO Auto-generated method stub
List<Accounts> goalPlan = null;
goalPlan = goalPlanRepository.getCalculateRecurringAmount(accountID);

return goalPlan.subList(0, 1);

}

目标计划存储库 ->

public interface GoalPlanRepository extends JpaRepository<GoalPlan, String>{    

@Query("select ac from Accounts ac where ac.userId = :accountID")
public List<Accounts> getCalculateRecurringAmount(@Param("accountID") String accountID);

}

我得到的数组结果如下

{
"accountID": "acc12345",
"accountName": "hellooee",
"accountType": "goalPlanner",
"userId": "abcd",
"bankName": null,
"bankId": null,
"debiitCardNumber": null,
"availableBalance": null,
}
]```


Now using accountID I need to return a json object like this


{
"calculatedGoalAmount": [
{
"goalFrequency": "Monthly",
"goalAmount": 0.4166666666666667,
"fromAccount": "acc12345"
},
{
"goalFrequency": "Quarterly",
"goalAmount": 1.25,
"fromAccount": "acc12345"
}
]
}



My AccountsDTO has folllowing

public class AccountsDTO {
private String accountID;
private String accountName;
private String accountType;
private String userId;
private String bankName;
private String bankId;
private String debitCardNumber;


//getters and setters
}


And initilAmount, goalTimePeriod, goalAmount are the values entered by user.
then i need to calculate
monthly = (goalAmount-initialAmount)/(12*goalTimePeriod)
quarterly = (goalAmount-initialAmount)/(4*goalTimePeriod)
accountId = (got from the response array above)

最佳答案

首先您需要创建两个类。

自定义响应类

public class CustomResponse {
private List<CalculatedGoalAmount> calculatedGoalAmount;

//getters and setters
}

CalculatedGoalAmount 类

public class CalculatedGoalAmount {
private String goalFrequency;
private double goalAmount;
private String fromAccount;

//getters and setters
}

然后在 getCalculateRecurringAmount 方法中编写以下代码。请注意,我不知道您的 AccountsDTO 类。

@RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {
CalculatedGoalAmount calculatedGoalAmount = null;
CustomResponse customResponse = null;
try {
customResponse = new CustomResponse();
AccountsDTO[] goalPlan = goalPlanService.getCalculateRecurringAmount(accountID);

for (AccountsDTO accountsDTO : goalPlan) {
calculatedGoalAmount = new CalculatedGoalAmount();
calculatedGoalAmount.setFromAccount(accountsDTO.getFromAccount());
calculatedGoalAmount.setGoalAmount(accountsDTO.getGoalAmount());
calculatedGoalAmount.setGoalFrequency(accountsDTO.getFrequency());

customResponse.getCalculatedGoalAmount().add(calculatedGoalAmount);
}
return new ResponseEntity<>(customResponse, HttpStatus.OK);
}catch(Exception ex) {
logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
ErrorDTO errors = new ErrorDTO();
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());

return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);
}
}

关于java - Dao实现中如何进行以下操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591783/

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