MyBatis defines SQL statements in XML files and uses them, but
<select id="hoge" resultType="hoge">
SELECT
*
FROM
HOGE
WHERE
HOGE.HOGE_DATE < SYSDATE
</select>
The WHERE clause that uses the inequality sign gets angry.
Cause: org.xml.sax.SAXParseException; lineNumber: XX; columnNumber: XX;The content of the element must consist of well-formed character data or markup.
The method of enclosing the part using the inequality sign with <![CDATA [...]]>.
<select id="hoge" resultType="hoge">
SELECT
*
FROM
HOGE
WHERE
HOGE_DATE <![CDATA[ < ]]> SYSDATE
</select>
It was okay to enclose the entire SELECT clause as in the example below. This one is more readable. (I feel.)
<select id="hoge" resultType="hoge">
<![CDATA[
SELECT
*
FROM
HOGE
WHERE
HOGE_DATE < SYSDATE
]]>
</select>
This is the method of using the entity reference provided in the comment. After a little research, it seems that there are the following 5 types.
letter | Entity reference |
---|---|
< | < |
> | > |
& | & |
'(Single quote) | ' |
"(Double quotation) | " |
Described using an entity reference, it looks like this:
<select id="hoge" resultType="hoge">
SELECT
*
FROM
HOGE
WHERE
HOGE_DATE < SYSDATE
</select>
I'm touching MyBatis for the first time, but it's convenient because I can use it just by copying and calling the SQL that has been confirmed to work. The official MyBatis document has been translated into Japanese, and the amount of information is abundant, so it is easy to use. Dynamic SQL such as if, choose, and foreach will be summarized at a later date.
MyBatis – MyBatis 3 | Mapper XML File Character and entity references [XML standard]
Recommended Posts