mybatis动态sql中test判断Boolean

mybatis中动态sql使用if test判断String,pojo一般写法如下(sql片段):

String

<if test="countryArea != null and countryArea != ''">
    AND country_area=#{countryArea}
</if>

pojo

<if test="map.keyWord != null and map.keyWord != ''">
    AND s.subject LIKE concat('%',#{map.keyWord},'%')
</if>
但是如果是Boolean类型,如果写成如下方式,会出现false无效的bug,一直执行的都是true
<update id="updateHaveNewComment">
        <choose>
            <when test="flag=true">
                UPDATE epc_subject_center s
                SET s.have_new_comment=1
                WHERE s.id=#{id}
            </when>
            <otherwise>
                UPDATE epc_subject_center s
                SET s.have_new_comment=0
                WHERE s.id=#{id}
            </otherwise>
        </choose>
    </update>
网上有一些资料说只用判断是否为null就可以了,个人检验是错误的。下面的也是错误的
<update id="updateHaveNewComment">
        <choose>
            <when test="flag !=null">
                UPDATE epc_subject_center s
                SET s.have_new_comment=1
                WHERE s.id=#{id}
            </when>
            <otherwise>
                UPDATE epc_subject_center s
                SET s.have_new_comment=0
                WHERE s.id=#{id}
            </otherwise>
        </choose>
    </update>

正确的写法应该是

<update id="updateHaveNewComment">
        <choose>
            <when test="flag">
                UPDATE epc_subject_center s
                SET s.have_new_comment=1
                WHERE s.id=#{id}
            </when>
            <otherwise>
                UPDATE epc_subject_center s
                SET s.have_new_comment=0
                WHERE s.id=#{id}
            </otherwise>
        </choose>
    </update>

或者

<update id="updateHaveNewComment">
        <choose>
            <when test="flag==true">
                UPDATE epc_subject_center s
                SET s.have_new_comment=1
                WHERE s.id=#{id}
            </when>
            <otherwise>
                UPDATE epc_subject_center s
                SET s.have_new_comment=0
                WHERE s.id=#{id}
            </otherwise>
        </choose>
    </update>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 上边使...
    哇哈哈E阅读 3,336评论 0 38
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,694评论 0 4
  • 田间地头割稻机, 农家庭院绞米器。 秋收在望笑开颜, 时复余闲享安逸。 闻到什么味道能够让你想到小时候? 老师身上...
    逆向既视感阅读 663评论 0 1
  • 为了养好宝宝,新手妈妈很迷茫,四处打听前辈妈妈的经验分享,但却一不小心误入了各种歧途。那些“老人言”、“常见到”、...
    复明的瞎子阅读 205评论 0 0