๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

์Šคํ”„๋ง JdbcTemplate

์Šคํ”„๋ง JdbcTemplate ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ƒ์„ฑ ์„ค์ •์€ Jdbc ์„ค์ •๊ณผ ๋™์ผํ•˜๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค. ์Šคํ”„๋ง Jdbc Data Access Layer ์ดํ•ด DataSource ๋ž€? DB์™€ ๊ด€๊ณ„๋œ Connection ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ, bean์œผ๋กœ ๋“ฑ๋กํ•˜์—ฌ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€๋‹ค. ์ด ๊ณผ์ •์„ ํ†ตํ•ด Spring์€ DataSource๋กœ DB์™€์˜ ์—ฐ๊ฒฐ์„ ํš๋“ํ•œ๋‹ค. DataSource ์„ค์ • ๋ฐ ๋นˆ ๋“ฑ๋ก๋ฐฉ๋ฒ• application.properties spring.datasource.url=jdbc:h2:tcp://localhost/~/test #url : db์˜ url์„ ๋„ฃ์–ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ connection์ด ๋จ. spring.datasource.driver-class-name=org.h2.Driver #h2๋ฅผ ์‚ฌ์šฉ ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ..

๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

์ˆœ์ˆ˜ Jdbc

ํ™˜๊ฒฝ์„ค์ • build.gradle ํŒŒ์ผ์— jdbc, h2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'com.h2database:h2' ์Šคํ”„๋ง ๋ถ€ํŠธ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ • ์ถ”๊ฐ€ resources/application.properties spring.datasource.url=jdbc:h2:tcp://localhost/~/test spring.datasource.driver-class-name=org.h2.Driver ์—ฌ๊ธฐ์„œ datasource๊ฐ€ ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋ก์ด ๋˜๊ณ  db ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. ์ด๊ฒƒ์„ ์šฐ๋ฆฌ๋Š” Injection ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค! Jdbc ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๊ตฌ..

๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

h2 database ์„ค์น˜ ๋ฐ ์—ฐ๊ฒฐ

H2 database ์„ค์น˜ https://www.h2database.com ๊ถŒํ•œ ์ฃผ๊ธฐ: chmod 755 h2.sh ์‹คํ–‰: ./h2.sh ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŒŒ์ผ ์ƒ์„ฑ ๋ฐฉ๋ฒ• jdbc:h2:~/test (์ตœ์ดˆ ํ•œ๋ฒˆ) ~/test.mv.db ํŒŒ์ผ ์ƒ์„ฑ ํ™•์ธ ์ดํ›„๋ถ€ํ„ฐ๋Š” jdbc:h2:tcp://localhost/~/test ์ด๋ ‡๊ฒŒ ์ ‘์† ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๊ธฐ ํ…Œ์ด๋ธ” ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— sql/ddl.sql ํŒŒ์ผ์„ ์ƒ์„ฑ drop table if exists member CASCADE; create table member ( id bigint generated by default as identity, name varchar(255), primary key (id) );ํ™˜๊ฒฝ์„ค์ • build.gradle ํŒŒ์ผ์— jdbc,..

๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

์•„์ฃผ ๊ฐ„๋‹จํ•œ CRUD ๊ฐœ๋ฐœ ์—ฐ์Šต

๊ฐœ๋ฐœํ™˜๊ฒฝ : IntelliJ IDEA , H2 DataBase, JAVA 11 ์Šคํ”„๋ง ๋ถ€ํŠธ ์Šคํƒ€ํ„ฐ ์‚ฌ์ดํŠธ์—์„œ ์Šคํ”„๋ง ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ https://start.spring.io ํ”„๋กœ์ ํŠธ ์„ ํƒ Project : Gradle Project Spring Boot : 2.3.x Language : Java Java : 11 Project Metadata groupId : hello artifactId: hello-spring Dependencies : Spring Web, Hateoas, H2 database H2 Database ์„ค์น˜ ๋ฐ ์„ค์ •๋ฐฉ๋ฒ• ๋ฅผ ์ฐธ๊ณ ํ•˜์ž ํ™˜๊ฒฝ์„ค์ • build.gradle ํŒŒ์ผ์— jdbc, h2 database ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ implementation 'org.springframework..

๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

[์›นMVC] ์Šคํ”„๋ง JdbcTemplate

์„ค์ •์€ ์ˆœ์ˆ˜ Jdbc์™€ ๋™์ผํ•œ ํ™˜๊ฒฝ์„ค์ •์„ ํ•˜๋ฉด ๋œ๋‹ค. ์Šคํ”„๋ง JdbcTemplate๊ณผ Mybatis ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” JDBC API์—์„œ ๋ณธ ๋ฐ˜๋ณต ์ฝ”๋“œ๋ฅผ ๋Œ€๋ถ€๋ถ„ ์ œ๊ฑฐํ•ด์ค€๋‹ค. ํ•˜์ง€๋งŒ SQL์€ ์ง์ ‘ ์ž‘์„ฑ ํ•ด์•ผ ํ•œ๋‹ค. ์Šคํ”„๋ง JdbcTemplate ํšŒ์› ๋ฆฌํฌ์ง€ํ† ๋ฆฌ package iseunghan.iseunghanspring.repository; import iseunghan.iseunghanspring.domain.Member; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParame..

๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ 

[์›นMVC] ์Šคํ”„๋ง ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ

์ˆœ์ˆ˜ JDBC ํ™˜๊ฒฝ ์„ค์ • build.gradle ํŒŒ์ผ์— jdbc, h2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'com.h2database:h2' ์Šคํ”„๋ง ๋ถ€ํŠธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ • ์ถ”๊ฐ€ resource/application.properties spring.datasource.url=jdbc:h2:tcp://localhost/~/test spring.datasource.driver-class-name=org.h2.Driver jdbc ๊ด€๋ จ ์ฝ”๋“œ ์ƒ๋žต.. (์ •์‹ ๊ฑด๊ฐ•์„ ์œ„ํ•ด ๋„˜์–ด๊ฐ„๋‹ค) ์Šคํ”„๋ง ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ ํšŒ์› ์„œ๋น„์Šค ์Šคํ”„๋ง ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ package iseunghan..

iseunghan
'๐Ÿ’ Spring/Spring MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก