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

    总结在此,期望对以后有所帮助。
  • 相关阅读:
    消息队列rabbitmq/kafka
    centos下redis安全相关
    Ubuntu安装
    python 操作redis集群
    redis 发布订阅
    redis基础
    解决Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/cqupt/paging/dao/User.xml
    mongodb启动出现Failed to connect to 127.0.0.1:27017 after 5000ms milliseconds,giving up
    MongoDB的安装及安装为windows服务
    解决jsp表达式不能解析的问题
  • 原文地址:https://www.cnblogs.com/lendar/p/10156546.html
Copyright © 2011-2022 走看看