1. UserRepository 에 save() 만들기
package shop.mtcoding.blog.user; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @RequiredArgsConstructor @Repository public class UserRepository { private final EntityManager em; @Transactional public User save(User user){ em.persist(user); return user; } public User findByUsernameAndPassword(UserRequest.LoginDTO reqDTO){ Query query = em.createQuery("select u from User u where u.username = :username and u.password =:password", User.class); query.setParameter("username", reqDTO.getUsername()); query.setParameter("password", reqDTO.getPassword()); return (User) query.getSingleResult(); } }
- saveV2() : 이 코드가 jpql을 사용하면 save()가 됨
@Transactional public User saveV2(String username, String password, String email){ Query q1 = em.createNativeQuery("insert into user_tb(username, password, email, created_at) values(?, ?, ?, now())"); q1.setParameter(1, username); q1.setParameter(2, password); q1.setParameter(3, email); q1.executeUpdate(); Query q2 = em.createNativeQuery("select * from user_tb where id = (select max(id) from user_tb)", User.class); User user = (User) q2.getSingleResult(); return user; }
2. UserRequest 에 JoinDTO 만들기
package shop.mtcoding.blog.user; import lombok.Data; public class UserRequest { @Data public static class JoinDTO { private String username; private String password; private String email; public User toEntity() { return User.builder() .username(username) .password(password) .email(email) .build(); } } @Data public static class LoginDTO { private String username; private String password; } }
3. UserController 에 join 만들기
package shop.mtcoding.blog.user; import jakarta.servlet.http.HttpSession; import lombok.NoArgsConstructor; 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 UserRepository userRepository; private final HttpSession session; @PostMapping("/join") public String join(UserRequest.JoinDTO reqDTO) { userRepository.save(reqDTO.toEntity()); return "redirect:/"; } @PostMapping("/login") public String login(UserRequest.LoginDTO reqDTO) { User sessionUser = userRepository.findByUsernameAndPassword(reqDTO); session.setAttribute("sessionUser", sessionUser); return "redirect:/"; } @GetMapping("/join-form") public String joinForm() { return "user/join-form"; } @GetMapping("/login-form") public String loginForm() { return "user/login-form"; } @GetMapping("/user/update-form") public String updateForm() { return "user/update-form"; } @GetMapping("/logout") public String logout() { session.invalidate(); return "redirect:/"; } }
4. 회원가입시 바로 로그인하기
package shop.mtcoding.blog.user; import jakarta.servlet.http.HttpSession; import lombok.NoArgsConstructor; 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 UserRepository userRepository; private final HttpSession session; @PostMapping("/join") public String join(UserRequest.JoinDTO reqDTO) { User sessionUser = userRepository.save(reqDTO.toEntity()); session.setAttribute("sessionUser", sessionUser); return "redirect:/"; } @PostMapping("/login") public String login(UserRequest.LoginDTO reqDTO) { User sessionUser = userRepository.findByUsernameAndPassword(reqDTO); session.setAttribute("sessionUser", sessionUser); return "redirect:/"; } @GetMapping("/join-form") public String joinForm() { return "user/join-form"; } @GetMapping("/login-form") public String loginForm() { return "user/login-form"; } @GetMapping("/user/update-form") public String updateForm() { return "user/update-form"; } @GetMapping("/logout") public String logout() { session.invalidate(); return "redirect:/"; } }
Share article