mybatis动态传参order by 失效的问题

今天遇到这么个问题,用easyui的romoteSort进行单个字段的全局排序

在sql上拼接order by  #{sort:VARCHAR}   #{order:VARCHAR}

传参没问题,sql能正常执行,debug返回列表很明显没有排序啊

一步一步排除,确定问题出在sql

对于这种#{sort:VARCHAR} 的变量,Mybatis会将其视为字符,给变量值加上引号

假如传入参数是count,desc  就是order by 'count' 'desc'

这个结果在日志里也发现不了什么问题,也能正常执行只是过滤了order by

找了好久发现这个问题

对于${sort}的变量,Mybatis会将其视作直接变量,不会再给其加上引号

假如传入参数是count,desc  就是order by count desc

这就是想要的结果。

mybatis语句:

特别注意:#和$用法的不同之处


    <select id="getDynamicCitys" parameterType="map" resultMap="map">
        select <include refid="param"/>
        from city as c
        <where>
            <if test="id != null and id != '' ">
                c.ID LIKE concat(concat('%',#{id} ),'%')
            </if>
            <if test="name!=null and name !='' ">
                and  c.Name LIKE concat(concat('%',#{name} ),'%')
            </if>
            <if test="countryCode!=null and countryCode != '' ">
                and  c.CountryCode LIKE concat(concat('%',#{countryCode} ),'%')
            </if>
            <if test="district!=null and district != '' ">
                and  c.District LIKE concat(concat('%',#{district} ),'%')
            </if>
            <if test="population!=null and population != '' ">
                and  c.Population LIKE concat(concat('%',#{population} ),'%')
            </if>
        </where>
        order by ${sidx} ${sord}
    </select>

 

 

失败的sql

select c.ID id,c.Name name,c.CountryCode countryCode, c.District district, c.Population population, status,sex,birthday,createTime
 FROM city as c
 WHERE c.ID LIKE concat(concat('%','1' ),'%') and c.Name LIKE concat(concat('%','Ka' ),'%') and c.CountryCode LIKE concat(concat('%','FG' ),'%') and c.District LIKE concat(concat('%','bo' ),'%') order by 'id' 'desc'
 LIMIT 20;

成功的sql

select c.ID id,c.Name name,c.CountryCode countryCode, c.District district, c.Population population, status,sex,birthday,createTime
 FROM city as c order by id desc
 LIMIT 20;

如有不解,请加java爱好群大家交流:852665736;大家一起交流,一同进步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值