选择注释中的问题

选择注释中的问题

本文介绍了Mybatis:“小于"选择注释中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java7,spring 3和mybatis

I am using java7, spring 3 and mybatis

Pom.xml

<org.mybatis-version>3.2.8</org.mybatis-version>
<org.mybatis-spring-version>1.2.2</org.mybatis-spring-version>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>${org.mybatis-version}</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>${org.mybatis-spring-version}</version>
</dependency>

在使用基于注释的Select时,遇到一个奇怪的问题,由于使用< (然后减少),而>(然后更大)将按预期工作.

While using annotation based Select i came across with strange issue where below mentioned code was throwing exception due to use of < (Less then) while > (greater then) works as expected.

<script>
SELECT * FROM STUDENT
WHERE DEPARTMENT_ID = #{depId}
<if test='joiningDate != null'> AND STUDENT_ID <= #{joiningDate} </if>
</script>

谷歌搜索一段时间后,我发现下面报告了这个问题.

After googling for a while i find out this issue reported below.

https://code.google.com/p/mybatis/issues/detail?id = 787

可以通过替换<来解决上述问题. (少于此),如下所示.

Above issue can be fixed by replacing < (less then) with &lt; as shown below.

<script>
SELECT * FROM STUDENT
WHERE DEPARTMENT_ID = #{depId}
<if test='joiningDate != null'> AND STUDENT_ID &lt;= #{joiningDate} </if>
</script>

我还遇到了在尚未尝试使用的受尊敬的情况下使用CDATA或^的建议.

I have also came across of suggestion for using using CDATA or ^ in respected scenarios which i haven't given try yet.

问题:

  • 我的问题是Mybatis团队不应该解决此问题(对于频繁使用的查询标签至少进行xml特定转换),或者由于我们使用的是<script>标签?
  • 是否有我错过的替代解决方案?
  • My question is shouldn't this issue be fixed by Mybatis team (at-least xml specific conversion for frequently used query tags) or this behaviour is as expected since we are using <script> tag ??
  • Is there any alternate solution which i have missed out ?

推荐答案

公平地说,这不是MyBatis中的问题,而是XML解析的行为.

To be fair, it's not the issue in MyBatis, but the behaviour of XML parsing.

如果您不想解析诸如<&之类的字符,则可以使用术语CDATA来防止XML解析器解析此类文本.有关详细信息,请参考 http://www.w3schools.com/xml/xml_cdata.asp .或者,您也可以使用&lt作为注释对其进行转义.

If you don't want characters such as < and & to be parsed, you can use the term CDATA to prevent the XML parser to parse such text. Please refer to http://www.w3schools.com/xml/xml_cdata.asp for detail. Or you may escape it with &lt as comments.

IE.

<script>
SELECT * FROM STUDENT
WHERE DEPARTMENT_ID = #{depId}
<if test='joiningDate != null'>
<![CDATA[
AND STUDENT_ID <= #{joiningDate}
]]>
</if>
</script>

这篇关于Mybatis:“小于"选择注释中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:23