在Dao实施中如何进行以下操作?

时间:2019-06-14 05:17:23

标签: java hibernate spring-boot jpa

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

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

GoalPlanController

 @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);                

} 

GoalPlanRepository->

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)

1 个答案:

答案 0 :(得分:3)

首先,您需要创建两个类。

CustomResponse类

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);                 
    } 
}