mybatis if..else

1.mybatis中if-else的语法

<choose>
    <when test="">
        //...
    </when>
    <otherwise>
        //...
    </otherwise>
</choose>

2举例:

/**
* 需求: 1 当选择问答标签,即不为空时, 可实现拖动排序,且按时间顺序排序
* 	   2 若没有选择问答标签,则按时间倒叙排序
* / 
<select id="getQuestionAnswerList" resultType="QuestionAnswerDto">
        SELECT id, question_type AS questionType, question, answer, picture_url AS pictureUrl, page_view AS pageView,
        resolved_count AS resolvedCount, unresolved_count AS unresolvedCount, priority, enable, created, modified
        FROM t_question_answer
        WHERE enable = 1
        <choose>
            <when test="questionType != null and questionType &gt;0 and questionType &lt;100" >
                  AND question_type = #{questionType}
                  ORDER BY priority ASC, created ASC
            </when>
            <otherwise>
                ORDER BY created DESC
            </otherwise>
        </choose>
        <if test="start >= 0 and size >0 ">
            limit #{start}, #{size}
        </if>

</select>

(1) 之前, 在sql中显示如下图错误
在这里插入图片描述
(2)原因

	在if-else 比较时, 用了> 和 <, 但是在mybatis中不能识别小于号<,所以报错
	在mybatis中最好用转义符来表示

(3) 在mybatis中的转义符

	<         在mybaitis中转义符           &lt;
	>         在mybaitis中转义符           &gt;
	<=        在mybaitis中转义符           &lt;=
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值