Spring Boot[1]

BaeSeBinยท2025๋…„ 1์›” 24์ผ

Spring Boot

๋ชฉ๋ก ๋ณด๊ธฐ
1/1

๐Ÿ‚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 {

	// DataSource๋Š” application.properties์— ์„ค์ •๋œ DataSource๊ด€๋ จ ์„ค์ •์„ ํ†ตํ•ด
    // ์ƒ์„ฑ๋œ Bean์ž„
    @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"};

    }


}

0๊ฐœ์˜ ๋Œ“๊ธ€