1. 개발샘플 : 조회 + 수정
//p0003.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="hm.p0003">
<resultMap id="p0003Result01" type="p0003VO">
<result property="cust_id" column="cust_id" />
<result property="cust_name" column="cust_name" />
<result property="cust_address" column="cust_address" />
<result property="cust_state" column="cust_state" />
<result property="cust_zip" column="cust_zip" />
<result property="cust_country" column="cust_country" />
<result property="cust_contact" column="cust_contact" />
<result property="cust_email" column="cust_email" />
</resultMap>
<select id="searchMember" resultMap="p0003Result01" parameterType="java.util.Map">
<![CDATA[
select cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email
from customers
]]>
<where>
<if test=" p_id != '' and p_id != null">
cust_id = #{p_id}
</if>
</where>
</select>
</mapper>
-----------------------------------------
//p0004_init.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 검색창</title>
</head>
<body>
<form name="frm" method="post" action="${contextPath}/hm/p0003/searchMember.do">
고객번호 : <input type="text" name="p_id">
<input type="submit" value="조회하기">
</form>
</body>
</html>
-----------------------------------------------------
//p0004_mod.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*"
import="sec02.ex01.*"
pageEncoding="UTF-8"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<jsp:useBean id="p0003VO" class="project.hm.p0003.vo.P0003VO" scope="request"/>
<%
request.setCharacterEncoding( "utf-8" );
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function frm_update() {
var frmPro = document.frm;
frmPro.method = "post";
frmPro.action = "${contextPath}/hm/p0003/updateMember.do";
frmPro.submit();
}
</script>
</head>
<body>
<form name="frm" method="post" encType="UTF-8">
고객번호 :<input type="text" name="cust_id" value="${p0003VO.cust_id}" disabled /><br>
고객이름 :<input type="text" name="cust_name" value="${p0003VO.cust_name}"><br>
고객주소 :<input type="text" name="cust_address" value="${p0003VO.cust_address}"><br>
고객주 :<input type="text" name="cust_state" value="${p0003VO.cust_state}"><br>
고객우편번호 :<input type="text" name="cust_zip" value="${p0003VO.cust_zip}"><br>
고객국가 :<input type="text" name="cust_country" value="${p0003VO.cust_country}"><br>
고객담당자 :<input type="text" name="cust_contact" value="${p0003VO.cust_contact}"><br>
고객메일주소 :<input type="text" name="cust_email" value="${p0003VO.cust_email}"><br>
<c:if test="${command=='modSearch'}" >
<input type="submit" name='submit' value="수정" onclick = "frm_update()">
<input type='hidden' name='command' value='modUpdate' />
<input type='hidden' name='cust_id' value="${p0003VO.cust_id}" />
</c:if>
</form>
</body>
</html>
-----------------------------------------------------
//p0004_search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*"
import="sec02.ex01.*"
pageEncoding="UTF-8"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
}
</style>
<meta charset="UTF-8">
<title>고객 정보 출력창</title>
</head>
<body>
<h1>고객 정보 출력</h1>
<%
request.setCharacterEncoding( "utf-8" );
%>
<table border='1' width='800' align='center'>
<tr align='center' bgcolor='#FFFF66'>
<td>고객번호</td>
<td>고객이름</td>
<td>고객주소</td>
<td>고객주</td>
<td>고객우편번호</td>
<td>고객국가</td>
<td>고객담당자</td>
<td>고객메일주소</td>
<td>수정</td>
</tr>
<c:forEach var="searchMember" items="${searchMember}" >
<c:url var="url" value="searchMod.do" >
<c:param name="p_mod_id" value="${searchMember.cust_id}" />
</c:url>
<tr align=center>
<td>${searchMember.cust_id}</td>
<td>${searchMember.cust_name}</td>
<td>${searchMember.cust_address}</td>
<td>${searchMember.cust_state}</td>
<td>${searchMember.cust_zip}</td>
<td>${searchMember.cust_country}</td>
<td>${searchMember.cust_contact}</td>
<td>${searchMember.cust_email}</td>
<td><a href='${url}'> 수정 </a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
-----------------------------------------------------
//P0004ControllerImpl
package project.hm.p0003.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import project.hm.p0003.service.P0003Service;
import project.hm.p0003.vo.P0003VO;
@Controller("p0003Controller")
public class P0003ControllerImpl implements P0003Controller {
private static final Logger logger = LoggerFactory.getLogger(P0003ControllerImpl.class);
@Autowired
P0003Service P0003Service;
@Autowired
P0003VO P0003VO;
@Override
@RequestMapping(value = "/hm/p0003/searchInit.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView searchInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
ModelAndView mav = new ModelAndView("hm/p0003_init");
return mav;
}
@Override
@RequestMapping(value = "/hm/p0003/searchMember.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView searchMember(@RequestParam(value="p_id", required=false) String p_id, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
Map<String, Object> searchMap = new HashMap<String, Object>();
searchMap.put("p_id", p_id);
List list = P0003Service.searchMember(searchMap);
ModelAndView mav = new ModelAndView("hm/p0003_search");
mav.addObject("searchMember", list);
return mav;
}
@Override
@RequestMapping(value = "/hm/p0003/searchMod.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView searchMod(@RequestParam(value="p_mod_id", required=false) String p_id, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
Map<String, Object> searchMap = new HashMap<String, Object>();
searchMap.put("p_id", p_id);
List list = P0003Service.searchMod(searchMap);
if(!list.isEmpty()) {
P0003VO = (P0003VO)list.get(0);
}
ModelAndView mav = new ModelAndView("hm/p0003_mod");
mav.addObject("p0003VO", P0003VO);
mav.addObject("command", "modSearch");
return mav;
}
@Override
@RequestMapping(value = "/hm/p0003/updateMember.do", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public ResponseEntity updateMember(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
Map<String, Object> dataMap = new HashMap<String, Object>();
Enumeration enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String name = (String) enu.nextElement();
String value = request.getParameter(name);
dataMap.put(name, value);
}
String message;
ResponseEntity resEnt = null;
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "text/html; charset=utf-8");
try {
P0003Service.updateMember(dataMap);
RequestDispatcher dispatch = request.getRequestDispatcher("/hm/p0003/searchMember.do");
dispatch.forward(request, response);
} catch (Exception e) {
message = " <script>";
message += " alert('오류발생');";
message += " location.href='" + request.getContextPath() + "/hm/p0003/searchInit.do'; ";
message += " </script>";
resEnt = new ResponseEntity(message, responseHeaders, HttpStatus.CREATED);
e.printStackTrace();
}
return resEnt;
}
@RequestMapping(value = "/common/ajaxTest", produces="application/json", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public Map<String, Object> ajaxTest() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "hong");
map.put("name", "??浿");
return map;
}
}
-----------------------------------------------------
//P0003Controller
package project.hm.p0003.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
public interface P0003Controller {
public ModelAndView searchInit(HttpServletRequest request, HttpServletResponse response) throws Exception;
public ModelAndView searchMember(String p_id, HttpServletRequest request, HttpServletResponse response) throws Exception;
public ModelAndView searchMod(String p_id, HttpServletRequest request, HttpServletResponse response) throws Exception;
public ResponseEntity updateMember(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
-----------------------------------------------------
//P0003DAOImpl
package project.hm.p0003.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import project.hm.p0003.vo.P0003VO;
@Repository("p0003DAO")
public class P0003DAOImpl implements P0003DAO {
@Autowired
private SqlSession sqlSession;
@Override
public List<P0003VO> searchMember(Map<String, Object> searchMap) throws DataAccessException {
List<P0003VO> list = sqlSession.selectList("hm.p0003.searchMember", searchMap);
return list;
}
@Override
public List<P0003VO> searchMod(Map<String, Object> searchMap) throws DataAccessException {
List<P0003VO> list = sqlSession.selectList("hm.p0003.searchMod", searchMap);
return list;
}
@Override
public void updateMember(Map<String, Object> datahMap) throws DataAccessException {
sqlSession.update("hm.p0003.updateMember", datahMap);
}
}
-----------------------------------------------------
//P0004DAOImpl
package project.hm.p0003.dao;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import project.hm.p0003.vo.P0003VO;
public interface P0003DAO {
public List<P0003VO> searchMember(Map<String, Object> searchMap) throws DataAccessException;
public List<P0003VO> searchMod(Map<String, Object> searchMap) throws DataAccessException;
public void updateMember(Map<String, Object> datahMap) throws DataAccessException;
}
-----------------------------------------------------
//P0003ServiceImpl
package project.hm.p0003.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import project.hm.p0003.dao.P0003DAO;
import project.hm.p0003.vo.P0003VO;
@Service("p0003Service")
@Transactional(propagation = Propagation.REQUIRED)
public class P0003ServiceImpl implements P0003Service {
@Autowired
private P0003DAO P0003DAO;
@Override
public List<P0003VO> searchMember(Map<String, Object> searchMap) throws DataAccessException {
List<P0003VO> list = P0003DAO.searchMember(searchMap);
return list;
}
@Override
public List<P0003VO> searchMod(Map<String, Object> searchMap) throws DataAccessException {
List<P0003VO> list = P0003DAO.searchMod(searchMap);
return list;
}
@Override
public void updateMember(Map<String, Object> datahMap) throws Exception {
P0003DAO.updateMember(datahMap);
}
}
-----------------------------------------------------
//P0003Service
package project.hm.p0003.service;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import project.hm.p0003.vo.P0003VO;
public interface P0003Service {
public List<P0003VO> searchMember(Map<String, Object> searchMap) throws DataAccessException;
public List<P0003VO> searchMod(Map<String, Object> searchMap) throws DataAccessException;
public void updateMember(Map<String, Object> datahMap) throws Exception;
}
-------------------------------
// P0003VO
package project.hm.p0003.vo;
import java.sql.Date;
import org.springframework.stereotype.Component;
@Component("p0003VO")
public class P0003VO {
private String cust_id;
private String cust_name;
private String cust_address;
private String cust_state;
private String cust_zip;
private String cust_country;
private String cust_contact;
private String cust_email;
public P0003VO() {
System.out.println("MemberVO 생성자 호출");
}
public String getCust_id() {
return cust_id;
}
public void setCust_id(String cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public String getCust_state() {
return cust_state;
}
public void setCust_state(String cust_state) {
this.cust_state = cust_state;
}
public String getCust_zip() {
return cust_zip;
}
public void setCust_zip(String cust_zip) {
this.cust_zip = cust_zip;
}
public String getCust_country() {
return cust_country;
}
public void setCust_country(String cust_country) {
this.cust_country = cust_country;
}
public String getCust_contact() {
return cust_contact;
}
public void setCust_contact(String cust_contact) {
this.cust_contact = cust_contact;
}
public String getCust_email() {
return cust_email;
}
public void setCust_email(String cust_email) {
this.cust_email = cust_email;
}
}
2. 개발샘플 : ajax 로 구현(VO는 1번과 동일)
//p0004.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="hm.p0004">
<resultMap id="p0004Result01" type="p0004VO"> <!--column: SQL의 컬럼명 -->
<result property="cust_id" column="cust_id" />
<result property="before_id" column="before_id" />
<result property="after_id" column="after_id" />
<result property="cust_name" column="cust_name" />
<result property="cust_address" column="cust_address" />
<result property="cust_state" column="cust_state" />
<result property="cust_zip" column="cust_zip" />
<result property="cust_country" column="cust_country" />
<result property="cust_contact" column="cust_contact" />
<result property="cust_email" column="cust_email" />
</resultMap>
<select id="searchMember" resultMap="p0004Result01" parameterType="java.util.Map">
<![CDATA[
SELECT *
FROM (
SELECT a.*
, LAG(cust_id, 1, 0) OVER (ORDER BY cust_id desc) as after_id
, LEAD(cust_id, 1, 0) OVER (ORDER BY cust_id desc) as before_id
FROM customers a
)
]]>
<where>
<if test=" p_id != '' and p_id != null">
cust_id = #{p_id}
</if>
ORDER BY cust_id desc
</where>
</select>
<update id="updateMember" parameterType="java.util.Map">
update customers
set cust_name = #{cust_name,jdbcType=VARCHAR}
, cust_address = #{cust_address,jdbcType=VARCHAR}
, cust_state = #{cust_state,jdbcType=VARCHAR}
, cust_zip = #{cust_zip,jdbcType=VARCHAR}
, cust_country = #{cust_country,jdbcType=VARCHAR}
, cust_contact = #{cust_contact,jdbcType=VARCHAR}
, cust_email = #{cust_email,jdbcType=VARCHAR}
where cust_id = #{cust_id, jdbcType=VARCHAR}
</update>
</mapper>
-----------------------------------------
//p0004.ajax.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>p0004</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function fn_process(command){
var v_url = '';
var vp_id = $("#p_id").val();
if(command == 'before'){
command = 'search';
var before_id = $("#before_id").val();
$("#p_id").val(before_id);
vp_id = $("#p_id").val();
}
if(command == 'after'){
command = 'search';
var after_id = $("#after_id").val();
$("#p_id").val(after_id);
vp_id = $("#p_id").val();
}
// command 설정
$("#command").val(command);
var params = $("#frm").serialize();
console.log("cust_id : " + vp_id);
console.log("command : " + command);
// 조회 시 validation + 널값 체크
if(command == 'search' && vp_id == ''){
$('#frm')[0].reset();
$('#cust_id').attr("readonly",true).attr("disabled",true) // 조회시 cust_id 활성화
alert("고객번호를 입력하세요");
return;
}
// 추가 시 값 초기화 후 종료
if(command == 'add'){
$('#frm')[0].reset();
$('#cust_id').attr("readonly",false).attr("disabled",false)
return;
}
// 저장 시 validation
var v_id = $("#cust_id").val();
if(command == 'save' && v_id == ''){
alert("조회나 추가 후 저장을 선택해 주세요");
return;
}
if(command == 'search'){
v_url = "${contextPath}/hm/p0004/searchMember.do";
}else if(command == 'save'){
v_url = "${contextPath}/hm/p0004/updateMember.do";
}
$.ajax({
type:"post",
async:false,
url:v_url,
data: params,
dataType:"json",
success:function (data,textStatus){
//var jsonInfo = JSON.parse(data);
var jsonInfo = data;
if(command == 'search'){
if(jsonInfo.error_yn == 'Y'){
alert(jsonInfo.error_text);
$("#frm")[0].reset();
return;
}
console.log("search로그" + jsonInfo.cust_id);
$('#cust_id').val(jsonInfo.cust_id);
$('#before_id').val(jsonInfo.before_id);
$('#after_id').val(jsonInfo.after_id);
$('#idCopy').val(jsonInfo.cust_id);
$('#cust_name').val(jsonInfo.cust_name);
$('#cust_address').val(jsonInfo.cust_address);
$('#cust_state').val(jsonInfo.cust_state);
$('#cust_zip').val(jsonInfo.cust_zip);
$('#cust_country').val(jsonInfo.cust_country);
$('#cust_contact').val(jsonInfo.cust_contact);
$('#cust_email').val(jsonInfo.cust_email);
}else if(command == 'save'){
if(jsonInfo.error_yn == 'Y'){
alert(jsonInfo.error_text);
}else{
alert('저장되었습니다');
}
}
},
error:function(request,textStatus,error){
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
},
complete:function(data,textStatus){
alert("작업을완료 했습니다");
}
});
}
</script>
</head>
<body>
<form name="frm" id="frm">
고객번호<input type="text" id="p_id" name="p_id" >
<input type="hidden" id="command" name="command" />
<input type="button" id="btn_search" value="조회" onClick="fn_process('search')"/>
<input type="button" id="btn_add" value="추가" onClick="fn_process('add')" />
<input type="button" id="btn_save" value="저장" onClick="fn_process('save')" />
<input type="button" id="btn_before" value="이전" onClick="fn_process('before')" />
<input type="button" id="btn_after" value="이후" onClick="fn_process('after')" />
<table border=1 align=left>
<tr>
<td>고객번호</td>
<td><input type="text" name="cust_id" id="cust_id" disabled /></td>
<input type="hidden" name="idCopy" id="idCopy">
<input type="hidden" name="before_id" id="before_id">
<input type="hidden" name="after_id" id="after_id">
</tr>
<tr>
<td>고객이름</td>
<td><input type="text" name="cust_name" id="cust_name"></td>
</tr>
<tr>
<td>고객주소</td>
<td><input type="text" name="cust_address" id="cust_address"></td>
</tr>
<tr>
<td>고객주</td>
<td><input type="text" name="cust_state" id="cust_state">
</td>
</tr>
<tr>
<td>고객우편번호</td>
<td><input type="text" name="cust_zip" id="cust_zip"></td>
</tr>
<tr>
<td>고객국가</td>
<td><input type="text" name="cust_country" id="cust_country">
</td>
</tr>
<tr>
<td>고객담당자</td>
<td><input type="text" name="cust_contact" id="cust_contact"></td>
</tr>
<tr>
<td>고객메일주소</td>
<td><input type="text" name="cust_email" id="cust_email"></td>
</tr>
</table>
</form>
</body>
</html>
-----------------------------------------
//P0004ControllerImpl
package project.hm.p0004.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import project.hm.p0004.service.P0004Service;
import project.hm.p0004.vo.P0004VO;
@Controller("p0004Controller")
public class P0004ControllerImpl implements P0004Controller {
private static final Logger logger = LoggerFactory.getLogger(P0004ControllerImpl.class);
@Autowired
P0004Service P0004Service;
@Autowired
P0004VO P0004VO;
@Override
@RequestMapping(value = "/hm/p0004/searchInit.do", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView searchInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
ModelAndView mav = new ModelAndView("hm/p0004_ajax");
return mav;
}
@Override
@RequestMapping(value = "/hm/p0004/searchMember.do", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public Map searchMember(@RequestParam(value="p_id", required=false) String p_id, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
Map<String, Object> searchMap = new HashMap<String, Object>(); // 검색조건
Map<String, Object> resultMap = new HashMap<String, Object>(); // 조회결과
searchMap.put("p_id", p_id);
List list = null;
try {
list = P0004Service.searchMember(searchMap);
if(!list.isEmpty()) {
P0004VO = (P0004VO)list.get(0);
resultMap = BeanUtils.describe(P0004VO);
resultMap.put("error_yn", "N");
}else {
resultMap.put("error_yn", "Y");
resultMap.put("error_text", "존재하지 않습니다");
}
}catch(Exception e) {
resultMap.put("error_yn", "Y");
resultMap.put("error_text", "에러발생");
e.printStackTrace();
}
return resultMap;
}
@Override
@RequestMapping(value = "/hm/p0004/updateMember.do", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public Map updateMember(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("utf-8");
Map<String, Object> dataMap = new HashMap<String, Object>(); // 저장할 Data
Map<String, Object> resultMap = new HashMap<String, Object>(); // 처리결과
Enumeration enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String name = (String) enu.nextElement();
String value = request.getParameter(name);
dataMap.put(name, value);
}
try {
P0004Service.updateMember(dataMap);
resultMap.put("error_yn", "N");
} catch (Exception e) {
resultMap.put("error_yn", "Y");
resultMap.put("error_text", "에러발생");
e.printStackTrace();
}
System.out.println("=======================>>"+resultMap.toString());
return resultMap;
}
@Override
public ModelAndView searchInsert(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
return null;
}
@RequestMapping(value = "/common/ajaxTest2", produces="application/json2", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public Map<String, Object> ajaxTest() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "hong");
map.put("name", "??浿");
return map;
}
}
-----------------------------------------
//P0004Controller
package project.hm.p0004.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
public interface P0004Controller {
public ModelAndView searchInit(HttpServletRequest request, HttpServletResponse response) throws Exception;
public Map searchMember(String p_id, HttpServletRequest request, HttpServletResponse response) throws Exception;
public ModelAndView searchInsert(HttpServletRequest request, HttpServletResponse response) throws Exception;
public Map updateMember(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
-----------------------------------------
//P0004DAOImpl
package project.hm.p0004.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import project.hm.p0004.vo.P0004VO;
@Repository("p0004DAO")
public class P0004DAOImpl implements P0004DAO {
@Autowired
private SqlSession sqlSession;
@Override
public List<P0004VO> searchMember(Map<String, Object> searchMap) throws DataAccessException {
List<P0004VO> list = sqlSession.selectList("hm.p0004.searchMember", searchMap);
return list;
}
@Override
public void updateMember(Map<String, Object> datahMap) throws DataAccessException {
sqlSession.update("hm.p0004.updateMember", datahMap);
}
}
-----------------------------------------
//P0004DAO
package project.hm.p0004.dao;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import project.hm.p0004.vo.P0004VO;
public interface P0004DAO {
public List<P0004VO> searchMember(Map<String, Object> searchMap) throws DataAccessException;
public void updateMember(Map<String, Object> datahMap) throws DataAccessException;
}
-----------------------------------------
//P0004ServiceImpl
package project.hm.p0004.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import project.hm.p0004.dao.P0004DAO;
import project.hm.p0004.vo.P0004VO;
@Service("p0004Service")
@Transactional(propagation = Propagation.REQUIRED)
public class P0004ServiceImpl implements P0004Service {
@Autowired
private P0004DAO P0004DAO;
@Override
public List<P0004VO> searchMember(Map<String, Object> searchMap) throws DataAccessException {
List<P0004VO> list = P0004DAO.searchMember(searchMap);
return list;
}
@Override
public void updateMember(Map<String, Object> datahMap) throws Exception {
P0004DAO.updateMember(datahMap);
}
}
-----------------------------------------
//P0004Service
package project.hm.p0004.service;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import project.hm.p0004.vo.P0004VO;
public interface P0004Service {
public List<P0004VO> searchMember(Map<String, Object> searchMap) throws DataAccessException;
public void updateMember(Map<String, Object> datahMap) throws Exception;
}