- MEMBERS 테이블 INSERT, DELETE, UPDATE, SELECT
<소스구조>
[[ web.xml ]]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring06_members</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 인코딩필터 설정하기 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/-</url-pattern>
</filter-mapping>
<!-- /// applicationContext.xml을 읽어와 빈으로 등록하는 리스너 설정 /// -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- //////////////////// DispatcherServlet설정 //////////////////// -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- /////////////////////////////////////////////////////////////// -->
</web-app>
[[ applicatonContext.xml ]]
; 전역변수와 비슷한 개념으로 공통부분을 미리 선언해 놓음.
; db설정 등 ..
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- //// 전역적인 자원들이 등록 (Dao,Service,...) ////// -->
<!-- 1. DataSource 설정(url,username,password,..) -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- 2. SqlSessionFactory설정(dataSource,Mapper파일설정) -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:test/mybatis/-.xml"/>
</bean>
<!-- 3. SqlSessionTemplate설정(SqlSessionFactory주입) -->
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" index="0"/>
</bean>
<!-- 4. Dao생성(SqlSessionTemplate주입) -->
<bean id="membersDao" class="test.dao.MembersDao">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>
[[ mvc-servlet.xml ]]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- /////////// MVC관련 Bean객체 등록(컨트롤러,뷰리절버,..) //////// -->
<!-- //////////// 컨트롤러 등록 /////////////////-->
<bean class="test.controller.InsertController">
<property name="membersDao" ref="membersDao"/>
</bean>
<bean class="test.controller.ListController">
<property name="membersDao" ref="membersDao"/>
</bean>
<bean class="test.controller.DeleteController">
<property name="dao" ref="membersDao"/>
</bean>
<bean class="test.controller.UpdateController">
<property name="dao" ref="membersDao"/>
</bean>
<!--///////// 뷰리절버등록 //////////////
/WEB-INF/views/insert.jsp
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(1) main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<ul>
<!-- <li><a href="insertForm.do">회원가입</a></li> -->
<li><a href="insert.do">회원가입</a></li>
<li><a href="list.do">회원목록</a></li>
</ul>
</body>
</html>
(2) insert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>회원가입</h2>
<form action="insert.do" method="post">
아이디<input type="text" name="id" /><br/>
비밀번호<input type="password" name="pwd" /><br/>
이메일<input type="text" name="email"/><br/>
전화번호<input type="text" name="phone"/><br/>
<input type="submit" value="전송" />
</form>
</body>
</html>
(3) InsertController.java
; main.jsp에서 <a>태그로 넘어온 것은 GET방식, 저장할때 insert.jsp에서 넘어온 것은 POST방식
; RequestMapping에 insert.do 동일한 이름으로 받아도 method를 지정하여 받으면 상관없음.
; String 타입으로 return . 리턴된 값이 뷰페이지가됨.
mvc-servlet.xml에 등록한 Resolver 설정에 따라 경로가 지정됨.
package test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class InsertController {
private MembersDao membersDao;
public void setMembersDao(MembersDao membersDao) {
this.membersDao = membersDao;
}
//@RequestMapping("/insertForm.do")
@RequestMapping(value="/insert.do",method=RequestMethod.GET)
public String insertForm(){
//String타입이 반환되는것은 뷰이름을 의미함.
return "insert";
}
@RequestMapping("/insert.do")
public String insert(MembersVo vo){
//db에 vo저장하기
int n = membersDao.insert(vo);
if(n>0){
//뷰페이지로 이동 ==> 리다이렉트방식으로 페이지 이동하기
return "redirect:/main.jsp";
}else{
return "redirect:/error.jsp";
}
}
}
(4) DeleteController.java
; 삭제 후 목록페이지로 이동하기 위해조회 로직 수행
; 또는 UpdateController.java처럼 list.do를 return해도 됨.
package test.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class DeleteController {
private MembersDao dao;
public void setDao(MembersDao dao) {
this.dao = dao;
}
@RequestMapping("/delete.do")
public ModelAndView delete(String id){
System.out.println("id:" + id);
dao.delete(id);
//DB에서 회원정보 얻어오기
List<MembersVo> list = dao.getList();
//결과값(모델)과 뷰이름을 담을 객체 생성
ModelAndView mv = new ModelAndView("memlist");
//모델에 결과값 담기
mv.addObject("list", list);
return mv;
}
}
(5) UpdateController.java
package test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class UpdateController {
private MembersDao dao;
public void setDao(MembersDao dao) {
this.dao = dao;
}
@RequestMapping(value="/update.do",method=RequestMethod.GET)
public ModelAndView updateForm(String id){
//DB에서 회원정보 얻어오기
MembersVo vo = dao.getInfo(id);
//결과값(모델)과 뷰이름을 담을 객체 생성
ModelAndView mv = new ModelAndView("update");
//모델에 결과값 담기
mv.addObject("vo", vo);
return mv;
}
@RequestMapping("/update.do")
public String update(MembersVo vo){
int n = dao.update(vo);
System.out.println("id:" + vo.getId());
if ( n>0){
System.out.println("성공");
return "redirect:/list.do";
}else{
System.out.println("실패");
return "redirect:/error.jsp";
}
}
}
(6) ListController.java
package test.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class ListController {
private MembersDao membersDao;
public void setMembersDao(MembersDao membersDao) {
this.membersDao = membersDao;
}
@RequestMapping("/list.do")
public ModelAndView list(){
//DB에서 회원정보 얻어오기
List<MembersVo> list = membersDao.getList();
//결과값(모델)과 뷰이름을 담을 객체 생성
ModelAndView mv = new ModelAndView("memlist");
//모델에 결과값 담기
mv.addObject("list", list);
return mv;
}
}
(7) MembersDao.java
package test.dao;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import test.vo.MembersVo;
public class MembersDao {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<MembersVo> getList(){
return (List<MembersVo>)sqlSessionTemplate.selectList("getList");
}
public int insert(MembersVo vo){
return sqlSessionTemplate.insert("insert", vo);
}
public int delete(String id){
return sqlSessionTemplate.delete("delete", id);
}
public int update(MembersVo vo){
return sqlSessionTemplate.update("update", vo);
}
public MembersVo getInfo(String id){
return (MembersVo)sqlSessionTemplate.selectOne("getInfo", id);
}
}
(8) MembersVo.java
package test.vo;
import java.sql.Timestamp;
public class MembersVo {
private String id;
private String pwd;
private String email;
private String phone;
private Timestamp regdate;
public MembersVo(){}
public MembersVo(String id, String pwd, String email, String phone, Timestamp regdate) {
super();
this.id = id;
this.pwd = pwd;
this.email = email;
this.phone = phone;
this.regdate = regdate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Timestamp getRegdate() {
return regdate;
}
public void setRegdate(Timestamp regdate) {
this.regdate = regdate;
}
}
(9) MembersMapper.java
<?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="orm.mybatis.MembersMapper">
<insert id="insert" parameterType="test.vo.MembersVo">
insert into Members values ( #{ id }, #{ pwd }, #{ email }, #{ phone }, sysdate )
</insert>
<select id="getList" resultType="test.vo.MembersVo">
select * from members
</select>
<delete id="delete" parameterType="String">
delete from members where id=#{value}
</delete>
<select id="getInfo" resultType="test.vo.MembersVo" parameterType="String">
select * from members where id=#{value}
</select>
<update id="update" parameterType="test.vo.MembersVo">
update members set pwd=#{pwd},email=#{email},phone=#{phone} where id=#{id}
</update>
</mapper>
'WEB > Spring3.0' 카테고리의 다른 글
SPRING3.0 #11(게시판) (0) | 2013.01.27 |
---|---|
SPRING3.0 #11(annotation) (0) | 2013.01.27 |
SPRING3.0 #09( MVC ) (0) | 2013.01.27 |
SPRING3.0 #08( Mybatis ) (0) | 2013.01.27 |
SPRING3.0 #07( jdbc ) (0) | 2013.01.27 |