자바로 애플리케이션 개발을 할 때 자주 사용하는 Mybatis와 CDATA에 대해 알아봅시다.

MyBatis란?

MyBatis는 SQL Mapper Framework 중 하나로, 자바 객체와 SQL문 사이의 자동 매핑을 지원합니다. 데이터베이스의 CRUD(Create, Read, Update, Delete) 작업을 간편하게 처리할 수 있도록 도와줍니다.

CDATA 태그란?

CDATA 태그는 XML 문서 안에 특수 문자를 포함시킬 때 사용됩니다. 특수 문자를 그대로 출력하게 해주는 역할을 합니다.

MyBatis에서 CDATA 태그 사용하기

MyBatis에서 SQL문을 작성할 때, 문자열 안에 특수 문자가 포함될 경우 문제가 발생할 수 있습니다. 이런 경우에 CDATA 태그를 사용하여 특수 문자를 그대로 출력할 수 있습니다.

예시

xmlCopy code

<select id="selectUserByName" parameterType="java.lang.String" resultType="User">
	SELECT * FROM users WHERE name = <![CDATA[#{name}]]> 
</select>


CDATA 태그를 사용하면 SQL문 안에서 작은 괄호(<, >)나 & 같은 특수 문자들이 자동으로 이스케이핑되지 않으므로, SQL문 작성 시 편리하게 사용할 수 있습니다.

MyBatis에서 CDATA 태그 사용 시 주의할 점

하지만 CDATA 태그를 사용할 때 주의할 점이 있습니다. 예를 들어, SQL문 안에 ]]> 문자열이 포함된 경우에는 CDATA 태그를 닫는 문자열과 혼동이 발생할 수 있습니다. 이런 경우에는 ]]>]]&gt;와 같이 문자열을 변경해 주어야 합니다.

공식문서

MyBatis 공식문서: https://mybatis.org/mybatis-3/ko/index.html

 

MyBatis – 마이바티스 3 | 소개

마이바티스는 무엇인가? 마이바티스는 개발자가 지정한 SQL, 저장프로시저 그리고 몇가지 고급 매핑을 지원하는 퍼시스턴스 프레임워크이다. 마이바티스는 JDBC로 처리하는 상당부분의 코드와

mybatis.org

XML 이스케이핑(MyBatis 공식문서): https://mybatis.org/mybatis-3/sqlmap-xml.html#XML_Escaping

 

mybatis – MyBatis 3 | Mapper XML Files

Mapper XML Files The true power of MyBatis is in the Mapped Statements. This is where the magic happens. For all of their power, the Mapper XML files are relatively simple. Certainly if you were to compare them to the equivalent JDBC code, you would immedi

mybatis.org

참고자료

MyBatis - SQL Mapper Framework: https://github.com/mybatis/mybatis-3

 

GitHub - mybatis/mybatis-3: MyBatis SQL mapper framework for Java

MyBatis SQL mapper framework for Java. Contribute to mybatis/mybatis-3 development by creating an account on GitHub.

github.com

CDATA Section (MDN Web Docs): https://developer.mozilla.org/ko/docs/Web/XML/CDATA

 

'IT > JAVA' 카테고리의 다른 글

[Java] Cannot construct instance of `class`  (1) 2023.11.18
[Java]Serializable 직렬화  (0) 2022.06.12

+ Recent posts