28. SQL 데이터베이스 작업 by sh
Spring Framework는 SQL 데이터베이스 작업을 광범위하게 지원합니다. JdbcTemplate
을 사용하는 직접 JDBC 액세스부터 Hibernate와 같은 완전한 '객체 관계형 매핑'기술까지. Spring Data는 인터페이스로부터 직접 Repository
구현을 생성하고 컨벤션을 사용하여 메서드 이름에서 쿼리를 생성하는 추가 수준의 기능을 제공합니다.
28.1 DataSource 구성
Java의 javax.sql.DataSource
인터페이스는 데이터베이스 연결을 작업하는 표준 방법을 제공합니다. 일반적으로 DataSource는 URL을 사용하여 일부 자격 증명을 사용해서 데이터베이스 연결을 설정합니다.
28.1.1 내장형 데이터베이스 지원
메모리 내장형 데이터베이스를 사용하여 응용 프로그램을 개발하는 경우에 종종 편리합니다. 분명히 메모리 내 데이터베이스는 영구 저장소를 제공하지 않습니다. 응용 프로그램이 시작될 때 데이터베이스를 채워야하고 응용 프로그램이 종료 될 때 데이터를 버릴 준비가 필요합니다.
'How-to'섹션에는 section on how to initialize a database이 포함되어 있습니다.
스프링 부트는 내장된 H2, HSQL및 Derby 데이터베이스를 자동 구성 할 수 있습니다. 연결 URL을 제공할 필요가 없으며 사용하려는 내장 데이터베이스에 빌드 종속성을 포함하기만 하면됩니다.
예를 들어, 일반적인 POM 종속성은 다음과 같습니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
내장 데이터베이스를 자동으로 구성하려면 spring-jdbc에 대한 종속성이 필요합니다. 이 예제에서 이것은 spring-boot-starter-data-jpa
를 통해 일시적으로 끌어 당깁니다.
28.1.2 프로덕션 데이터베이스 연결
프로덕션 데이터베이스 연결은 풀링 DataSource
를 사용하여 자동 구성 될 수도 있습니다. 특정 구현을 선택하기위한 알고리즘은 다음과 같습니다.
Tomcat이 성능과 동시성을 위해
DataSource
를 풀링하는 것을 선호하므로, 가능하다면 항상 그것을 선택합니다.HikariCP를 사용할 수 있다면 사용할 것입니다.
Commons DBCP를 사용할 수 있다면 이를 사용할 것이지만 프로덕션 환경에서는 권장하지 않습니다.
마지막으로, Commons DBCP2를 사용할 수 있다면 사용할 것입니다.
spring-boot-starter-jdbc
또는 spring-boot-starter-data-jpa
'starter POMs'를 사용하면 자동으로 tomcat-jdbc에 종속됩니다.
추가 연결 풀은 항상 수동으로 구성 할 수 있습니다. 자체 DataSource
bean을 정의하면 자동 구성이 발생하지 않습니다.
DataSource 구성은 spring.datasource.*
의 외부 구성 등록 정보에 의해 제어됩니다. 예를 들어, application.properties
에 다음 섹션을 선언 할 수 있습니다.
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
지원되는 옵션에 대한 자세한 내용은 DataSourceProperties
를 참조하십시오. spring.datasource.*
:를 통해 DataSource
구현 특정 속성을 구성 할 수 있습니다. 자세한 내용은 사용중인 연결 풀 구현 문서를 참조하십시오.
Spring Boot는 url
로부터 대부분의 데이터베이스를 위해 추론 할 수 있기 때문에 driver-class-name
을 지정할 필요가 없을 때가 많습니다.
Pooling DataSource
를 생성하기 위해서는 유효한 Driver 클래스가 사용 가능한지 확인할 수 있어야합니다. 그래서 우리는 무엇인가하기 전에 이를 확인합니다. spring.datasource.driverClassName=com.mysql.jdbc.Driver
를 설정하면 해당 클래스를 로드 할 수 있어야합니다.
28.1.3 JNDI DataSource 연결
Spring Boot 애플리케이션을 Application Server에 배포하는 경우 Application Server 내장 기능을 사용하여 DataSource를 구성 및 관리하고 JNDI를 사용하여 액세스 할 수 있습니다.
spring.datasource.jndi-name
속성은 spring.datasource.url
, spring.datasource.username
및 spring.datasource.password
속성의 대안으로 사용되어 특정 JNDI 위치에서 DataSource
에 액세스 할 수 있습니다. 예를 들어 application.properties
의 다음 섹션에서는 JBoss AS에 정의 된 DataSource에 액세스하는 방법을 보여줍니다.
spring.datasource.jndi-name=java:jboss/datasources/customers
28.2 JdbcTemplate 사용
Spring의 JdbcTemplate
클래스와 NamedParameterJdbcTemplate
클래스는 자동으로 구성되므로 직접 자신의 bean에 @Autowire
를 넣을 수 있습니다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final JdbcTemplate jdbcTemplate;
@Autowired
public MyBean(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// ...
}
28.3 JPA와 'Spring Data'
Java Persistence API는 객체를 관계형 데이터베이스에 '매핑'할 수있는 표준 기술입니다. spring-boot-starter-data-jpa
POM은 빠른 시작 방법을 제공합니다. 다음과 같은 주요 종속성을 제공합니다.
Hibernate - 가장 많이 사용되는 JPA 구현물 중 하나.
Spring Data JPA - JPA 기반 리포지토리를 쉽게 구현할 수 있습니다.
Spring ORMs - Spring Framework의 핵심 ORM 지원.


여기서 JPA 나 Spring Data에 대한 세부 사항을 다루지 않을 것입니다. spring.io 에서 'Accessing Data with JPA’가이드를 따르고 Spring Data JPA와 Hibernate 참조 문서를 읽을 수있습니다.
28.3.1 Entity 클래스들
전통적으로 JPA 'Entity'클래스는 persistence.xml
파일에 지정됩니다. Spring Boot에서는 이 파일이 필요 없으며 'Entity Scanning'이 사용됩니다. 기본적으로 기본 구성 클래스 (@EnableAutoConfiguration
또는 @SpringBootApplication
으로 주석 된 패키지) 아래의 모든 패키지가 검색됩니다.
@Entity
, @Embeddable
또는 @MappedSuperclass
로 주석 된 클래스가 고려됩니다. 일반적인 엔티티 클래스는 다음과 같습니다.
package com.example.myapp.domain;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class City implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
// ... additional members, often include @OneToMany mappings
protected City() {
// no-args constructor required by JPA spec
// this one is protected since it shouldn't be used directly
}
public City(String name, String state) {
this.name = name;
this.country = country;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
// ... etc
}
@EntityScan
주석을 사용하여 엔티티 스캐닝 위치를 사용자 정의 할 수 있습니다. 67.4 절. Section 67.4, “Separate @Entity definitions from Spring configuration” 방법을 보라.
28.3.2 Spring Data JPA 리포지토리들
스프링 데이터 JPA Repository는 데이터에 액세스하기 위해 정의 할 수있는 인터페이스입니다. JPA 쿼리는 메소드 이름에서 자동으로 작성됩니다. 예를 들어, CityRepository
인터페이스는 findAllByState(String state)
메소드를 선언하여 주어진 상태의 모든 도시를 찾을 수 있습니다.
좀 더 복잡한 쿼리의 경우 Spring Data의 Query
annotation을 사용하여 메소드에 주석을 달 수 있습니다.

Spring Data Repository는 대개 Repository
또는 CrudRepository
인터페이스에서 확장됩니다. 자동 구성을 사용하는 경우 기본 구성 클래스 (@EnableAutoConfiguration
또는 @SpringBootApplication
으로 주석 된 패키지)가 포함 된 패키지에서 저장소가 검색됩니다.
다음은 일반적인 Spring Data 저장소 입니다.
package com.example.myapp.domain;
import org.springframework.data.domain.*;
import org.springframework.data.repository.*;
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndCountryAllIgnoringCase(String name, String country);
}
Spring Data JPA의 표면을 간신히 긁어 모았습니다. 자세한 내용은 reference documentation.를 확인하십시오.
28.3.3 JPA 데이터베이스 작성 및 삭제
기본적으로 JPA 데이터베이스는 내장 데이터베이스 (H2, HSQL 또는 Derby)를 사용하는 경우에만 자동으로 작성됩니다. spring.jpa.*
속성을 사용하여 JPA 설정을 명시적으로 구성 할 수 있습니다. 예를 들어, 테이블을 작성하고 제거하려면 application.properties
에 다음을 추가 할 수 있습니다.
spring.jpa.hibernate.ddl-auto=create-drop
이것에 대한 Hibernate 자신의 내부 프로퍼티 이름은 hibernate.hbm2ddl.auto
입니다. spring.jpa.properties.*
(엔티티 관리자에 접두사를 추가하기 전에 접두사를 제거)를 사용하여 다른 Hibernate 기본 속성과 함께 이 속성을 설정할 수 있습니다. 예:
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
hibernate.globally_quoted_identifiers
를 Hibernate 엔티티 관리자에게 전달합니다.
기본적으로 DDL 실행 (또는 유효성 검사)은 ApplicationContext
가 시작될 때까지 연기됩니다. spring.jpa.generate-ddl
플래그도 있지만, ddl-auto 설정이 좀 더 세분화되어 있기 때문에 Hibernate autoconfig가 활성화되어 있다면 사용되지 않습니다.
Last updated
Was this helpful?