mybatis实现多条件查询

本文介绍了一个使用Map参数进行复杂条件查询的RESTful API设计案例,详细展示了如何在Java后端服务中接收并解析前端传来的查询条件,包括页码、页面大小、用户ID等,并通过MyBatis动态SQL实现数据库查询。文章还提供了具体的代码实现和mapper.xml文件配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码中实现:

 /**
     * 根据各条件查询列表
     *
     * @return
     */
    @RequestMapping(value = "/pageListByCondition")
    public String pageListByCondition(@RequestBody Map map) {
        Integer pageNo =Integer.parseInt(map.get("pageNo").toString());
        Integer pageSize =Integer.parseInt(map.get("pageSize").toString());
        if(map.get("uiId")== null){
            map.put("uiId",sessionService.getCurrentUiId());
        }else {
            Integer uiId = Integer.parseInt(map.get("uiId").toString());
        }
        Integer iiId = map.get("iiId")== null?null:Integer.parseInt(map.get("iiId").toString());
         Integer oiType= map.get("iiId")== null?null:Integer.parseInt(map.get("oiType").toString());
        Integer oiStatus = map.get("oiStatus")== null?null:Integer.parseInt(map.get("oiStatus").toString());
        if(pageNo==null||pageSize==null){
            return ApiResultHelper.Failure(Constants.API_RESULT_PARAM_NOTNULL);
        }else {
            PageInfo pageInfo = orderInfoService.pageListByCondition(map);
            return ApiResultHelper.Success(pageInfo);
        }
    }

这样把所需要的参数通过map传入就可以了
mapper.xml中实现:

<select id="pageListByCondition" resultMap="BaseResultMap" parameterType="java.util.Map">
        select
        <include refid="Base_Column_List"/>
        from order_info
        <where>

            <if test="oiStatus != null">
                and oi_status = #{oiStatus,jdbcType=INTEGER}
            </if>
            <if test="iiId != null">
                and ii_id = #{iiId,jdbcType=INTEGER}
            </if>
            <if test="uiId != null">
                and ui_id = #{uiId,jdbcType=INTEGER}
            </if>
            <if test="oiType != null">
                and oi_type = #{oiType,jdbcType=INTEGER}
            </if>
            <if test="oiEmergency != null">
                and oi_emergency = #{oiEmergency,jdbcType=INTEGER}
            </if>
        </where>
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值