Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mybatis mapper file escape characters

Tags:

mybatis

In a mybatis mapper file for a sql select statement I am unable to use special characters (<=) in where expressions. For Example (a simplified select):

<select id="selectMonday" resultType="SheetGameRec">
    select ColumnName
    from Table
    where ColumnName <= 2
    order by ColumnName;
</select>

The following error is generated

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in Mapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper 

Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: xx; columnNumber: xx; The content of elements must consist of well-formed character data or markup.

If I replace the <= with >= or =, the mapper file will work though this is not the select that I want.

How do I escape out these special characters. I have had trouble with other expressions like & as well. I am using mybatis 3.0.2.

Thanks.

like image 927
glaird Avatar asked Aug 31 '25 10:08

glaird


1 Answers

You can use CDATA to escape special characters.

<select id="selectMonday" resultType="SheetGameRec">
    select ColumnName
    from Table
    where ColumnName <![CDATA[ <= 2 ]]>
    order by ColumnName;
</select>
like image 146
Karthik Prasad Avatar answered Sep 03 '25 18:09

Karthik Prasad