contorller
package kr.co.gudi.controller;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 kr.co.gudi.dto.MemberDTO;
import kr.co.gudi.service.MemberService;
@Controller
public class MemberController {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired MemberService service;
@RequestMapping(value="/")
public String home() {
return "index";
}
@RequestMapping(value="/joinForm")
public String joinForm() {
return "joinForm";
}
@RequestMapping(value="/join", method = RequestMethod.POST)
public String join(@RequestParam HashMap<String, String> params, Model model) {
logger.info("params : "+params);
String msg = service.join(params);
model.addAttribute("msg", msg);
return "index";
}
@RequestMapping(value="/login", method = RequestMethod.POST)
public String login(Model model, HttpSession session,
@RequestParam String id, @RequestParam String pw) {
String page = "index";
logger.info(id+"/"+pw);
String loginId = service.login(id,pw);
logger.info("loginId : "+loginId);
if(loginId != null) {
session.setAttribute("loginId", loginId);
page = "redirect:/list";
}else {
model.addAttribute("msg", "");
}
return page;
}
@RequestMapping(value="/list")
public String list(Model model) {
ArrayList<MemberDTO> list = service.list();
model.addAttribute("list",list);
return "list";
}
@RequestMapping(value="/logout")
public String logout(HttpSession session) {
session.removeAttribute("loginId");
return "redirect:/";
}
@RequestMapping(value="/del")
public String del(@RequestParam String id) {
int row = service.del(id);
logger.info("�궘�젣�븳 媛��닔 : "+row);
return "redirect:/list";
}
@RequestMapping(value="/detail")
public String detail(@RequestParam String id, Model model) {
MemberDTO dto = service.detail(id);
model.addAttribute("member", dto);
return "detail";
}
@RequestMapping(value="/updateForm")
public String updateForm(Model model, @RequestParam String id) {
model.addAttribute("member", service.detail(id));
return "updateForm";
}
@RequestMapping(value="/update", method = RequestMethod.POST)
public String update(Model mode, @RequestParam HashMap<String, String> params) {
logger.info("params : "+params);
String page = "redirect:/updateForm?id="+params.get("id");
if(service.update(params)>0) {
page = "redirect:/detail?id="+params.get("id");
}
return page;
}
}
Service
package kr.co.gudi.service;
import java.util.ArrayList;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import kr.co.gudi.dao.MemberDAO;
import kr.co.gudi.dto.MemberDTO;
@Service
public class MemberService {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired MemberDAO dao;
public String join(HashMap<String, String> params) {
int row = dao.insert(params);
return row > 0 ? "회원가입에 성공했습니다.":"회원가입에 실패 했습니다.";
}
public String login(String id, String pw) {
return dao.login(id,pw);
}
public ArrayList<MemberDTO> list() {
return dao.list();
}
public int del(String id) {
return dao.del(id);
}
public MemberDTO detail(String id) {
return dao.detail(id);
}
public int update(HashMap<String, String> params) {
return dao.update(params);
}
}
DTO
package kr.co.gudi.dto;
public class MemberDTO {
private String id;
private String pw;
private String name;
private int age;
private String gender;
private String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
DAO
package kr.co.gudi.dao;
import java.util.ArrayList;
import java.util.HashMap;
import kr.co.gudi.dto.MemberDTO;
public interface MemberDAO {
int insert(HashMap<String, String> params);
String login(String id, String pw);
ArrayList<MemberDTO> list();
int del(String id);
MemberDTO detail(String id);
int update(HashMap<String, String> params);
}
mapper
<?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="kr.co.gudi.dao.MemberDAO">
<insert id="insert" parameterType="hashmap">
INSERT INTO member(id,pw,name,age,gender,email)
VALUES(#{id},#{pw},#{name},#{age},#{gender},#{email})
</insert>
<select id="login" resultType="String">
SELECT id FROM member WHERE
id = #{param1} AND pw = #{param2}
</select>
<select id="list" resultType="kr.co.gudi.dto.MemberDTO">
SELECT id, name, gender FROM member
</select>
<delete id="del">
DELETE FROM
member WHERE id = #{param1}
</delete>
<select id="detail" resultType="kr.co.gudi.dto.MemberDTO">
SELECT * FROM
member WHERE id = #{param1}
</select>
<update id="update" parameterType="hashmap">
UPDATE member SET
pw = #{pw}
,name = #{name}
,age = #{age}
,gender=#{gender}
,email=#{email}
WHERE id = #{id}
</update>
</mapper>
detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px 10px;
}
</style>
</head>
<body>
<jsp:include page="loginBox.jsp"/>
<table>
<tr>
<th>아이디</th>
<th>${member.id}</th>
</tr>
<tr>
<th>비밀번호</th>
<th>${member.pw}</th>
</tr>
<tr>
<th>이 름</th>
<th>${member.name}</th>
</tr>
<tr>
<th>나이</th>
<th>${member.age}</th>
</tr>
<tr>
<th>이메일</th>
<th>${member.email}</th>
</tr>
<tr>
<th>성별</th>
<th>${member.gender}</th>
</tr>
<tr>
<th colspan="2">
<input type="button" value="리스트" onclick="location.href='list'"/>
<input type="button" value="수정"
onclick="location.href='updateForm?id=${member.id}'"/>
</th>
</tr>
</table>
</body>
<script></script>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px 10px;
}
input[type="submit"]{
height: 50px;
}
</style>
</head>
<body>
<h2>LOGIN</h2>
<hr/>
<form action="login" method="post">
<table>
<tr>
<th>ID</th>
<th>
<input type="text" name="id" value="" placeholder="아이디를 입력 하세요"/>
</th>
<th rowspan="2">
<input type="submit" value="login"/>
</th>
</tr>
<tr>
<th>PW</th>
<th>
<input type="password" name="pw" value="" placeholder="비밀번호를 입력 하세요"/>
</th>
</tr>
<tr>
<th colspan="3">
<input id="regist" type="button" value="회원가입"/>
<input type="button" value="아이디/비번 찾기"/>
</th>
</tr>
</table>
</form>
</body>
<script>
$('#regist').on('click',function(){
location.href='joinForm';
});
var msg = "${msg}";
if(msg != ""){
alert(msg);
}
</script>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px 10px;
}
</style>
</head>
<body>
<form action="join" method="post">
<table>
<tr>
<th>아이디</th>
<th>
<input type="text" name="id"/>
</th>
</tr>
<tr>
<th>비밀번호</th>
<th>
<input type="password" name="pw"/>
</th>
</tr>
<tr>
<th>이 름</th>
<th>
<input type="text" name="name"/>
</th>
</tr>
<tr>
<th>나이</th>
<th>
<input type="text" name="age"/>
</th>
</tr>
<tr>
<th>이메일</th>
<th>
<input type="email" name="email"/>
</th>
</tr>
<tr>
<th>성별</th>
<th>
<input type="radio" name="gender" value="남"
checked/>남자
<input type="radio" name="gender" value="여"/>여자
</th>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="회원가입"/>
</th>
</tr>
</table>
</form>
</body>
<script></script>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px 10px;
}
</style>
</head>
<body>
<jsp:include page="loginBox.jsp"/>
<h3>회원리스트</h3>
<table>
<tr>
<th>id</th>
<th>name</th>
<th>gender</th>
<th>삭제</th>
</tr>
<c:forEach items="${list}" var="member">
<tr>
<td>${member.id}</td>
<td><a href="detail?id=${member.id}">${member.name}</a></td>
<td>${member.gender}</td>
<td><a href="del?id=${member.id}">삭제</a></td>
</tr>
</c:forEach>
</table>
</body>
<script>
var msg = "${msg}";
if(msg != ""){
alert(msg);
}
</script>
</html>
loginBox
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div id="login"></div>
<script>
var loginId = "${sessionScope.loginId}";
if(loginId == ""){
alert("로그인이 필요한 서비스 입니다.");
location.href = "./";
}else{
$("#login").html('안녕하세요 '+loginId+' 님 <a href="logout">로그아웃</a>');
}
</script>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px 10px;
}
</style>
</head>
<body>
<form action="update" method="post">
<table>
<tr>
<th>아이디</th>
<th>
<input type="text" name="id" readonly="readonly" value="${member.id}"/>
</th>
</tr>
<tr>
<th>비밀번호</th>
<th>
<input type="text" name="pw" value="${member.pw}"/>
</th>
</tr>
<tr>
<th>이 름</th>
<th>
<input type="text" name="name" value="${member.name}"/>
</th>
</tr>
<tr>
<th>나이</th>
<th>
<input type="text" name="age" value="${member.age}"/>
</th>
</tr>
<tr>
<th>이메일</th>
<th>
<input type="email" name="email" value="${member.email}"/>
</th>
</tr>
<tr>
<th>성별</th>
<th>
<input type="radio" name="gender" value="남"
<c:if test="${fn:contains(member.gender, '남')}">checked</c:if>
/>남자
<input type="radio" name="gender" value="여"
<c:if test="${fn:contains(member.gender, '여')}">checked</c:if>
/>여자
</th>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="수정"/>
</th>
</tr>
</table>
</form>
</body>
<script></script>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kr.co</groupId>
<artifactId>gudi</artifactId>
<name>10_Mybatis</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.3.14.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="kr.co.gudi" />
<beans:bean name="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<beans:property name="driverClassName" value="org.mariadb.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mariadb://localhost:3306/gdj70"/>
<beans:property name="username" value="web_user"/>
<beans:property name="password" value="pass"/>
</beans:bean>
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="datasource"/>
<beans:property name="mapperLocations" value="classpath:kr/co/gudi/dao/*.xml"/>
</beans:bean>
<mybatis-spring:scan base-package="kr.co.gudi.dao"/>
</beans:beans>