본문 바로가기

Archived(Programming)/Spring #1(기초)

Spring_data 처리6(ajax 통신 통한 search)

# Mapper 설정

cf) Mybatis if 사용 방법 및 String 이어주기

BoardMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
<mapper namespace="com.sinc.intern.board">
	...

	<select id="search" parameterType="map" resultType="board">
		SELECT SEQ, TITLE, WRITER, TO_CHAR(REGDATE, 'RRRR-MM-DD') REGDATE,
		VIEWCNT
		FROM INTERN_BOARD_TBL
		<where>
			<if test="type == 'title'">
				TITLE LIKE '%'||#{keyword}||'%'
			</if>
			<if test="type == 'writer'">
				WRITER LIKE '%'||#{keyword}||'%'
			</if>
		</where>
		ORDER BY 1 DESC
		<!-- WHERE SALARY <![CDATA[>]] > 1000 -->
	</select>
	
</mapper>  

# Dao 구현

package com.sinc.intern.board.model.sql;

import java.util.List;

public interface BoardDao {
	public List<Object> listRow();
	public int 			registerRow(Object obj);
	public Object		readRow(Object obj);
	public int			deleteRow(Object obj);
	public int			updateRow(Object obj);
	public List<Object> searchRow(Object obj);
}
package com.sinc.intern.board.model.sql;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

@Repository("boardD")
public class BoardDaoImpl implements BoardDao {
	
	// Session 생성
	@Resource(name="sqlSession")
	private SqlSession session;
	public static final String PREFIX_B = "com.sinc.intern.board";
	
	...
	// 검색
	@Override
	public List<Object> searchRow(Object obj) {
		System.out.println("Board search dao : " + obj);
		List<Object> list = session.selectList(PREFIX_B+".search", obj);
		return list;
	}
}

# Service 구현

package com.sinc.intern.board.service;

import java.util.List;

public interface BoardService {
	public List<Object> list();
	public int			register(Object obj);
	public Object		read(Object obj);
	public int			delete(Object obj);
	public int			update(Object obj);
	public List<Object> search(Object obj);
}
package com.sinc.intern.board.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sinc.intern.board.model.sql.BoardDao;

@Service("boardS")
public class BoardServiceImpl implements BoardService{

	@Resource(name="boardD")
	private BoardDao dao;

	// Service에서 DAO 호출 후 메서드 수행
	@Override
	public List<Object> list() {
		System.out.println("Board get list");
		return dao.listRow();
	}

	@Override
	public int register(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("Board service insert data");
		return dao.registerRow(obj);
	}

	@Override
	public Object read(Object obj) {
		System.out.println("Board service Read data");
		return dao.readRow(obj);
	}

	@Override
	public int delete(Object obj) {
		System.out.println("Board service delete data");
		return dao.deleteRow(obj);
	}

	@Override
	public int update(Object obj) {
		System.out.println("Board service update data");
		return dao.updateRow(obj);
	}

	@Override
	public List<Object> search(Object obj) {
		System.out.println("Board search data");
		return dao.searchRow(obj);
	}
	
	

}

# Controller 구현

package com.sinc.intern.board.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinc.intern.board.model.vo.BoardVO;
import com.sinc.intern.board.service.BoardService;

@Controller
public class BoardCtrl {
	@Resource(name="boardS")
	private BoardService service;
	
	...
	
	// 글 검색
	@RequestMapping("/search.do")
	@ResponseBody
	public List<Object> search(String type, String keyword, HashMap<String, String> map) {
		System.out.println("Board search :" + type + " " +keyword);
		map.put("type", type);
		map.put("keyword", keyword);
		List<Object> board = service.search(map);
		return board;
	}
}

# 프론트 구현

...
	
	$("#searchBtn").click(function() {
		$.ajax({
			url  : "search.do" , 
			type : "post" , 
			dataType : "json" , 
			data : { type : $("#searchType").val() , keyword : $("#searchKeyword").val() } ,
			success : function(data) {
				$("#tbody").empty();
				var txt = "";
				$.each(data , function(idx, obj) {
					txt +="<tr><td>"+obj.seq+"</td>" ; 
					txt +="<td><a href='readPage?seq="+obj.seq+"'>"+obj.title+"</a></td>";
					txt +="<td>"+obj.writer+"</td>";
					txt +="<td>"+obj.regdate+"</td>";
					txt +="<td><span class='badge bg-red'>"+obj.viewcnt+"</span></td></tr>" ; 
				});
				$("#tbody").append(txt); 
			}
			
		});
		
	});

검색 정상 작동

 

'Archived(Programming) > Spring #1(기초)' 카테고리의 다른 글

Spring_Interceptor  (0) 2020.02.07
Spring_data 처리5(글 수정)  (0) 2020.02.07
Spring_data 처리4(글 삭제)  (0) 2020.02.07
Spring_data 처리3(글 읽기)  (1) 2020.02.06
Spring_data 처리2(글 작성)  (0) 2020.02.06