Skip to content

Commit 3ea52e4

Browse files
committed
feat: JPA 프로젝트 환경 세팅
1 parent 0df5dd5 commit 3ea52e4

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.sehee.weeklyjpa.config;
2+
3+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
4+
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
9+
import org.springframework.orm.jpa.JpaTransactionManager;
10+
import org.springframework.orm.jpa.JpaVendorAdapter;
11+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12+
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
13+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
16+
import javax.sql.DataSource;
17+
import java.util.Properties;
18+
19+
@Configuration
20+
@EnableJpaRepositories("com.sehee.weeklyjpa.domain")
21+
public class DataSourceConfig {
22+
@Bean
23+
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
24+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
25+
dataSource.setDriverClassName(dataSourceProperties.determineDriverClassName());
26+
dataSource.setUrl(dataSourceProperties.getUrl());
27+
dataSource.setUsername(dataSourceProperties.getUsername());
28+
dataSource.setPassword(dataSourceProperties.getPassword());
29+
30+
return dataSource;
31+
}
32+
33+
@Bean
34+
public JpaVendorAdapter jpaVendorAdapter(JpaProperties jpaProperties) {
35+
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
36+
adapter.setShowSql(jpaProperties.isShowSql());
37+
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
38+
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
39+
40+
return adapter;
41+
}
42+
43+
@Bean
44+
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
45+
DataSource dataSource,
46+
JpaVendorAdapter jpaVendorAdapter,
47+
JpaProperties jpaProperties
48+
) {
49+
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
50+
em.setDataSource(dataSource);
51+
em.setPackagesToScan("com.sehee.weeklyjpa.domain");
52+
em.setJpaVendorAdapter(jpaVendorAdapter);
53+
54+
Properties properties = new Properties();
55+
properties.putAll(jpaProperties.getProperties());
56+
em.setJpaProperties(properties);
57+
58+
return em;
59+
}
60+
61+
@Bean
62+
public PlatformTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
63+
JpaTransactionManager transactionManager = new JpaTransactionManager();
64+
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
65+
66+
return transactionManager;
67+
}
68+
}

src/main/resources/application.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ spring:
44
url: jdbc:mysql://localhost:3308/weeklyjpa
55
username: root
66
password: root
7+
driver-class-name: "com.mysql.cj.jdbc.Driver"
78
jpa:
89
open-in-view: false
910
hibernate:

0 commit comments

Comments
 (0)