ref : https://chung-develop.tistory.com/12
์ด ๊ธ ์์๋๋ก ์งํํฉ๋๋ค
[SpringBoot]์คํ๋ง๋ถํธ ํ๋ก์ ํธ ์์ํ๊ธฐ
[SpringBoot]์คํ๋ง๋ถํธ ํ๋ก์ ํธ ์์ํ๊ธฐ ์๋ ํ์ธ์! ์ค๋์ SpringBoot๋ฅผ ์ด์ฉํ ๊ฒ์ํ ๋ง๋ค๊ธฐ ํ๋ก์ ํธ์ ์ฒซ ํฌ์คํ ์ ์์ํด๋ณผ๊น ํฉ๋๋ค. ๊ฐ๋ฐ์ ์ฌ์ฉ๋ ์์ ์ธ ๊ธฐ์ ์ ์๋์ ๊ฐ์ต๋๋ค. ๊ฐ๋ฐ
chung-develop.tistory.com
spring.io ํํ์ด์ง์์ ํ์ผ ์์ฑ
ํ์ผ์์ ์ค๋ฅธ์ชฝ ํด๋ฆญํ open as project๋ก ์ด๊ธฐ
โ๏ธ๋ฌด์กฐ๊ฑด build.gradle ํ๋ก์ ํธ๋ก openํ๊ธฐ
intellij ์๋จ์ File -> Open -> importํ ํ๋ก์ ํธ์ build.gradle ์ ํ ํ Open as Project ์ ํ
1. Open
2. build.gradle ์ ํ
3. Open as Project ์ ํ ๋!
โ๏ธ ctrl + alt + sํ gradle ๊ฒ์ํ ์๋ฐ 11๋ก ์ค์ ๋ง์ถฐ์ฃผ๊ธฐ, intellij
โ๏ธFile - Project Structure
SDK๊ณผ JDK์ ์๋ฐ ๋ฒ์ 11๋ก ๊ฐ๊ฐ ํ์ธํ๊ธฐ
โ๏ธ ๊ทธ ํ lombok์ด๋ ธํ ์ด์ ์ค๋ฅ๊ฐ ์๊ธธํ ๋ฐ ctrl + alt + s ์ ๋ ฅํ Enable annotation processing ๋๋ฅด๊ธฐ
์ฒ์์๋ build.gradle์์ jpa์ ๊ฐ์ ๋ค๋ฅธ implementation๋ค ์ฐ์ ์ฃผ์ ์ฒ๋ฆฌํ ํ
โป security๋ ๋ฌด์กฐ๊ฑด ์ฃผ์์ผ๋ก
์ฐจ๊ทผ์ฐจ๊ทผ ์ฃผ์ ํ๋ฉด์ ์ฝ๋ ์ถ๊ฐํด ๋๊ฐ๊ธฐ
์์ ref ์ฐธ๊ณ ํ๋ฉด
โ๏ธ Cannot resolve symbol 'springframework'
@Entity์ด๋ ธํ ์ด์ ์ค๋ฅ๊ฐ ์๊ธธ๋
build.gradle์์ jpa ํ์ฉํ๊ณ ์๋์ง ํ์ธํ๊ธฐ
can not resolve symbol์ค๋ฅ๊ฐ ์๊ฒผ์ ๋๋
์๋ ๋ธ๋ก๊ทธ ์ฐธ๊ณ ํ๊ธฐ
https://velog.io/@coastby/Spring
๐ค ์ง๋ฌธ
Database ํ ์ด๋ธ์ ์์ฑํ๊ณ ์์ userId, password๋ฑ๋ฑ์ ๋ฐ๋ก ์ ๋ ฅํด์ ํผ์ ์์ฑํด ์ฃผ์ง ์์๋
์๋์ผ๋ก jpa๊ฐ ์์ฑ์์ผ ์ฃผ๋์?
๊ทธ๋ ๋ค!
๋์ ์
spring.jpa.hibernate.ddl-auto=create
spring:
jpa:
hibernate:
ddl-auto: create
# show-sql: true # DDL ์ถ๋ ฅ
์๋ ์ฝ๋๋ค์ ๋ฌด ์กฐ ๊ฑด ์ถ๊ฐํด์ผ ํ๋ค
๊ตฌ์ฑ๋
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.10'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
//DB์ค์
runtimeOnly 'mysql:mysql-connector-java'
}
tasks.named('test') {
useJUnitPlatform()
}
application.yml
server:
address: localhost
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/instudy?serverTimezone=UTC
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: create
# show-sql: true # DDL ์ถ๋ ฅ
Controller - BoardController
package com.example.board.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class BoardController {
@GetMapping("/index2")
public String home(Model model) {
model.addAttribute("data", "๋ช๋ช
์ด์ ์ค์
จ์ด์?! ๋ฐ๊ฐ์ต๋๋ค ์๋!");
return "index2";
}
@GetMapping("/join")
public String login(){
return "join";
}
// @GetMapping("/join")
// public String login(){
// return "join";
// }
}
Controller - UserController
package com.example.board.controller;
import com.example.board.domain.UserVO;
import com.example.board.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/joinUs.do",method = RequestMethod.POST)
public String joinUs(UserVO vo){
userService.joinUser(vo);
return "index";
}
}
domain - UserVO
package com.example.board.domain;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Getter @Setter
public class UserVO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userSn;
private String userId;
private String password;
private String userName;
private String role;
public void setRole(String user) {
this.role = user;
}
}
Repository - UserRepository
package com.example.board.repository;
import com.example.board.domain.UserVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<UserVO, Long> {
}
Service - UserService
package com.example.board.service;
import com.example.board.domain.UserVO;
import com.example.board.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserRepository userRepository;
public void joinUser(UserVO vo) {
vo.setRole("USER");
userRepository.save(vo);
}
}
templates - index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>๋๋ง์ ๊ฒ์ํ ๋ง๋ค๊ธฐ!</title>
</head>
<body style="text-align: center"> ์๋
ํ์ธ์ ๊ฒ์ํ ์
๋๋ค.
<p>์ฒ์์ด์ ๊ฐ์? ๊ฒ์ํ ์ด์ฉ์ ์ฌ๊ธฐ์ ํด์ฃผ์ธ์!</p>
<input type="button" onclick="location.href='/join';" value="๊ฒ์ํ ์ด์ฉํ๊ธฐ">
</body>
</html>
templates - index2.html
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>๋๋ง์ ๊ฒ์ํ ๋ง๋ค๊ธฐ!</title>
</head>
<body style="text-align: center">์๋
ํ์ธ์ ๊ฒ์ํ ์
๋๋ค ๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์:)
<p th:text="${data}"></p>
<div>
<label for="userId">์์ด๋: </label>
<input id="userId" type="text">
</div>
<div>
<label for="password">ํจ์ค์๋: </label>
<input id="password" type="password">
</div>
<p>์ฒ์์ด์ ๊ฐ์? ํ์๊ฐ์
์ ์ฌ๊ธฐ์ ํด์ฃผ์ธ์!</p>
<input type="button" onclick="location.href='/join';" value="ํ์๊ฐ์
ํ๊ธฐ">
</body>
</html>
templates - join.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์๊ฐ์
ํ์ด์ง</title>
</head>
<body>
๊ณ์ ์ ๋ณด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์!
<form action="/joinUs.do" method="post">
<div>
<label for="userId">์์ด๋๊ฐ ๋ญ์์: </label>
<input id="userId" name="userId" type="text">
</div>
<div>
<label for="password">ํจ์ค์๋๊ฐ ๋ญ์์?!: </label>
<input id="password" name="password" type="password">
</div>
<div>
<label for="userName">๋๋ค์: </label>
<input id="userName" name="userName" type="text">
</div>
<input type="submit" value="๊ฐ์
๋ ์ธ ๊ธฐ๋ฆฟ">
</form>
</body>
</html>
localhost ์คํํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์์ ์๋ก๊ณ ์นจ ๋๋ฅด๊ณ ํ์ธํ๊ธฐ
'Project๐ธ > ์ด๊ธฐ ์ธํ ๐ฑ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JDBC๐ฑ] SpringBoot์ JDBC ์ฐ๋ํ๊ธฐ (0) | 2023.05.11 |
---|---|
[๊ธฐ๋ณธ๐ฑ] ์คํ๋ง ๋ฆฌ์กํธ ๊ฐ๋ฐ ํ๊ฒฝ ์ค์ (0) | 2023.04.16 |
[๊ธฐ๋ณธ๐ฑ] Spring Boot + React.js ๊ฐ๋ฐํ๊ฒฝ ์ฐ๋ํ๊ธฐ (0) | 2023.04.15 |
spring boot + react ํ๊ฒฝ์ ํ (0) | 2023.04.15 |