카테고리 없음

스프링MVC : 회원

enterit 2022. 11. 15. 15:39

* 테이블

CREATE TABLE `wsuser` (
  `useridx` int(11) NOT NULL AUTO_INCREMENT COMMENT '회원 고유번호',
  `userid` varchar(255) NOT NULL COMMENT '회원 아이디',
  `userpw` varchar(255) NOT NULL DEFAULT '' COMMENT '회원 비밀번호',
  `username` varchar(255) NOT NULL DEFAULT '' COMMENT '회원 이름',
  `useremail` varchar(255) NOT NULL DEFAULT '' COMMENT '회원 이메일',
  `userdatereg` datetime DEFAULT NULL COMMENT '회원 등록 날짜',
  `userdatemod` datetime DEFAULT NULL COMMENT '회원 정보 수정 날짜',
  PRIMARY KEY (`useridx`) USING BTREE,
  KEY `userid` (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

 

* UserVO

package com.ps.wsol.domain;

import java.util.Date;

public class UserVO {
	
	private int useridx;
	private String userid;
    private String userpw;
    private String username;
    private String useremail;
    private Date userdatereg;
    private Date userdatemod;
    
	public int getUseridx() {
		return useridx;
	}
	public void setUseridx(int useridx) {
		this.useridx = useridx;
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getUserpw() {
		return userpw;
	}
	public void setUserpw(String userpw) {
		this.userpw = userpw;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUseremail() {
		return useremail;
	}
	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}
	public Date getUserdatereg() {
		return userdatereg;
	}
	public void setUserdatereg(Date userdatereg) {
		this.userdatereg = userdatereg;
	}
	public Date getUserdatemod() {
		return userdatemod;
	}
	public void setUserdatemod(Date userdatemod) {
		this.userdatemod = userdatemod;
	}
	
}

 

* UserDAO

package com.ps.wsol.dao;

import java.util.List;

import com.ps.wsol.domain.UserVO;

public interface UserDAO {
	
	public List<UserVO> list() throws Exception;
	
	public UserVO detail(String userId) throws Exception;
	
	public void insert(UserVO vo) throws Exception;
	
	public void update(UserVO vo) throws Exception;
	
	public void delete(String userId) throws Exception;;
	
}

 

* UserDAOImpl

package com.ps.wsol.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ps.wsol.domain.UserVO;

@Repository
public class UserDAOImpl implements UserDAO {
	
	@Autowired
	private SqlSession sqlSession;
	
	private static String namespace = "com.ps.wsol.mapper.UserMapper";
	
	@Override
	public List<UserVO> list() throws Exception {
		//sqlSession.selectList(namespace + ".listUser");
		return sqlSession.selectList(namespace+".listUser");
		//return null;
	}
	
	@Override
	public UserVO detail(String userId) throws Exception {
		return sqlSession.selectOne(namespace+".detailUser", userId);
	}
	
	@Override
	public void insert(UserVO vo) throws Exception {
		sqlSession.insert(namespace + ".insertUser", vo);
	}
	
	@Override
	public void update(UserVO vo) throws Exception {
		sqlSession.update(namespace + ".updateUser", vo);
	}
	
	@Override
	public void delete(String userId) throws Exception {
		sqlSession.delete(namespace + ".deleteUser", userId);
	}
	
}

 

* userMapper.xml

 

* UserService

package com.ps.wsol.service;

import java.util.List;

import com.ps.wsol.domain.UserVO;

public interface UserService {
	
	public List<UserVO> list() throws Exception;
	
	public UserVO detail(String userId) throws Exception;
	
	public void insert(UserVO vo) throws Exception;
	
	public void update(UserVO vo) throws Exception;
	
	public void delete(String userId) throws Exception;
	
}

 

* UserServiceImpl

package com.ps.wsol.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ps.wsol.dao.UserDAO;
import com.ps.wsol.domain.UserVO;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDAO dao;
	
	@Override
	public List<UserVO> list() throws Exception {
		return dao.list();
	}
	
	@Override
	public UserVO detail(String userId) throws Exception {
		return dao.detail(userId);
	}
	
	@Override
	public void insert(UserVO vo) throws Exception {
		dao.insert(vo);
	}
	
	@Override
	public void update(UserVO vo) throws Exception {
		dao.update(vo);
	}
	
	@Override
	public void delete(String userId) throws Exception {
		dao.delete(userId);
	}
	
}

 

* UserController

package com.ps.wsol.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ps.wsol.domain.UserVO;
import com.ps.wsol.service.UserService;

@Controller
@RequestMapping("/user/")
public class UserController {
	
	@Autowired
	private UserService service;
	
	@RequestMapping(value = "/userList")
	public void listUser(Model model) throws Exception {
		//System.out.println("User 목록");
		model.addAttribute("userList", service.list());
	}
	
	
	@RequestMapping(value = "/userDetail")
	public void detailUser(@RequestParam("userid") String userid, Model model) throws Exception {
		System.out.println("User ID : " + userid + " 상세 내용 페이지");
		model.addAttribute(service.detail(userid));
	}
	
	
	@RequestMapping(value = "/userForm")
	public String userForm(UserVO user, Model model) throws Exception {
		return "/user/userForm";
	}
	

	
	@RequestMapping(value = "/userFormMod")
	public String userFormMod(@RequestParam("userid") String userid, Model model) throws Exception {
		model.addAttribute(service.detail(userid));
		return "/user/userFormMod";
	}
	
	
	@RequestMapping(value = "/userInsert", method = RequestMethod.POST)
	public String insertUser(UserVO user, Model model) throws Exception {
		//System.out.println(user.toString());
		
		service.insert(user);
		//model.addAttribute("result", "성공");
		
		return "redirect:/user/userList";
		//return "/user/userResult";
	}
	
	
	@RequestMapping(value = "/userUpdate", method = RequestMethod.POST)
	public String updateUser(UserVO user, Model model) throws Exception {
		//System.out.println(user.toString());
		
		service.update(user);
		//String userId = model.getAttribute(userId);
		String userId = user.getUserid();
		//System.out.println(userId);
		
		return "redirect:/user/userDetail?userid="+userId;
		//return "";
	}
	
	
	@RequestMapping(value = "userDelete")
	public String deleteUser(@RequestParam("userid") String userid) throws Exception {
		System.out.println("회원 삭제 : " + userid );
		service.delete(userid);
		
		return "redirect:/user/userList";
	}
	
}

 

* userList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page session="false" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User</title>
</head>
<body>

<h1>회원 리스트</h1>

<br>
<a href="/user/userForm">회원등록</a>

<table border="1" width="80%" align="center">
<tr>
<th style="width:10%">No</th>
<th style="width:20%">ID</th>
<th style="width:10%">Name</th>
<th style="width:10%">E-mail</th>
<th style="width:20%">등록날짜</th>
<th style="width:20%">최종수정날짜</th>
<th ></th>
</tr>
	<c:forEach items="${userList }" var="userVO">
		<tr>
		<td>${userVO.useridx }</td>
		<td><a href="/user/userDetail?userid=${userVO.userid}">${userVO.userid }</a></td>
		<td>${userVO.username }</td>
		<td><span> ${userVO.useremail }</span> </td>
		<td><fmt:formatDate pattern="yyyy-MM-dd HH:mm" value="${userVO.userdatereg }" /> </td>
		<td><fmt:formatDate pattern="yyyy-MM-dd HH:mm" value="${userVO.userdatemod }" /> </td>
		<td>
			<a href="/user/userFormMod?userid=${userVO.userid}">수정</a>
			 | 
			<a href="/user/userDelete?userid=${userVO.userid}" onclick="if(!confirm('삭제 하시겠습니까?')) { return false; }" >삭제</a>
		</td>
		</tr>
	</c:forEach>
</table>

</body>
</html>

 

* userDetail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page session="false" %>
<html>
<head>
<title>회원</title>
</head>
<body>
<h1>회원 상세</h1>
    
<div>아이디 : ${userVO.userid }</div>

<div>이름 : ${userVO.username }</div>

<div>E-mail : ${userVO.useremail }</div>


<br><br>
<div><a href="/user/userList">회원목록</a></div>

</body>
</html>

 

* userForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="false" %>
<html>
<head>
<title>회원</title>
</head>
<body>
<h1>회원 등록</h1>
    
<form action="/user/userInsert" method="POST">
	<div>
		<label >아이디</label>
		<input type="text" name="userid" id="userid" placeholder = "회원 아이디">
	</div>
	<div>
		<label >암호</label>
		<input type="text" name="userpw" id="userpw" placeholder = "회원 암호">
	</div>
	<div>
		<label >이름</label>
		<input type="text" name="username" id="username" placeholder = "회원 이름">
	</div>
	<div>
		<label >E-mail</label>
		<input type="text" name="useremail" id="useremail" placeholder = "회원 E-mail">
	</div>
	
	<div>
		<button type="submit">확인</button>
	</div>            
	
</form>

</body>
</html>

 

* userFormMod.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="false" %>
<html>
<head>
<title>회원</title>
</head>
<body>
<h1>회원 정보 수정</h1>
    
<form action="/user/userUpdate" method="POST">
	<div>
		<label >아이디</label>
		<input type="text" name="userid" id="userid" value="${userVO.userid }" placeholder = "회원 아이디">
	</div>
	<div>
		<label >암호</label>
		<input type="text" name="userpw" id="userpw" value="${userVO.userpw }" placeholder = "회원 암호">
	</div>
	<div>
		<label >이름</label>
		<input type="text" name="username" id="username" value="${userVO.username }" placeholder = "회원 이름">
	</div>
	<div>
		<label >E-mail</label>
		<input type="text" name="useremail" id="useremail" value="${userVO.useremail }" placeholder = "회원 E-mail">
	</div>
	
	<div>
		<button type="submit">확인</button>
	</div>            
	
</form>

</body>
</html>