mybatis的 choose -- when test -- otherwise 标签和 if test 标签
·
在公司的开发中,遇到使用<when test="banktype == '2' ">的情况,但发现无论如何都不会走进去,后来测试了去掉空格:
<when test="banktype == '2'">
<when test="banktype=='2'">
这两种也不可以;后来搜到了网上有这种需要使用单引号包双引号的写法才正确,
写法为:<when test='banktype == "2"'>
原博客:
1.choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。
当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。例如下面例子,同样把所有可以限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">
SELECT *
FROM User u
<where>
<choose>
<when test="username !=null ">
u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')
</when >
<when test="sex != null and sex != '' ">
AND u.sex = #{sex, jdbcType=INTEGER}
</when >
<when test="birthday != null ">
AND u.birthday = #{birthday, jdbcType=DATE}
</when >
<otherwise>
</otherwise>
</choose>
</where>
</select>
2.if-test 不会跳出判断语句
2.1 if-test标签判断语法:
@see http://blog.csdn.net/z69183787/article/details/51589171
用==判断时应写成
<if test='type=="y"'>
and status = 0
</if>
而不是
<if test="type=='y'">
and status = 0
</if>
是使用的OGNL表达式来进行解析的,在OGNL的表达式中,’y’会被解析成字符,因为java是强类型的,char 和 一个string 会导致不等。所以if标签中的sql不会被解析。具体的请参照 OGNL 表达式的语法
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)