π Request λ°μ΄ν° DB Insert νκΈ° (2)
πΎ EX. SignUp νμκ°μ
β«οΈ PageController.java
@Controller
public class PageController {
@GetMapping("/auth/signup")
public String loadAuthSignup() {
return "auth/signup";
}
}
β«οΈ AuthController.java
@Slf4j
@RequiredArgsConstructor
@RestController
public class AuthController {
private final UserRepository userRepository;
// /api/auth/signup
@PostMapping("/api/auth/signup")
public ResponseEntity<?> signUp(UserSignUpReqDto userSignUpReqDto) {
// formData λ‘ λ€μ΄μ΄. -> HTML μμμ DTO λ‘ λ³νμ μ€νλ§μ΄ ν΄μ£Όλλ° κ·Έ λ Getter κ° νμν¨.
log.info("{}", userSignUpReqDto);
User user = userSignUpReqDto.toEntity();
log.info("λ§μ΄λ°ν°μ€ 보λ΄μ§κΈ° μ Entity: {}", user); // useGeneratedKeys λ₯Ό μ°λ©΄ Auto increment λ user code λ₯Ό λ°λ‘ κ°μ Έλ€ μΈ μ μκ² λλ κ²μ.
int result = userRepository.save(user);
log.info("λ§μ΄λ°ν°μ€ 보λ΄μ§κ³ λμ Entity: {}", user);
UserSignUpRespDto userSignUpRespDto = user.toDto();
if(result == 0) {
return ResponseEntity.internalServerError().body(new CMRespDto<>(-1, "νμ κ°μ
μ€λ₯", null));
}
return ResponseEntity.ok(new CMRespDto<>(1, "νμ κ°μ
μλ£", userSignUpRespDto));
}
}
β«οΈ User.java
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
private int user_code;
private String user_id;
private String user_password;
private String user_name;
private String user_email;
public UserSignUpRespDto toDto() {
return UserSignUpRespDto.builder()
.userCode(user_code)
.userId(user_id)
.userPassword(user_password)
.userName(user_name)
.userEmail(user_email)
.build();
}
}
β«οΈ CMRespDto.java
@Data
@AllArgsConstructor
public class CMRespDto<T> {
private int code;
private String msg;
private T data;
}
β«οΈ UserSignUpReqDto.java
@Data
public class UserSignUpReqDto {
private String userId;
private String userPassword;
private String userName;
private String userEmail;
public User toEntity() {
return User.builder()
.user_id(userId)
.user_password(userPassword)
.user_name(userName)
.user_email(userEmail)
.build();
}
}
β«οΈ UserSignUpRespDto.java
@Builder
@Data
public class UserSignUpRespDto {
private int userCode;
private String userId;
private String userPassword;
private String userName;
private String userEmail;
}
β«οΈ UserRepository.java
@Mapper
public interface UserRepository {
public int save(User user);
}
β«οΈ user.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.boot.mybatis20220923nk.repository.UserRepository">
<insert id="save"
parameterType="com.boot.mybatis20220923nk.domain.User"
useGeneratedKeys="true"
keyProperty="user_code"
>
<!-- select λΉΌκ³ insert update delete λ 리ν΄μ΄ μν₯λ°μ ν μ(int)λ‘ μ€μ ν΄μ€ νμ μμ. -->
insert into
user_mst
values (
0,
#{user_id},
#{user_password},
#{user_name},
#{user_email}
)
</insert>
</mapper>
β«οΈ signup.html
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<style>
...
</style>
</head>
<body>
<div id="container">
<form enctype="multipart/form-data">
<table>
<thead>
<tr>
<th colspan="2">
<h1>νμκ°μ
</h1>
</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<label for="user-id">μμ΄λ</label>
</th>
<td>
<input type="text" id="user-id" name="userId">
</td>
</tr>
<tr>
<th>
<label for="user-password">λΉλ°λ²νΈ</label>
</th>
<td>
<input type="password" id="user-password" name="userPassword">
</td>
</tr>
<tr>
<th>
<label for="user-name">μ΄λ¦</label>
</th>
<td>
<input type="text" id="user-name" name="userName">
</td>
</tr>
<tr>
<th>
<label for="user-email">μ΄λ©μΌ</label>
</th>
<td>
<input type="text" id="user-email" name="userEmail">
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="2">
<button type="button" class="signup-button">κ°μ
νκΈ°</button>
</th>
</tr>
</tfoot>
</table>
</form>
</div>
<script>
const signupButton = document.querySelector(".signup-button");
signupButton.onclick = () => {
request();
}
function getFormData() {
return new FormData(document.querySelector("form"));
}
function request() {
$.ajax({
async: false,
type: "post",
url: "/api/auth/signup",
enctype: "multipart/form-data",
contentType: false,
processData: false,
data: getFormData(),
dataType: "json",
success: (response) => {
alert(response.msg);
console.log(response);
},
error: (error) => {
console.log(error);
console.log(error.responseJSON.msg);
}
});
}
</script>
</body>
</html>
π¦
μ¬μ©μκ° /auth/signup μ£Όμλ‘ μ μνκ² λλ©΄,
PageController μμ html νλ©΄μ λμμ€.
μ¬μ©μκ° ν΄λΉ νΌμ λ§μΆμ΄ μ 보λ₯Ό μ
λ ₯νκ³ 'κ°μ
νκΈ°' λ²νΌμ λλ₯΄λ©΄,
μ
λ ₯ν μ 보λ₯Ό νΌ λ°μ΄ν° νμμΌλ‘ url /api/auth/signup μ ajaxλ‘ λ³΄λ΄κ² λλ€.
맀νλ AuthController μμ ν΄λΉ νΌ λ°μ΄ν°λ₯Ό UserSignUpReqDto λ‘ λ°λλ€. (μ€νλ§μμ DTOλ‘ λ³ννμ¬ λ°μμ€λλ‘ ν¨)
κ·ΈλΌ AuthController μμ DTO λ‘ λ°μμ¨ μ 보λ₯Ό User κ°μ²΄λ‘ λ³ννμ¬ UserRepository(μ€μ λ°μ΄ν°λ² μ΄μ€)μ User μ 보λ₯Ό Insert νλ€.
Insert κ° λμλμ§μ μ¬λΆλ₯Ό ν΄λΌμ΄μΈνΈμ RespDto λ‘ μλ΅νλ€.
π’ μκ° πΏ