- 설계도면만들기 -> DTO만들기 -> 화면 만들기 -> 컨트롤러 만들기
파일명 Boot20220328Application.java
package com.example.boot_20220328;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
// 컨트롤러, 환경파일, 서비스(mybatis 1번전용)
@ComponentScan(basePackages = {
"com.example.controller",
"com.example.service",
"com.example.config"
})
// Mapper(mybaris 2버전용)
@MapperScan(basePackages = {"com.example.mapper"})
public class Boot20220328Application {
public static void main(String[] args) {
SpringApplication.run(Boot20220328Application.class, args);
}
}
파일명 AddressDTO.java
package com.example.dto;
import java.util.Date;
import lombok.Data;
@Data
public class AddressDTO {
// 주소코드
private Long ucode;
// 전체주소
private String uaddress;
// 우편번호
private String upostcode;
// 등록일
private Date uregdate;
// 이메일
private String uemail;
// 대표주소
private Long uchk;
}
파일명 home.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>home</title>
</head>
<body style="padding: 10px;">
<div>
<h3>home화면</h3>
<p th:text="${session.SESSION_EMAIL}"></p>
<hr />
<h3>회원</h3>
<div th:if="${session.M_EMAIL == null}">
<a th:href="@{/member/join}">회원 가입</a>
<a th:href="@{/member/login}">회원 로그인</a>
</div>
<div th:if="${session.M_EMAIL != null}">
<p th:text="|${session.M_NAME} 님 로그인|"></p>
<form th:action="@{/member/logout}" method="post">
<input type="submit" value="로그아웃">
</form>
<a th:href="@{/member/address}">주소관리</a>
</div>
<hr />
<h3>판매자</h3>
<div th:if="${session.SESSION_EMAIL == null}">
<a th:href="@{/seller/select}">판매자 로그인</a>
<a th:href="@{/seller/insert}">판매자 등록</a>
</div>
<div th:if="${session.SESSION_EMAIL != null}">
<a th:href="@{/seller/logout}">판매자 로그아웃</a>
<a th:href="@{/item/insert}">판매자 물품 등록</a>
<a th:href="@{/item/selectlist}">판매자 물품 목록</a>
</div>
<hr />
<h3>판매자</h3>
<a th:href="@{/seller/selectlist}">판매자 목록(운영자용)</a>
<hr />
</div>
</body>
</html>
파일명 join.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>회원가입</title>
</head>
<body style="padding: 10px;">
<h3>회원 가입 페이지 입니다.</h3>
<hr />
<div style="padding: 20px;">
<form th:action="@{/member/joinaction}" method="post">
<label style="width:75px; height: 30px; display:inline-block;">이메일 : </label>
<input type="text" placeholder="이메일" name="uemail"/></br>
<label style="width:75px; height: 30px; display:inline-block;">암호 : </label>
<input type="password" placeholder="암호" name="upw"/></br>
<label style="width:75px; height: 30px; display:inline-block;">암호확인 : </label>
<input type="password" placeholder="암호확인" name="upw1"/></br>
<label style="width:75px; height: 30px; display:inline-block;">이름 : </label>
<input type="text" placeholder="이름" name="uname"/></br>
<label style="width:75px; height: 30px; display:inline-block;">연락처 : </label>
<input type="text" placeholder="연락처" name="uphone"/></br>
<label style="width:75px; height: 30px; display:inline-block;"></label>
<input type="submit" value="회원가입" />
<a th:href="@{/home}">홈으로</a>
</form>
</div>
</body>
</html>
파일명 login.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로그인</title>
</head>
<body style="padding: 10px;">
<h3>회원 로그인 페이지 입니다.</h3>
<hr />
<div style="padding: 20px;">
<form th:action="@{/member/loginaction}" method="post">
<label style="width:75px; height: 30px; display:inline-block;">이메일 : </label>
<input type="text" placeholder="이메일" name="uemail"/></br>
<label style="width:75px; height: 30px; display:inline-block;">암호 : </label>
<input type="password" placeholder="암호" name="upw"/></br>
<label style="width:75px; height: 30px; display:inline-block;"></label>
<input type="submit" value="로그인" />
<a th:href="@{/home}">홈으로</a>
<a th:href="@{/member/join}">회원가입</a>
</form>
</div>
</body>
</html>
파일명 address.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>주소관리</title>
</head>
<body style="padding: 10px;">
<h3>주소관리 페이지 입니다.</h3>
<hr />
<h3>주소등록</h3>
<div style="padding: 20px;">
<form th:action="@{/member/addressaction}" method="post">
<label style="width:75px; height: 30px; display:inline-block;">우편번호 : </label>
<input type="text" placeholder="우편번호" name="upostcode"/></br>
<label style="width:75px; height: 30px; display:inline-block;">주소 : </label>
<input type="text" placeholder="주소" name="uaddress"/></br>
<label style="width:75px; height: 30px; display:inline-block;"></label>
<input type="submit" value="주소등록" />
<a th:href="@{/home}">홈으로</a>
</form>
<hr />
<h3>주소 목록</h3>
<table border="1">
<tr>
<th>번호</th>
<th>주소코드</th>
<th>우편번호</th>
<th>주소</th>
<th>등록일</th>
<th>대표주소</th>
<th>버튼</th>
</tr>
<tr th:each="obj, idx :${addr}">
<td th:text="${idx.count}"></td>
<td th:text="${obj.ucode}"></td>
<td th:text="${obj.upostcode}"></td>
<td th:text="${obj.uaddress}"></td>
<td th:text="${obj.uregdate}"></td>
<td th:text="${obj.uchk}"></td>
<td>
<form th:action="@{/member/setaddr(code=${obj.ucode})}" method="post">
<input type="hidden" name="code" th:value="${obj.ucode}" />
<input type="submit" value="대표주소설정" />
</form>
<a th:href="@{/member/updateaddr(code=${obj.ucode})}">수정</a>
<form th:action="@{/member/deleteaddr}" method="post">
<input type="hidden" name="code" th:value="${obj.ucode}" />
<input type="submit" value="삭제" />
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
파일명 updateaddr.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>주소수정</title>
</head>
<body style="padding: 10px;">
<h3>주소수정 페이지 입니다.</h3>
<hr />
<h3>주소등록</h3>
<div style="padding: 20px;">
<form th:action="@{/member/updateaddraction}" method="post">
<input type="hidden" name="ucode" th:value="${addr.ucode}">
<label style="width:75px; height: 30px; display:inline-block;">우편번호 : </label>
<input type="text" placeholder="우편번호" th:value="${addr.upostcode}" name="upostcode"/></br>
<label style="width:75px; height: 30px; display:inline-block;">주소 : </label>
<input type="text" placeholder="주소" th:value="${addr.uaddress}" name="uaddress"/></br>
<label style="width:75px; height: 30px; display:inline-block;"></label>
<input type="submit" value="주소수정" />
<a th:href="@{/member/address}">주소관리</a>
</form>
</div>
</body>
</html>
파일명 MemberController.java
package com.example.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import com.example.dto.AddressDTO;
import com.example.dto.MemberDTO;
import com.example.mapper.AddressMapper;
import com.example.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value = "/member")
public class MemberController {
@Autowired MemberMapper mMapper;
@Autowired AddressMapper maMapper;
@Autowired HttpSession httpSession;
// 주소 삭제
@PostMapping(value = "/deleteaddr")
public String deleteaddrPOST(
@RequestParam(name = "code")long code
){
String em = (String)httpSession.getAttribute("M_EMAIL");
maMapper.deleteMemberAaddrOne(code, em);
return "redirect:/member/address";
}
// 주소 수정
// 127.0.0.1:8080/ROOT/member/updateaddr?code=1
@GetMapping(value = "/updateaddr")
public String updateaddrGET(
Model model,
@RequestParam(name = "code")long code
){
String em = (String)httpSession.getAttribute("M_EMAIL");
if(em != null){
AddressDTO addr = maMapper.selectAddressOne(code, em);
model.addAttribute("addr", addr);
// model.addAttribute("addr", maMapper.selectAddressOne(code, em));
return "/member/updateaddr";
}
return "redirect:/member/login";
}
@PostMapping(value = "/updateaddraction")
public String updateaddrPOST(
@ModelAttribute AddressDTO addr
){
String em = (String)httpSession.getAttribute("M_EMAIL");
// 주소, 우편번호, 코드 받음
// 이메일
addr.setUemail(em);
//System.out.println("html에서 받은 값 : " + addr.toString());
int ret = maMapper.updateMemberAddrOne(addr);
//System.out.println(ret);
if(ret == 1 ){
return "redirect:/member/address";
}
return "redirect:/member/address";
}
@PostMapping(value = "/setaddr")
public String setaddrPOST(
@RequestParam(name = "code") long code
){
String em = (String)httpSession.getAttribute("M_EMAIL");
String ro = (String)httpSession.getAttribute("M_ROLE");
if(em !=null){ // 로그인 되었는지
if(ro.equals("CUSTOMER")){ // 권한이 정확한지
int ret = maMapper.updateAddressSet(em, code);
// System.out.println(ret);
if(ret == 1){
return "redirect:/member/address";
}
}
}
return "/member/address";
}
@GetMapping(value = "/address")
public String addrGET( Model model){
String em = (String)httpSession.getAttribute("M_EMAIL");
String ro = (String)httpSession.getAttribute("M_ROLE");
if(em !=null){ // 로그인 되었는지
if(ro.equals("CUSTOMER")){ // 권한이 정확한지
// 화면에 표시하기
// view 전송해야 됨
List<AddressDTO> list = maMapper.selectAddressList(em);
model.addAttribute("addr", list);
return "/member/address";
}
return "redirect:/address";
}
return "redirect:/member/login";
}
@PostMapping(value = "/addressaction")
public String addrPOST(
@ModelAttribute AddressDTO addr
){
String em = (String)httpSession.getAttribute("M_EMAIL");
String ro = (String)httpSession.getAttribute("M_ROLE");
if(em !=null){
if(ro.equals("CUSTOMER")){
addr.setUemail(em);
// System.out.println(addr.toString());
int ret = maMapper.insertAddress(addr);
// System.out.println(ret);
if(ret == 1){
return "redirect:/member/address";
}
}
}
return "redirect:/member/login";
}
@GetMapping(value = "/login")
public String loginGET(){
return "member/login";
}
@PostMapping(value = "/loginaction")
public String loginactionPOST(
@RequestParam(name = "uemail") String em,
@RequestParam(name = "upw") String pw
){
System.out.println("uemail em : " + em);
System.out.println("upw pw : " + pw);
MemberDTO member = mMapper.memberLogin(em, pw);
if(member != null){
System.out.println(member.toString());
httpSession.setAttribute("M_EMAIL", member.getUemail());
httpSession.setAttribute("M_NAME", member.getUname());
httpSession.setAttribute("M_ROLE", member.getUrole());
return "redirect:/";
}
return "redirect:/member/login";
}
@PostMapping(value = "/logout")
public String logoutPOST(){
httpSession.invalidate();
return "redirect:/";
}
@GetMapping(value = "/join")
public String joinGET(){
return "/member/join";
}
@PostMapping(value = "joinaction")
public String joinactionPOST(
// ModelAttribute : DTO의 컬럼명과 html name값이 일치해야 정보가 저장됨
@ModelAttribute MemberDTO member
// // RequestParam : 정보를 하나씩 받아서 저장
// @RequestParam(name = "uemail")String em,
// @RequestParam(name = "upw")String pw,
// @RequestParam(name = "uname")String na,
// @RequestParam(name = "uphone")String ph
){
// // 1. view에서 전송된 값 확인용
// System.out.println("uemail em : " + em);
// System.out.println("upw pw : " + pw);
// System.out.println("uname na : " + na);
// System.out.println("uphone ph : " + ph);
// // 2. dto로 객체화 시켜 저장함
// MemberDTO member = new MemberDTO();
// member.setUemail(em);
// member.setUpw(pw);
// member.setUname(na);
// member.setUphone(ph);
// 필요한 부분을 수동으로 채움
member.setUrole("CUSTOMER");
// 3. 객체의 값이 잘 들어갔는지 확인
System.out.println(member.toString());
int ret = mMapper.memberJoin(member);
if(ret == 1){
// redirect는 주소를 변경한후에 엔터키를 누른 효과
return "redirect:/";
}
return "redirect:/member/join";
// templates의 member폴거의 join을 표시(render)
// render은 GET에서만 사용해야 함.
// return "/member/join";
}
}
파일명 MemberMapper.java
package com.example.mapper;
import com.example.dto.MemberDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface MemberMapper {
// 회원가입
// INSERT INTO 테이블명(컬럼명들) VALUES(추가할 값들)
@Insert({
"INSERT INTO MEMBER( UEMAIL, UPW, UNAME, UPHONE, UROLE, UREGDATE )",
" VALUES( #{ obj.uemail }, #{ obj.upw }, #{ obj.uname }",
" , #{ obj.uphone }, #{ obj.urole }, CURRENT_DATE ) "
})
public int memberJoin( @Param(value = "obj") MemberDTO member);
// 로그인
// SELECT 컬럼명들 FROM 테이블명 WHERE 조건 AND 조건
@Select({
"SELECT UEMAIL, UNAME, UROLE FROM MEMBER WHERE UEMAIL = #{email} AND UPW = #{pw}"
})
public MemberDTO memberLogin(
@Param(value = "email") String em,
@Param(value = "pw") String userpw);
}
파일명 AddressMapper.java
package com.example.mapper;
import java.util.List;
import com.example.dto.AddressDTO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface AddressMapper {
// INSERT INTO 테이블명(컬럼명들) VALUES(추가할 값들)
@Insert({" INSERT INTO MEMBERADDR( UCODE, UADDRESS, UPOSTCODE, UEMAIL ) ",
" VALUES( SEQ_MEMBERADDR_UCODE.NEXTVAL, #{obj.uaddress}, #{obj.upostcode}, #{obj.uemail} ) "})
public int insertAddress(@Param(value = "obj") AddressDTO addr);
// SELECT * FROM 테이블명 WHERE 조건 AND 조건
@Select({"SELECT * FROM MEMBERADDR WHERE UEMAIL = #{em}",
" ORDER BY UCHK DESC"
})
public List<AddressDTO> selectAddressList( @Param(value = "em") String email );
// 1. SELECT MAX(UCHK) FROM MEMBERADDR WHERE UEMAIL = 'aaa'
// 2. UPDATE 테이블명 SET 변경할 컬럼명 = 변경값(1에서 구한 값 +1) WHERE 조건
@Update({
"UPDATE MEMBERADDR SET UCHK =(SELECT MAX(UCHK) FROM MEMBERADDR WHERE UEMAIL = #{email}) +1 ",
" WHERE UCODE = #{code}"
})
public int updateAddressSet(
@Param(value = "email") String email, // 현재 사용자의 이메일
@Param(value = "code") long code// 변경하고자 하는 코드변호
);
// 1개의 주소 정보 가져오기
// SELECT * FROM MEMBERADDR WHERE 조건
@Select({
"SELECT UCODE, UADDRESS, UPOSTCODE FROM MEMBERADDR",
" WHERE UCODE = #{cd} AND UEMAIL = #{em}"
})
public AddressDTO selectAddressOne(
@Param(value = "cd")long ucode,
@Param(value = "em")String uemail
);
// 1개의 주소를 수정하기
// UPDATE 테이블명 SET 변경컬럼명 = 변경값 WHERE 조건
@Update({"UPDATE MEMBERADDR SET UPOSTCODE = #{obj.upostcode}, UADDRESS = #{obj.uaddress} ",
" WHERE UCODE = #{obj.ucode} AND UEMAIL = #{obj.uemail}"})
public int updateMemberAddrOne( @Param(value = "obj")AddressDTO addr );
// 1개 주소 삭제
// DELETE FROM 테이블명 WHERE 조건
@Delete({
"DELETE FROM MEMBERADDR WHERE UCODE= #{cd} AND UEMAIL =#{em}"
})
public int deleteMemberAaddrOne(
@Param(value = "cd")long ucode,
@Param(value = "em")String uemail
);
}