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 >0 and questionType <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中转义符 < > 在mybaitis中转义符 > <= 在mybaitis中转义符 <=