0. DB 생성하기
※ Mysql workbench를 사용할때 datatype DATETIME()하면 에러가 남. DATETIME이라고 작성해주면 됨.
1. domain 패키지에 User.java 생성
package com.tnqls.spring.domain;
import java.util.Date;
public class User {
private String username;
private String userid;
private String password;
private Date generated_date;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getuserid() {
return userid;
}
public void setuserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getGenerated_date() {
return generated_date;
}
public void setGenerated_date(Date generated_date) {
this.generated_date = generated_date;
}
}
※ mapper에 대해 더 이해하고 싶어서 DB 컬럼은 id, User 객체에는 userid로 변수 선언
STS getter setter 단축키 : Alt + Shift + S -> R
2. src/main/webapp/WEB-INF/views에 signup.jsp 작성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Signup</title>
</head>
<body>
<h1>
회원가입
</h1>
<form action="" method="post">
<p>
<label for="userId">아이디</label>
<input type="text" id="userId" name="userid"/>
</P>
<p>
<label for="userName">이름</label>
<input type="text" id="userName" name="username"/>
</P>
<p>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password"/>
</P>
<p>
<label for="password2">비밀번호 확인</label>
<input type="password" id="password2" name="password2"/>
</P>
<button type="submit">회원가입하기</button>
<p><a href="/board/login">로그인하러가기</a></p>
</form>
<P> 현재시각은 ${serverTime}. </P>
</body>
</html>
※ input칸의 name이 User 객체의 컬럼명과 동일해야함
3. mapper 패키지에 UserMapper.java 작성
package com.tnqls.spring.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.tnqls.spring.domain.User;
@Repository
public interface UserMapper {
void createUser(User user);
User getUserById(String id);
List<User> getUserAll = null;
void updateUser(User user);
void removeUserById(String id);
}
많은 함수중에 createUser만 사용할 것임
4. mappers 폴더에 userMapper.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.tnqls.spring.mapper.UserMapper">
<!-- 회원가입 -->
<insert id="createUser">
INSERT INTO user(id, username, password) VALUES(#{userid}, #{username},#{password})
</insert>
</mapper>
※ VALUES안에 내용은 User 객체의 컬럼명과 동일해야함
5. service 패키지에 UserService.java 생성
package com.tnqls.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tnqls.spring.domain.User;
import com.tnqls.spring.mapper.UserMapper;
@Service
public class UserService {
@Autowired UserMapper userMapper;
public void createUser(User user) {
userMapper.createUser(user);
}
}
6. HomeController에 연결
@Autowired
UserService userService;
@RequestMapping(value = "/signup", method = RequestMethod.GET)
public String getsignup(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "signup";
}
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String postsignup(User user) {
userService.createUser(user);
logger.info("user sign up!");
return "redirect:/";
}
위는 회원가입 페이지를 띄울 때! (GET)
아래는 회원가입 페이지 폼을 작성하고 제출할 때! (POST)
redirect를 통해 "/"으로 이동
'개발 > Spring' 카테고리의 다른 글
[Spring/게시판] 4. 로그인 / 로그아웃 (0) | 2020.05.05 |
---|---|
[Spring/게시판] 3.1 회원가입 추가기능 (0) | 2020.04.25 |
[Spring/게시판] 2. Mybatis 연동하기 (0) | 2020.04.23 |
[Spring/게시판] 1. MySql 연동 (0) | 2020.04.23 |
최근댓글