반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- SpringBoot
- Eclipse
- mysql
- docker
- Java
- FCM
- config
- git
- ajax
- pwa
- popup
- jenkins
- MariaDB
- MSsql
- spring3
- Tomcat
- security
- jquery
- rocky9
- javascript
- centos7
- PM2
- Push
- NextJS
- yona
- nodejs
- node.js
- Next.js
- mybatis
- Maven
Archives
- Today
- Total
ふたりで
mybatis foreach 사용하기 본문
728x90
반응형
SMALL
예를 들어 단순하게 검색어를 공백을 기준으로 나누어 리스트화 후 해당 단어 리스트를
쿼리에서 AND 또는 OR검색으로 조건을 거는 경우 아래와 같이 처리함.
1. searchParams 모델 객체에 검색어를 리스트화할 geter, seter 정의.
private ArrayList<String> searchKeywordArr;
public ArrayList<String> getSearchKeywordArr() {
return searchKeywordArr;
}
public void setSearchKeywordArr(ArrayList<String> searchKeywordArr) {
this.searchKeywordArr = searchKeywordArr;
}
2. 검색어를 공백 기준으로 나누어 1에서 만든 인스턴스에 저장.
String spaceChkStr = searchParams.getSearchKeyword();
String[] words = spaceChkStr.split("\\s");//공백 기준으로 자른다.
ArrayList<String> wordsArr = new ArrayList<String>();
for(int i=0; i < words.length;i++) {
if(words[i].length() != 0) {//리스트에 담을때 길이가 0(공백)이 아닌 것만 담는다.
wordsArr.add(i, words[i]);
}
}
searchParams.setSearchKeywordArr(wordsArr);//완성된 리스트를 파라메터 객체에 담는다.
graykangDao.selectProductList(searchParams);//select호출.
3. mybatis에서 foreach를 사용해 조건 부분에 아래와 같이 사용한다.
<if test="searchKeyword != null and searchKeyword != '' ">
AND (
<foreach collection="searchKeywordArr" item="item" index="index" separator="" open="" close="">
<choose>
<when test="index == 0">
CONCAT(a_name,'',a_barcode,'') LIKE CONCAT('%',#{item},'%')
</when>
<otherwise>
AND CONCAT(a_name,'',a_barcode,'') LIKE CONCAT('%',#{item},'%')
</otherwise>
</choose>
</foreach>
OR (
<foreach collection="searchKeywordArr" item="item" index="index" separator="" open="" close="">
<choose>
<when test="index == 0">
INSTR(a_tag, #{item})
</when>
<otherwise>
AND INSTR(a_tag, #{item})
</otherwise>
</choose>
</foreach>
)
)
</if>
728x90
반응형
LIST
'Other' 카테고리의 다른 글
MSSQL 설치 후 한글 깨짐 (0) | 2023.12.20 |
---|---|
MSSQL COALESCE() 활용. (0) | 2023.12.15 |
eclipse + GIT + Rebase + force push (0) | 2023.04.18 |
yona+Git+clone+springboot+maven+STS4+eclipse+import (0) | 2022.12.06 |
windows10 +느려짐+해결+sfc+scannow (0) | 2022.11.18 |
Comments