zoukankan      html  css  js  c++  java
  • mybitis下choose..when. otherwise条件不起作用

    我的代码如下:

          <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,问题解决。

    总结在此,期望对以后有所帮助。
  • 相关阅读:
    PHP延迟静态绑定
    PHP SPL神器实现堆排序
    魔术方法__sleep 和 __wakeup
    SPL 笔记
    PHP中对象的深拷贝与浅拷贝
    PHP的垃圾回收机制详解
    深入理解PHP中赋值与引用
    xdebug安装及使用小结
    工作中碰到的一个问题(cookie相关)
    分享一个解析XML成为php数组的方法
  • 原文地址:https://www.cnblogs.com/lendar/p/10156546.html
Copyright © 2011-2022 走看看