spring
day4 class04 검색 기능 추가 구현
일상다반ㅅㅏ
2019. 2. 11. 21:57
1. command 객체 수정
- BoardVO에 searchCondition과 searchKeyword 변수와 getter/setter 메소드를 추가한다.
2. controller 구현
- 로그인 성공 후의 상황이나 상세 화면에서 글 목록으로 되돌아올 경우 전체 목록 검색을 위해 null 체크를 한다.
- 기본이 TITLE에 Keyword는 "" 검색
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 글 목록 검색 @RequestMapping("/getBoardList.do") public String getBoardList(BoardVO vo, Model model) { // System.out.println("글 목록 검색 처리"); if(vo.getSearchCondition() == null) { vo.setSearchCondition("TITLE"); } if(vo.getSearchKeyword() == null) { vo.setSearchKeyword(""); } model.addAttribute("boardList", boardService.getBoardList(vo)); // Model 정보 저장 return "getBoardList.jsp"; } | cs |
3. DAO 클래스 수정
3-1 BoardDAO 수정
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 31 32 33 34 35 36 37 | // ~~~~~ private final String BOARD_LIST_T = "select * from board where title like '%'||?||'%' order by seq desc"; private final String BOARD_LIST_C = "select * from board where content like '%'||?||'%' order by seq desc"; // ~~~~~ // 글 목록 조회 public List<BoardVO> getBoardList(BoardVO vo) { System.out.println("===> JDBC로 getBoardList() 기능 처리"); List<BoardVO> boardList = new ArrayList<BoardVO>(); try { conn = JDBCUtil.getConnection(); // stmt = conn.prepareStatement(BOARD_LIST); if(vo.getSearchCondition().equals("TITLE")) { stmt = conn.prepareStatement(BOARD_LIST_T); } else if(vo.getSearchCondition().equals("CONTENT")) { stmt = conn.prepareStatement(BOARD_LIST_C); } stmt.setString(1, vo.getSearchKeyword()); rs = stmt.executeQuery(); while (rs.next()) { BoardVO board = new BoardVO(); board.setSeq(rs.getInt("SEQ")); board.setTitle(rs.getString("TITLE")); board.setWriter(rs.getString("WRITER")); board.setContent(rs.getString("CONTENT")); board.setRegDate(rs.getDate("REGDATE")); board.setCnt(rs.getInt("CNT")); boardList.add(board); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.close(rs, stmt, conn); } return boardList; } | cs |
3-2. BoardDAOSpring 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // ~~~~~ private final String BOARD_LIST_T = "select * from board where title like '%'||?||'%' order by seq desc"; private final String BOARD_LIST_C = "select * from board where content like '%'||?||'%' order by seq desc"; // ~~~~~ // 글 목록 조회 public List<BoardVO> getBoardList(BoardVO vo) { System.out.println("===> JDBC로 getBoardList() 기능 처리"); Object[] args = {vo.getSearchKeyword()}; if(vo.getSearchCondition().equals("TITLE")) { return jdbcTemplate.query(BOARD_LIST_T, args, new BoardRowMapper()); } else if(vo.getSearchCondition().equals("CONTENT")) { return jdbcTemplate.query(BOARD_LIST_C, args, new BoardRowMapper()); } return null; } // ~~~~~ | cs |