美文网首页
MYBATIS中 if-test 判断大坑

MYBATIS中 if-test 判断大坑

作者: 音乐与咖啡Bean | 来源:发表于2018-07-12 14:49 被阅读0次

    【<if test="takeWay == '0'">】mybatis的if判断

    单个的字符要写到双引号里面才行,改为<if test='takeWay == "1"'>或者改为<if test="takeWay == '1'.toString() ">

    .xml文件的部分代码

    <insert id="insertDelivery" parameterType="com.zuci.request.DeliveryPreferenceReq" >

          insert cx_customer_deliverypreference

          <trim prefix="(" suffix=")" suffixOverrides="," >

                .... 此处省略

            <if test="takeWay == '1' and workday != null ">

              WORKDAY,

            </if>

            ....

          </trim>

          <trim prefix="values (" suffix=")" suffixOverrides="," >

              .... 此处省略

              <if test="takeWay == '1' and workday != null ">

                #{workday, jdbcType=VARCHAR},

          </if>

          ....

          </trim> 

      </insert>

    takeWay == “1”处出错,导致不执行if判断中的sql,运行程序不报错,没有任何提示。去掉takeWay == “1” and 则可执行。对此我百思不得其解,

    因为自己有写过如下代码,是没错的。

    <if test="messageType == 'senderReceiveSuccess' ">

          ......

    </if>

    把<if test="takeWay == '1' and workday != null ">

    改为<if test='takeWay == "1" and workday != null '>

    或改为<if test="takeWay == '1'.toString() and workday != null ">即可。

    原因是:mybatis是用OGNL表达式来解析的,在OGNL的表达式中,’1’会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析。

    总结下使用方法:单个的字符要写到双引号里面或者使用.toString()才行!

    相关文章

      网友评论

          本文标题:MYBATIS中 if-test 判断大坑

          本文链接:https://www.haomeiwen.com/subject/smqrpftx.html