일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
- 그럼 int배열의 deefault값은?????
- boolean배열
- 출처:구멍가게코딩단-코배스(개정판)
- bindingresult 쓰니까 에러났다. 어떻게해야하냐;;
- 생활코딩
- (참고로 boolean 배열의 default 값은 false 이다.
- while문이 틀린이유?? math.random()을 사용해서푸는법?
- ㅓㅂ
- 마지막에 배열의 foreach구문이 틀린것같은데 ...... 저게왜틀린건지나는잘모르겠슴다.
- 행열. 2중반복문..
- form.getImageFies 오타났음
- Today
- Total
푸린세스
mybatis 검색.->totalCount 바꾸기. 본문
<select id="getListWithPaging" resultType="org.moominzero.domain.BoardVO">
<![CDATA[select R1.* FROM(
SELECT * FROM tbl_board
order by bno desc
) R1 ]]>
<trim prefix="where (" suffix=")">
<foreach collection="typeArr" item="type" separator="OR">
<if test="type=='T'.toString()">
title like concat('%',#{keyword},'%')
</if>
<if test="type=='C'.toString()">
content like concat('%',#{keyword},'%')
</if>
<if test="type=='W'.toString()">
writer like concat('%',#{keyword},'%')
</if>
</foreach>
</trim>
<include refid="criteria"></include>
<![CDATA[
LIMIT #{amount} OFFSET #{skipCount}]]>
</select>
select R1.* FROM(
SELECT * FROM tbl_board
order by bno desc
) R1 LIMIT 10 OFFSET 0;
검색추가시
- select R1.* FROM( SELECT * FROM tbl_board order by bno desc ) R1 where ( title like concat('%','%새로%','%')
OR content like concat('%','%새로%','%') OR writer like concat('%','%새로%','%') ) LIMIT 10 OFFSET
select R1.* FROM(
SELECT * FROM tbl_board
order by bno desc
) R1 where (title like '%test%' or content like '%test%' ) LIMIT 10 OFFSET 0;
-> trim으로 where ( 를 prefix, )를 suffix로 잡았다.
검색시 totalcount 갯수도 바뀌게된다.
우선 sql Fragment
include를 하자
<!-- sql조각선언 -->
<sql id="criteria">
<trim prefix="where (" suffix=")">
<foreach collection="typeArr" item="type" separator="OR">
<if test="type=='T'.toString()">
title like concat('%',#{keyword},'%')
</if>
<if test="type=='C'.toString()">
content like concat('%',#{keyword},'%')
</if>
<if test="type=='W'.toString()">
writer like concat('%',#{keyword},'%')
</if>
</foreach>
</trim>
</sql>
<select id="getListWithPaging" resultType="org.moominzero.domain.BoardVO">
<![CDATA[select R1.* FROM(
SELECT * FROM tbl_board
order by bno desc
) R1 ]]>
<include refid="criteria"></include>
<![CDATA[
LIMIT #{amount} OFFSET #{skipCount}]]>
</select>
이제 검색시 total Count 해보자.
select count(*) from tbl_board where bno>0;
select count(*) from tbl_board where (title like '%새로%' )and bno>0;
검색 조건이 추가시 => 원래 붙는 where을 없애주고 마지막에 and를 붙여준다.
<select id="getTotalCount" resultType ="int">
select count(*) from tbl_board where
<trim prefixOverrides="where" suffix="and"><include refid="criteria"></include></trim> bno >0
</select>
prefixOveerides 로 where 선언과 suffix로 and를 선언해주었다.
이제 테스트
@Test
public void testTotalCount() {
Criteria cri = new Criteria();
cri.setType("TCW");
cri.setKeyword("%새로%");
int count =mapper.getTotalCount(cri);
log.info(count);
}
select count(*) from tbl_board where ( title like concat('%','%새로%','%') OR content like concat('%','%새로%','%')
OR writer like concat('%','%새로%','%') ) and bno >0
정상작동한다!
'spring > 구멍가게코딩단-스프링' 카테고리의 다른 글
ReplyMapperTests (0) | 2023.05.09 |
---|---|
@RestController의 반환타입 (0) | 2023.05.09 |
페이징화면 처리 (14장) (0) | 2023.05.06 |
rownum과 index (0) | 2023.05.06 |
MySQL ~ log4jdbc 설정하기. (0) | 2023.05.03 |