mybatis 动态查询 参数类型判断

后端java

public class  DataScope{
       public String beginTime ;
       public String endTime ;
       public DataScope(String beginTime,String endTime){
           this.beginTime = beginTime ;
           this.endTime = endTime;
       }
    }
    Object val = map.get(s);
                    if(val instanceof  JSONArray){
                        //日期between
                        JSONArray jval = (JSONArray)val ;
                        DataScope dataScope = new DataScope((String)jval.get(0),(String)jval.get(1));
                        List plist = new ArrayList();
                        plist.add(dataScope);
                        queryParams.put(s,plist);

                    }else{
                        queryParams.put(s, map.get(s));
                     }

mybatis mapping配置

 <
 select id="getListByPage" resultType="Map">
        SELECT ${fields} FROM `${tableName}`
        <choose>
            <when test="params!= null and params.size>0">
                WHERE
                <foreach collection="params.keys" item="key" open="(" close=")" separator="and">
                    <choose>
                        <when test="params[key]  instanceof java.util.List and params[key] !=null and params[key].size > 0 ">
                            <foreach collection="params[key]" index="index" item="item" open="(" close=")" separator="and">
                                ${key}  between #{item.beginTime} and #{item.endTime}
                            </foreach>
                        </when>
                        <when test="params[key].indexOf('*')>-1">
                            ${key} LIKE REPLACE(#{params[${key}]},"*","%")
                        </when>
                        <otherwise>
                            ${key} = #{params[${key}]}
                        </otherwise>
                    </choose>
                </foreach>
                <if test="superQueryMap != null and superQueryMap.size>0 ">
                    and
                    <choose>
                    <when test="matchType=='AND'">
                        <foreach collection="superQueryMap" item="value" open="(" close=")" separator="and">
                        <choose>
                            <when test="value.ruleValue=='IN'.toString()">
                                ${value.field} ${value.ruleValue}
                                <foreach item="iv" collection="value.val.split(',')" open="(" separator="," close=")">
                                    #{iv}
                                </foreach>
                            </when>
                            <otherwise>
                                ${value.field} ${value.ruleValue} #{value.val}
                            </otherwise>
                        </choose>
                    </foreach>
                    </when>
                        <otherwise>
                            <foreach collection="superQueryMap" item="value" open="(" close=")" separator="or">
                                <choose>
                                    <when test="value.ruleValue=='IN'.toString()">
                                        ${value.field} ${value.ruleValue}
                                        <foreach item="iv" collection="value.val.split(',')" open="(" separator="," close=")">
                                            #{iv}
                                        </foreach>
                                    </when>
                                    <otherwise>
                                        ${value.field} ${value.ruleValue} #{value.val}
                                    </otherwise>
                                </choose>
                            </foreach>
                        </otherwise>
                    </choose>
                </if>
            </when>
            <otherwise>
                <if test="superQueryMap != null and superQueryMap.size>0 ">
                    where
                    <choose>
                    <when test="matchType=='AND'">
                        <foreach collection="superQueryMap" item="value" open="(" close=")" separator="and">
                        <choose>
                            <when test="value.ruleValue=='IN'.toString()">
                                ${value.field} ${value.ruleValue}
                                <foreach item="iv" collection="value.val.split(',')" open="(" separator="," close=")">
                                    #{iv}
                                </foreach>
                            </when>
                            <otherwise>
                                ${value.field} ${value.ruleValue} #{value.val}
                            </otherwise>
                        </choose>
                    </foreach>
                    </when>
                        <otherwise>
                            <foreach collection="superQueryMap" item="value" open="(" close=")" separator="or">
                                <choose>
                                    <when test="value.ruleValue=='IN'.toString()">
                                        ${value.field} ${value.ruleValue}
                                        <foreach item="iv" collection="value.val.split(',')" open="(" separator="," close=")">
                                            #{iv}
                                        </foreach>
                                    </when>
                                    <otherwise>
                                        ${value.field} ${value.ruleValue} #{value.val}
                                    </otherwise>
                                </choose>
                            </foreach>
                        </otherwise>
                    </choose>
                </if>
            </otherwise>
        </choose>

        <if test="orderBy!= null">
            order by ${orderBy}
        </if>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值