【发布时间】:2021-03-10 01:23:09
【问题描述】:
我创建了一个 API,它可以让我根据位置进行计数(我在此使用本机查询),这是我的服务类中的方法
public ResponseModel getData(RequestModel mRequestModel) {
JSONObject mJsonObject = new JSONObject(mRequestModel.getData());
String startDAte = mJsonObject.getString("StartDate");
// String endDate = mJsonObject.getString("EndDate");
List<ReportModel1> ispeeddetails = ispeedVio.getDataIspeed(startDAte);
List<ReportModel1> rlvdDetails = rlvdVio.getDataRlvd(startDAte);
List<ReportModel1> challanDetails = challanRepo.getChallan(startDAte);
HashMap<String,Webchallanservice> violation = new HashMap<String,Webchallanservice>();
for(ReportModel1 m:ispeeddetails) {
String location= m.getLocation();
if(violation.containsKey(location)) {
violation.get(location).setIspped_count(m.getCount());
}
else {
Webchallanservice challn = new Webchallanservice();
challn.setIspped_count(m.getCount());
violation.put(location, challn);
}
}
if (violation != null && !violation.isEmpty()) {
return Util.setResponse(AppConstant.SUCCESS_STATUS_CODE, "Successfully Fetched", violation);
} else {
return Util.setResponse(AppConstant.ERROR_STATUS_CODE, "No Record Found", violation);
}
我使用的查询是这样的
@Query(value = "SELECT COUNT(*) as count ,location FROM ispeed_transits v WHERE v.entry_timestamp LIKE %:startDate% GROUP BY v.location", nativeQuery = true)
List<ReportModel1> getDataIspeed(@Param("startDate") String startDate);
我在邮递员上得到的响应是正确的,但是当没有数据时它给我 null 但我想返回 0。
"status": 1,
"message": "Successfully Fetched",
"myObjectList": {
"Taat Mill- From Jhakarkati": {
"challan_count": 3,
"ispped_count": null,
"rlvd_count": 254
},
【问题讨论】:
-
ispped_count的数据类型是什么?是int还是整数? -
数据类型为BigInteger
-
在Webchallanservice类本身的构造函数中将
ispped_count的值初始化为0怎么样?或者在返回if-else之前,可以加if(violation.get(location).getIspped_count()==null){violation.get(location).setIspped_count(0);} -
好的,先生,我试试
-
我尝试了两种方式,但两种方式都无法初始化 0,因为它显示的 Int 值 0 不能适用于 BigInteger
标签: java mysql spring spring-boot jpa