inblog logo
|
jay0628
    SpringBoot

    [Spring Boot] 22. 스프링부트 뱅크 v1 (4) 기능 설계

    김주희's avatar
    김주희
    Mar 23, 2025
    [Spring Boot] 22. 스프링부트 뱅크 v1 (4) 기능 설계
    Contents
    1. 회원가입
    회원가입
    로그인
    계좌목록
    계좌생성
    계좌이체
    계좌상세
     
    1. @RequestParam == req.getParameter()
    1. DTO 클래스로 받아오기
    toString으로 호출되면서
    notion image

    1. 회원가입

    1. UserRequest

    package com.metacoding.bankv1.user; import lombok.Data; public class UserRequest { @Data public static class JoinDTO { private String username; private String password; private String fullname; } @Data public static class LoginDTO { private String username; private String password; } }
     

    2. UserController

    package com.metacoding.bankv1.user; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @RequiredArgsConstructor @Controller public class UserController { private final UserService userService; @GetMapping("/login-form") public String loginForm() { return "user/login-form"; } @GetMapping("/join-form") public String joinForm() { return "user/join-form"; } @PostMapping("/join") public String join(UserRequest.JoinDTO joinDTO) { // System.out.println(joinDTO); // toString()이 실행됨 userService.회원가입(joinDTO); // joinDTO 안에 username, password, fullname return "redirect:/login-form"; } }
     

    3. UserRepository

    package com.metacoding.bankv1.user; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; @RequiredArgsConstructor @Repository public class UserRepository { private final EntityManager em; public User findByUsername(String username) { Query query = em.createNativeQuery("select * from user_tb where username = ?", User.class); query.setParameter(1, username); try { return (User) query.getSingleResult(); } catch (Exception e) { return null; } } public void save(String username, String password, String fullname) { // client로부터 받은 것들 (주소 or body) Query query = em.createNativeQuery("insert into user_tb(username, password, fullname, created_at) values (?, ?, ?, now())"); query.setParameter(1, username); query.setParameter(2, password); query.setParameter(3, fullname); query.executeUpdate(); } }
     

    4. UserService

    package com.metacoding.bankv1.user; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @RequiredArgsConstructor @Service // 기능을 정리하면 여기에 전부 다 있다. public class UserService { private final UserRepository userRepository; @Transactional public void 회원가입(UserRequest.JoinDTO joinDTO) { // 1. 동일 유저 네임 있는지 검사 User user = userRepository.findByUsername(joinDTO.getUsername()); // 2. 있으면, exception 터트리기 (오류는 그 자리에서 터트려야 한다. 리턴X -> 따로 잡아채서 처리) if (user != null) throw new RuntimeException("동일한 username이 존재합니다."); // 3. 없으면 회원가입하기 userRepository.save(joinDTO.getUsername(), joinDTO.getPassword(), joinDTO.getFullname()); } }
     
     
     
    notion image
    notion image
     

    문제점

    fullname을 nullable=false 처리를 했지만 회원가입시 아무것도 입력하지 않을 경우 공백이 입력된다. → 원래는 다 처리해줘야되는데 지금은 X
    notion image
    notion image
    Share article

    jay0628

    RSS·Powered by Inblog