Mybatis预编译order by 语句无法生效

在mybatis的xml语句中 根据情况不同 采用不同的排序方式
        <if test="ordercolumn != null">
            ORDER BY #{ordercolumn} DESC
        </if>

但是没有生效,参考别人意见后,原来预编译时,将ordercolumn字段名转为字符串String格式,比如ordercolumn="name", sql语句是
ORDER BY “name” DESC

所以排序无法生效。

    解决办法:将xml按如下修改
        <choose>
            <when test="ordertype!=null and ordertype==1">
                ORDER BY carindex DESC
            </when>
            <when test="ordertype!=null and ordertype==2">
                ORDER BY statdate DESC
            </when>
            <otherwise>
                ORDER BY carindex DESC
            </otherwise>
        </choose>


传参数ordertype使用枚举形式,排序生效。
问题解决

猜你喜欢

转载自sunmit1024.iteye.com/blog/2390163