본문 바로가기
🌈 백엔드/스프링 프레임워크

MyBatis 동적 태그 + 검색

by 개발자 알마 2023. 12. 29.
반응형

 

[1] MyBatis 동적 태그


(1) if : 특정한 조건이 true일때 SQL문을 실행한다

 

T 조건일때 제목을 검색하여 키워드를 찾는다 

<if test="type == 'T' .toString()">
(title like '%' ||#{keyword} || '%')
</if>

 

C 조건일때 내용을 검색하여 키워드를 찾는다 

<if test="type == 'C' .toString()">
(content like '%' ||#{keyword} || '%')
</if>


W 조건일때 작성자을 검색하여 키워드를 찾는다 

</if test="type == 'w' .tostring()">
(content like '%' ||#{keyword} || '%')
</if>

 

 

(2) choose : 여러 상황들 중 하나에서만 SQL문이 실행한다 

 

<choose>
<when test="type == 'T' .toString()">
(title like '%' ||#{keyword} || '%')
</when>

<when test="type == 'C' .toString()">
(Content like '%' ||#{keyword} || '%')
</when>

<when test="type == 'W' .toString()">
(writer like '%' ||#{keyword} || '%')
</when>

<otherwise>
(title like '%' ||#{keyword} || '%' OR content like '%' ||#{keyword} || '%')
</otherwise>
</choose>

 

(3) where

select * from tbl_board
<where>
<if test="bno !=null">
bno =#{bno}
</if>
</where>

 

(4) trim

select * from tbl_board
<where>
<if test="bno !=null">
bno =#{bno}
</if>
<trim prifix="and">
rownum=1
</trim>
</where>

 

(5) foreach

select * from tbl_board
<trim prefix="where ("suffix=")" prefixOverrides="OR">
<foreach item="val" index="key" collection="map">
<trim prefix="OR">
<if test="key == 'C'.toString()">
contetn = #{val}
</if>
<if test="key == 'T'.toString()">
title = #{val}
</if>
<if test="key == 'W'.toString()">
writer = #{val}
</if>
</trim>
</foreach>
</trim>
반응형

'🌈 백엔드 > 스프링 프레임워크' 카테고리의 다른 글

검색처리 기능  (12) 2023.12.29
Junit @WebAppConfiguration API 테스트  (32) 2023.12.11
스프링_MyBatis_Mapper  (108) 2023.12.04
pom.xml 라이브러리 정리  (2) 2023.12.03
컨트롤러  (28) 2023.11.28

댓글