조건문으로 DB를 검색해야한다....
<form action="/board/search.kh" method="get">
<select name="searchCondition">
<option value="all">전체</option>
<option value="writer">작성자</option>
<option value="title">제목</option>
<option value="contents">내용</option>
</select>
<input type="text" name="searchValue">
<button type="submit" value="검색">검색</button>
</form>
먼저lselct값의 분류 값과 input에서 입력값을 같이 매핑시 전달해야하는데
클래스를 계속만들면 복잡하니깐 hashMap을 사용해서 한번에 그냥 전달했다....
그다음 조건문!
STORELOGIC에서 먼저 했다.물론controller에서 해도 된다.
@Override
public List<Board> selectAllByValue(SqlSessionTemplate session, String searchCondition, String searchValue,int currentPage, int limit) {
int offset=(currentPage-1)*limit;
RowBounds rowBounds = new RowBounds(offset, limit);
HashMap<String, String > paramMap=new HashMap<String, String>();
//검색시 데이터가 2개인데 같이 전송할수가 없다.
//해시맵을 사용해 2개를 한개로 묶는다. 아니면 클래스 사용해서 진행해야함 mybatis에서 했음
paramMap.put("searchCondition",searchCondition);
paramMap.put("searchValue",searchValue);
String mapping=null;
switch(searchCondition){
case "writer":
mapping="BoardMapper.selectAllByWriter";
break;
case "title":
mapping="BoardMapper.selectAllByTitle";
break;
case "contents":
mapping="BoardMapper.selectAllByContents";
break;
default:
mapping="BoardMapper.selectAllByContents";
break;
}
List<Board> bList=session.selectList(mapping, paramMap,rowBounds);
return bList;
}
2번째는 매핑이다. 조건문을 하고 나니 출력할 데이터의 페이징이 문제가 되었다.
<select id="selectAllByValue" resultMap="boardResultMap">
SELECT * FROM BOARD_TBL
<where>
<if test="searchCondition.toString() == 'title'"> BOARD_TITLE LIKE '%'||#{searchValue}||'%' </if>
<if test="searchCondition.toString() == 'contents'"> BOARD_CONTENTS LIKE '%'||#{searchValue}||'%' </if>
<if test="searchCondition.toString() == 'writer'"> BOARD_WRITER LIKE '%'||#{searchValue}||'%' </if>
<if test="searchCondition.toString() == 'all'"> BOARD_TITLE LIKE '%'||#{searchValue}||'%' OR BOARD_CONTENTS LIKE '%'||#{searchValue}||'%' OR BOARD_WRITER LIKE '%'||#{searchValue}||'%' </if>
AND B_STATUS = 'Y'
</where>
</select>
반영한 controller
@RequestMapping(value = "/board/search.kh", method = RequestMethod.GET)
public ModelAndView boardSearchList(ModelAndView mv
, @RequestParam("searchCondition") String searchCondition // select
, @RequestParam("searchValue") String searchValue // input
, @RequestParam(value = "page", required = false) Integer page) {
int currentPage = (page != null) ? page : 1;
int totalCount = bService.getTotalCount(searchCondition, searchValue);
// 분류전체게시물의 갯수가 필요함.
int limit = 10; // 한페이지당 보여주고 싶은 게시물의 갯수 //이것은 프론트에서 Param에서 받아올수 있다.
int naviLimit = 5; // 몇개의 페이지씩 표기할것인가.....[이전]1 2 3 4 5[다음]
int maxPage;
int startNavi;
int endNavi;
// 23/5 = 4.8+0.9=5(.7)=5 페이지 갯수구하는 것 무조건 올림해야한다.
maxPage = (int) ((double) totalCount / limit + 0.9);
startNavi = ((int) ((double) currentPage / naviLimit + 0.9) - 1) * naviLimit + 1;
endNavi = startNavi + naviLimit - 1; // 처음과 끝만 알면 나머지는 FOR문이 한다.
if (maxPage < endNavi) {
endNavi = maxPage;
}
//페이징 부분
try {
List<Board> bList = bService.printAllByValue(searchCondition, searchValue,currentPage, limit);
if (!bList.isEmpty()) {
mv.addObject("bList", bList);
mv.addObject("maxPage", maxPage);
mv.addObject("currentPage", currentPage);
mv.addObject("startNavi", startNavi);
mv.addObject("endNavi", endNavi);
mv.setViewName("board/listView");
}
} catch (Exception e) {
mv.addObject("msg", e.toString()).setViewName("common/errorPage");
}
return mv;
}
'SPRING' 카테고리의 다른 글
220915 스프링 게시판만들기 7-2 페이징:보던 페이지로 화면출력 (0) | 2022.09.15 |
---|---|
220915 스프링 게시판만들기 7-1 조건검색값null,검색칸에 값남기기 (0) | 2022.09.15 |
220914 스프링 게시판만들기 6-2 페이징-현재페이지 강조 (0) | 2022.09.14 |
220914 스프링 게시판만들기 6-1 첨부파일 수정하기. (0) | 2022.09.14 |
220913 스프링 게시판만들기 5-1 페이징 이전 다음 (0) | 2022.09.14 |