自己想的办法,比较麻烦。。
DAO中:
根据两个条件的四种情况(均不空、前空、后空、均空),写四组查List以及获取满足条件记录数的方法
// 根据页数和每页展示数查
@Select("select * from damage_assess_result where warhead_id=#{warheadId} and target_id=#{targetId} limit #{start} , #{end}")
public List<DamageAssessResult> findByWarheadAndTarget(String warheadId, String targetId, int start, int end);
// 查询到的条数
@Select("select count(*) from damage_assess_result where warhead_id=#{warheadId} and target_id=#{targetId}")
public int countFindByWarheadAndTarget(String warheadId, String targetId);
// 根据页数和每页展示数查
@Select("select * from damage_assess_result where warhead_id=#{warheadId} limit #{start} , #{end}")
public List<DamageAssessResult> findByWarhead(String warheadId, int start, int end);
// 查询到的条数
@Select("select count(*) from damage_assess_result where warhead_id=#{warheadId}")
public int countFindByWarhead(String warheadId);
// 根据页数和每页展示数查
@Select("select * from damage_assess_result where target_id=#{targetId} limit #{start} , #{end}")
public List<DamageAssessResult> findByTarget(String targetId, int start, int end);
// 查询到的条数
@Select("select count(*) from damage_assess_result where target_id=#{targetId}")
public int countFindByTarget(String targetId);
// 根据页数和每页展示数查
@Select("select * from damage_assess_result limit #{start} , #{end}")
public List<DamageAssessResult> find(int start, int end);
// 查询到的条数
@Select("select count(*) from damage_assess_result")
public int countFind();
Service的impl中:
根据俩条件的四种情况,分别调用不同的方法。
@Override
public JSONObject findByWarheadAndTarget(String warheadId, String targetId, int start, int end) {
List<DamageAssessResult> list=new ArrayList<>();
int total = 0;
if (warheadId!=null&&targetId!=null){
total = damageAssessResultDao.countFindByWarheadAndTarget(warheadId,targetId);
list = damageAssessResultDao.findByWarheadAndTarget(warheadId,targetId,start,end);
}
else if (warheadId==null&&targetId!=null) {
total = damageAssessResultDao.countFindByTarget(targetId);
list = damageAssessResultDao.findByTarget(targetId, start, end);
}
else if (warheadId!=null&&targetId==null) {
total = damageAssessResultDao.countFindByWarhead(warheadId);
list = damageAssessResultDao.findByWarhead(warheadId, start, end);
}
else {
total = damageAssessResultDao.countFind();
list = damageAssessResultDao.find(start, end);
}
List<JSONObject> jsonList = new ArrayList<>();
jsonObject.put("list",jsonList);
jsonObject.put("total",total);
return jsonObject;
}
List<DamageAssessResult> list=new ArrayList<>();
int total = 0;
if (warheadId!=null&&targetId!=null){
total = damageAssessResultDao.countFindByWarheadAndTarget(warheadId,targetId);
list = damageAssessResultDao.findByWarheadAndTarget(warheadId,targetId,start,end);
}
else if (warheadId==null&&targetId!=null) {
total = damageAssessResultDao.countFindByTarget(targetId);
list = damageAssessResultDao.findByTarget(targetId, start, end);
}
else if (warheadId!=null&&targetId==null) {
total = damageAssessResultDao.countFindByWarhead(warheadId);
list = damageAssessResultDao.findByWarhead(warheadId, start, end);
}
else {
total = damageAssessResultDao.countFind();
list = damageAssessResultDao.find(start, end);
}
List<JSONObject> jsonList = new ArrayList<>();
jsonObject.put("list",jsonList);
jsonObject.put("total",total);
return jsonObject;
Controller中:
@ApiOperation("根据战斗部id和目标id分页获取计算结果列表")
@GetMapping("resultList")
@ResponseBody
public CommonResult<JSONObject> resultList(@RequestParam(value = "warheadId",required = false) String warheadId,
@RequestParam(value = "targetId",required = false) String targetId,
@RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
int start = (pageNum - 1) * pageSize;
int end = pageSize;
JSONObject jsonObject = new JSONObject();
jsonObject=damageAssessResultService.findByWarheadAndTarget(warheadId,targetId,start,end);
return CommonResult.success(jsonObject);
}