๐Spring Boot
๐์ํฐํ๋ผ์ด์ฆ(์ฃผ๋ก ๋๊ธฐ์
์ด๋ ๊ณต๊ณต๊ธฐ๊ด ๊ฐ์ ๋๊ท๋ชจ ์กฐ์ง์์ ์ฌ์ฉํ๋ ์ํํธ์จ์ด)๊ธ ์ ํ๋ฆฌ์ผ์ด์
์ ๊ฐ๋ฐํ๊ธฐ ์ํด์ ํ์ํ ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํ๋ ๊ฐ๋ฐ ๋๊ตฌ์.
1) Auto Configuration(์๋ ์ค์ )
์คํ๋ง๋ง์ ์ด์ฉํ๋ ๊ฒฝ์ฐ์ ๋น๊ตํ์์ ๋ ์ค์ ์์ฒด๋ ๋จ์ํจ.
2) ๋ด์ฅ ํฐ์ผ๊ณผ ๋จ๋
์คํ ๊ฐ๋ฅํ ๋๊ตฌ์.
3) ๋น ์ค์ ๋ณ๊ฒฝ(XML -> ์๋ฐ ์ค์ ์ด์ฉ)
4) JSP -> Thymeleaf ํ
ํ๋ฆฟ ์์ง ํ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์
5) Mybatis -> JPA ํ์ฉํ์ฌ ์์ ์ฒ๋ฆฌ
๐Spring Boot ์ค์
๊ณต๋ถํ ๋ ์ฌ์ฉํ๋ Dependency
Spring Boot DevTools
Lombok
Spring Web -> JSON ์ฌ์ฉ ๊ฐ๋ฅ!
Thymeleaf
Spring Data JPA
MariaDB Driver
src/main/resources/application.properties ํ์ผ ์ด์ฉ / application.ym-l(YAML) ํ์ผ ์ด์ฉ
ex) DB ๊ด๋ จ ์ค์ ์ถ๊ฐ(Spring Boot๋ HikariCP ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํจ)
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3333/webdb
spring.datasource.username=QWER
spring.datasource.password=CHODAN
๐๋ก๊ทธ ๋ ๋ฒจ์ ์ค์
์คํ๋ง ๋ถํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก Log4j2๊ฐ ์ถ๊ฐ๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ์ง ์๊ณ application.properties ํ์ผ์ ์ด์ฉํด์ ๊ฐ๋จํ๊ฒ ๋ก๊ทธ ์ค์ (info level)์ ์ถ๊ฐํ ์ ์์.
logging.level.org.springframework=info
logging.level.org.zerock=debug
DB ํ
์คํธ ์ฝ๋ ์์ฑ
package org.zerock.b01;
import lombok.Cleanup;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
@Log4j2
public class DataSourceTests {
@Autowired
private DataSource dataSource;
@Test
public void testDataSource() throws SQLException {
@Cleanup
Connection connection = dataSource.getConnection();
log.info(connection);
Assertions.assertNotNull(connection);
}
}
Spring Data JPA๋ฅผ ์ํ ์ค์
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
๐Spring Boot์์ ์น ๊ฐ๋ฐ
MVC ํจํด์ ๋ฐ๋ฅด๋ ๊ฒ์ ๋์ผํ๋ ์น ๊ด๋ จ ์ค์ ํ์ผ๋ค์ด ์๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ๋์ ํ๋ ํด๋์ค๋ฅผ ์์ฑํด์ค!
Thymeleaf๋ฅผ ์ด์ฉํด์ ๋ง๋ค ๋ ์์น๋ฅผ ์ฃผ์ํด์ ์์ฑํด์ผํจ.
์์น : main/resources/templatesํดํฐ ์์ ์์ฑํจ!
EX) ์ปจํธ๋กค๋ฌ๋ก ์ฟผ๋ฆฌ์คํธ๋ง ๊ฐ VIEW๋ก ์ ๋ฌํด์ ํ๋ฉด ๋ณด์ฌ์ฃผ๊ธฐ
ex) ์ปจํธ๋กค๋ฌ(hello)
package org.zerock.b01.controller;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Arrays;
import java.util.List;
@Controller
@Log4j2
public class SampleController {
@GetMapping("/hello")
public void hello(Model model) {
log.info("hello");
model.addAttribute("msg", "hello");
}
}
EX) View(hello.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
๊ฐ๋จํ๊ฒ JSONController ๋ง๋ค์ด๋ณด๊ธฐ
package org.zerock.b01.controller;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Log4j2
public class SampleJSONController {
@GetMapping("/helloArr")
public String[] helloArr(){
log.info("helloArr----------------");
return new String[]{"AAA", "BBB", "CCC"};
}
}