JAVA后端根据两个条件查询获得List,给前端分页展示,且两个条件均可能为空的处理

自己想的办法,比较麻烦。。

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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值