MyBatis中大于号以及小于号的表达方式

x33g5p2x  于2022-01-04 转载在 其他  
字(3.1k)|赞(0)|评价(0)|浏览(256)

mybatis使用的是xml格式的文件。使用>和<号的时候,会存在与xml的标签的规范冲突。

1.场景还原

在实际项目中,有很多需求需要通过设定一个具体的时间段来搜索或过滤所需的数据,今天笔者就mybatis中时间比较涉及到的大于,小于号的应用方法作个详尽的讲解。

2.实现方案

以下介绍两种可行方法:

①转义法

大于:>

小于:<

大于等于:>=

小于等于:<=

笔者案例:

  1. <select id="view" parameterType="map" resultMap="BaseResultMap">
  2. SELECT * FROM task t,staff s,product p WHERE t.staff_id = s.id AND t.product_id = p.id
  3. <if test="companyId != null ">
  4. AND t.company_id = #{companyId}
  5. </if>
  6. <if test="workshopId != null">
  7. AND t.workshop_id = #{workshopId}
  8. </if>
  9. <if test="opunitId != null">
  10. AND t.opunit_id = #{opunitshopId}
  11. </if>
  12. <if test="processId != null">
  13. AND t.process_id = #{processId}
  14. </if>
  15. <if test="@Ognl@isNotEmpty(equipmentId)">
  16. AND t.equipment_id = #{equipmentId}
  17. </if>
  18. <if test="dateStart != null and dateStart !='' ">
  19. AND UNIX_TIMESTAMP(t.date_work) &gt;= UNIX_TIMESTAMP(#{dateStart})
  20. </if>
  21. <if test="dateEnd != null and dateEnd !='' ">
  22. AND UNIX_TIMESTAMP(t.date_work) &lt;= UNIX_TIMESTAMP(#{dateEnd})
  23. </if>
  24. GROUP BY t.order_no
  25. ORDER BY t.date_work
  26. </select>

运行效果:

注意:这里的 日期入参类型为String

②<![CDATA[ sql语句 ]]>

<![CDATA[ sql语句 ]]>中的<![CDATA[ ]]>在mybatis中自动注释

笔者案例:

  1. <select id="selectByTime" resultType="Date" parameterType="map">
  2. SELECT
  3. r.stop_time
  4. FROM
  5. rtg r <![CDATA[ WHERE UNIX_TIMESTAMP(r.stop_time) >= UNIX_TIMESTAMP(#{startTime}) AND UNIX_TIMESTAMP(r.stop_time) <= UNIX_TIMESTAMP(#{endTime}) ]]>
  6. </select>

运行效果:

一、简要概述

  1. 平时在mybatis的映射文件写sql时,很多时候都需要写一些特殊的字符。例如:"<" 字符 “>” 字符 “>=” 字符 “<=” 字符,但是在xml文件中并不能直接写上述列举的字符,否则就会报错。
  2. 因为在解析xml文件时候,我们如果书写了特殊字符,在没有特殊处理的情况下。这些字符会被转义,但我们并不希望它被转义,所以我们要使用<![CDATA[ ]]>来解决。
  3. 那为什么要这样书写呢?<![CDATA[ ]]> ,不言而喻:这是XML语法。在CDATA内部的所有内容都会被解析器忽略。
  4. 所以,当我们在xml文本中包含了很多的"<" 字符 “<=” 和 “&” 字符—就像程序代码一样,那么最好把他们都放到CDATA部件中。

二、实际书写规范

  1. 有个问题需要注意的就是在我们的mybatis的映射文件中,以下 等这些标签都不会被解析,所以我们只把有特殊字符的语句放在 <![CDATA[ ]]>中,尽量缩小 <![CDATA[ ]]> 的范围。
  2. 案例
  1. SELECT * FROM (SELECT t.*, rownum FROM bst_busi_msg t
  2. <where>
  3. <if test="targetId != null">
  4. and (t.busi_sys_order = #{targetId,jdbcType=VARCHAR}
  5. or t.busi_intf_seq = #{targetId,jdbcType=VARCHAR}
  6. )
  7. </if>
  8. <if test="targetId == null and shardingTotal > 0">
  9. and (t.task_status = '0'
  10. OR (t.task_status = '3'
  11. AND t.task_count <![CDATA[ < ]]> ${@com.asiainfo.bst.common.Constant@max_handle_count()}
  12. )
  13. )
  14. and MOD(t.msg_id,#{shardingTotal,jdbcType=NUMERIC}) = #{shardingIndex,jdbcType=NUMERIC}
  15. </if>
  16. </where>
  17. ORDER BY t.msg_id ASC
  18. ) WHERE rownum <![CDATA[ <= ]]> #{rownum,jdbcType=NUMERIC}

[说明]
因为这里有 ">" "<=" 特殊字符所以要使用 来注释,但是有标签,所以把等放外面

mybatis中写sql语句时需要转义的字符

mybatis配置文件,sql语句中含有转义字符:

错误语句:

  1. select * from table_base where flag_topic & #{topic_num}

错误信息:

  1. Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 54; The entity name must immediately follow the '&' in the entity reference.

正确语句:

  1. select * from table_base where flag_topic &amp; #{topic_num}

将语句中的位运算(与)”&“符使用“&”替换

mybatis配置文件写SQL语句的某些字符需要转义:

  1. &lt; <
  2. &gt; >
  3. &lt;&gt; <>
  4. &amp; &
  5. &apos; '
  6. &quot; "

注意:要加上分号!

ok,以上全是笔者实际需求提炼的心得,望能够帮助更多的伙伴

相关文章