基于Spring Boot进行多条件筛选的实现

筛选按钮需要进行多个条件的选择,将参数传入后端,并得到筛选结果。

后端接口进行数据处理

@PostMapping(value = "/getResult")
public HttpResult getResult(HttpServletRequest request){
    //数据类型
    HttpResult result = new HttpResult();
    Map<String,String[]> paramMap = request.getParameterMap();
    //当传入的参数为空时确保也能够执行
    if(paramMap.size()>=0){
        Map<String,String> condtionMap = new HashMap();
        for(String key: paramMap.keySet()){
            condtionMap.put(key,paramMap.get(key)[0]);
        }
        try{
            List<JobDO> sjob =service.selectJob(condtionMap);
            return HttpResult.build(sjob);
        }catch (Exception e){
            result = HttpResult.build(false,"获取结果失败");
            return result;
        }
    }else{
        result = HttpResult.build(false,"查询结果失败");
        return result;
    }
}

DAO层进行数据筛选,动态sql

Interger类型的参数为0时的查询结果异常,Mapper层sql语句条件查询时status为Interger类型时,当status为0时,查询结果为所有的情况,因为status为Interger类型,所以为0时,默认为null。

解决方法:多加一条判断即可

"<if test=\"status==0 ? '0':status \"> and status= #{status} </if>"

@Results(id = "job",value = {
        @Result(column = "id", property = "id"),
        @Result(column = "name", property = "name"),
        @Result(column = "state", property = "state")
})

@ResultMap(value = "job")
@Select("<script>" +
        "select * from table" +
        "<where>" +
        "<if test=\"conditionMap.id !=null  and conditionMap.id!=''\"> id=#{conditionMap.id}</if>" +
        //字段的类型为封装类型 值为0时,会转为空
        "<if test=\"conditionMap.state == 0\">and state=0 </if> "+
        "<if test=\"conditionMap.state !=null and conditionMap.state!=''\">and state=#{conditionMap.state}</if>" +
        "</where>" +
        "</script>"
)
List<JobDO> selectJob(@Param("conditionMap") Map<String,String> conditionMap);

 react前端使用request调用接口

//筛选按钮
    doFilter = ()=>{
        //配置路径,微服务名称 ,接口参数
        request.post(`/api/jobservice/job/getResult`)
            .set('Content-Type', 'application/x-www-form-urlencoded')
            .send({
                //参数是否传入
                id:this.state.Id ? this.state.Id :undefined,
                //状态为开启,关闭,全部,全部情况下为undefined
                states : (this.state.choseState && this.state.choseState != 2  ? this.state.choseState : undefined)
            })
            .end((err,res)=>{
                if(!err){
                    let response=JSON.parse(res.xhr.response);
                    if(response.code='200'){
                        //对取得的值倒序
                        this.setState({list:response.data.reverse()})
                    }else{
                        message.error(response.msg);
                    }
                }else{
                    message.error("服务暂不可用");
                }
                this.setState({ afterFiltered: true });
            });
    }
发布了36 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_27182767/article/details/89647917