mybatis sql, include 중복 쿼리가 있을 때 사용하는 문법!




재사용문법


<sql>, <include>



<sql>



SQL
<sql id ="hyehwi">
  WHERE category = #{category}
</sql>



<include>



SQL
/* 구문을 사용한 경우 */
<SELECT id="selectHyehwi" resultType="UserVO">
  SELECT *
    FROM TBL_USER
    <include refid="hyehwi"/>
</SELECT>



SQL
/* 구문을 사용하지 않은 경우 */
<SELECT id="selectHyehwi" resultType="UserVO">
  SELECT *
    FROM TBL_USER
   WHERE category = #{category}
</SELECT>



예시


SQL
<sql id="check">
  <if test="searchCheck != null && searchCheck != ''">
     AND USER_ID = #{searchCheck}
      OR USER_NAME = #{searchCheck}
  </if>
  <if test="searchCheck != null && searchCheck != '' && searchCategory == 1">
     AND USER_ID = #{searchCheck}
  </if>
  <if test="searchCheck != null && searchCheck != '' && searchCategory == 2">
     AND USER_NAME = #{searchCheck}
  </if>
</sql>

<SELECT id="user" resultType="userVO">
  SELECT *
    FROM TBL_USER
    <where>
      <include refid="check">
      AND USER_USE = 'Y'
    </where>
</SELECT>




https://mybatis.org/mybatis-3/ko/sqlmap-xml.html