我的代码如下:
<select id="findList" resultType="TyArticle">
SELECT
<include refid="tyArticleColumns"/>
FROM ty_article a
<include refid="tyArticleJoins"/>
<where>
a.del_flag = #{DEL_FLAG_NORMAL}
<if test="enVersion != null and enVersion != ''">
AND a.en_version = #{enVersion}
</if>
<if test="category != null and category != ''">
AND a.category = #{category}
</if>
<if test="top != null and top != ''">
AND a.top = #{top}
</if>
<if test="title != null and title != ''">
AND a.title LIKE
<if test="dbName == 'oracle'">'%'||#{title}||'%'</if>
<if test="dbName == 'mssql'">'%'+#{title}+'%'</if>
<if test="dbName == 'mysql'">concat('%',#{title},'%')</if>
</if>
</where>
<choose>
<when test="enVersion != null and enVersion =='0'">
order by a.weight desc ,str_to_date(a.publish_date, '%Y年%m月%d日') desc
</when>
<otherwise>
order by a.weight desc ,str_to_date(a.publish_date, '%d %M %Y') desc
</otherwise>
</choose>
</select>
choose...when...otherwise语法,when中的条件不起作用,不管条件是不是0,都取otherwise中的结果。
首先,仔细检查语法格式,感觉没有问题啊,怎么就不起作用呢?
其次,有检查调用sql时传递的参数enVersion也是String类型,所以书写也没问题啊。
最后,检查数据中enVersion的数据类型,发现是char(1),于是把代码条件换成这样,再测试,
<choose>
<when test="enVersion != null and enVersion == 0 ">
order by a.weight desc ,str_to_date(a.publish_date, '%Y年%m月%d日') desc
</when>
<otherwise>
order by a.weight desc ,str_to_date(a.publish_date, '%d %M %Y') desc
</otherwise>
</choose>
结果,成功了。原来数据再判断参数类型时,还跟参数在表中对应的数据类型有关。在参数比较的过程直接转成了int型,所以把
enVersion =='0', 改成 enVersion == 0,问题解决。
总结在此,期望对以后有所帮助。