0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MapperクラスのSQLで不等号を使いたい(MyBatis)

Last updated at Posted at 2021-05-16

困ったこと

MapperのSQLをXMLに記載するとき、不等号(大なり、小なり など)を使用すると、
「要素のコンテンツは、整形式の文字データまたはマークアップから成るものでなければなりません。」
のエラーになってしまう。

解決方法

<![CDATA[]]>で囲むと良い.
以下の2つがあるが、1がおすすめ。

1. 該当箇所を囲む

.Java
  <select id="methodName" parameterType="EntityClass" resultType="Integer">
    SELECT 
        id
    FROM
        test
    WHERE
         <![CDATA[ 
         start_date <= NOW()
         and
         end_date > NOW() 
         ]]>
  </select>

2. SQL文全体を囲む

この方が可読性が良いが、動的SQLを記載しても反映されない。

.Java
  <select id="methodName" parameterType="EntityClass" resultType="Integer">
    <![CDATA[
    SELECT 
        id
    FROM
        test
    WHERE
         start_date <= NOW()
         and
         end_date > NOW())
    ]]>
  </select>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?