今天遇到这么个问题,用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;大家一起交流,一同进步