두번째 프로젝트 이야기

[mybatis] include refid(반복되는 쿼리 묶기!)

@ENFJ 2023. 10. 6. 13:22

refid(reference id )는 반복되는 쿼리를 미리 작성해 놓고 재활용 할 수 있게 해준다.

 

반복할 쿼리 선언: <sql id =" ">

선언한 쿼리 사용: <include refid="">

변수 사용방법:<property name="">

 

작성 예시

 

<mapper>

<sql id ="test_a">
	select *
    from table
</sql>


<sql id = "test_b">
	select *
    from table
    where ${param}
</sql>



<select id ="getListA" resultType="hashmap">
	<include refid="test_a"/>
    where filed=#{value}
</select>

<select id = "getListB" resultType="hashmap">
	<include refid = "test_b">
    <property name = "param1" value="value">
    </include>
    where filed = #{value}
</select>

</mapper>

 

getListA, getListB 의 출력 결과

SELECT * 
  FROM TABLE
 WHERE filed = #{value}