Java Mybatis:选择注释中的“小于”问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29092950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:20:57  来源:igfitidea点击:

Mybatis : "less than" issue in Select annotations

javaspringmybatis

提问by Om.

I am using java7, spring 3 and mybatis

我正在使用 java7、spring 3 和 mybatis

Pom.xml

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>

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.

在使用基于注释的 Select 时,我遇到了一个奇怪的问题,下面提到的代码由于使用 < (Less then) 而抛出异常 > (greater then) 按预期工作。

<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

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

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

可以通过将 < (less then) 替换&lt;为如下所示来解决上述问题。

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

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

我还遇到了在我尚未尝试的受人尊敬的场景中使用 CDATA 或 ^ 的建议。

Question:

题:

  • 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 特定转换),或者这种行为是否符合预期,因为我们正在使用<script>标签?
  • 有没有我错过的替代解决方案?

采纳答案by Landys

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

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

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

如果您不想解析诸如<和 之类的字符&,则可以使用该术语CDATA来阻止 XML 解析器解析此类文本。详情请参考http://www.w3schools.com/xml/xml_cdata.asp。或者你可以用&lt注释来逃避它。

I.E.

IE

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

回答by Abrahan Gonzalez

you too can use that.

你也可以使用它。

<= less or equal SELECT * FROM STUDENT WHERE DEPARTMENT_ID = #{depId}

<= 小于或等于 SELECT * FROM STUDENT WHERE DEPARTMENT_ID = #{depId}

= greater that

= 大于

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