Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mybatis : "less than" issue in Select annotations

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>

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>

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

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 ?
like image 348
Om. Avatar asked Mar 17 '15 06:03

Om.


People also ask

Is MyBatis faster than JDBC?

For an easiest query. Not considering the first query, for the following queries, JDBC always takes about 6ms~10ms, and MyBatis takes about 31~35ms, which is about 3 times. For a more complex query (6 inner joins and an order by in it), JDBC only takes 25~30ms, while MyBatis needs 80~100ms.

How do I configure MyBatis?

mappers tag Mapper XML file is the important file, which contains the mapped SQL statements. Mapper's element is used to configure the location of these mappers xml files in the configuration file of MyBatis (this element contains four attributes namely resources, url, class, and name).

What is the use of MyBatis?

MyBatis is an open source persistence framework which simplifies the implementation of database access in Java applications. It provides the support for custom SQL, stored procedures and different types of mapping relations. Simply put, it's an alternative to JDBC and Hibernate.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.


1 Answers

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

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.

I.E.

<script>
SELECT * FROM STUDENT
WHERE DEPARTMENT_ID = #{depId}
<if test='joiningDate != null'> 
<![CDATA[
AND STUDENT_ID <= #{joiningDate} 
]]>
</if>
</script>
like image 182
Landys Avatar answered Oct 18 '22 15:10

Landys