diff --git a/build.gradle b/build.gradle index c568841..568b2fc 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,10 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' + // dbcp + implementation 'com.mysql:mysql-connector-j' + implementation 'org.apache.commons:commons-dbcp2:2.9.0' + //AWS s3 implementation(platform("software.amazon.awssdk:bom:2.27.5")) implementation("software.amazon.awssdk:s3") diff --git a/src/main/java/com/wefood/back/BackApplication.java b/src/main/java/com/wefood/back/BackApplication.java index 9c975bb..f8c09bc 100644 --- a/src/main/java/com/wefood/back/BackApplication.java +++ b/src/main/java/com/wefood/back/BackApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; @SpringBootApplication +@ConfigurationPropertiesScan public class BackApplication { public static void main(String[] args) { diff --git a/src/main/java/com/wefood/back/config/DatabaseConfig.java b/src/main/java/com/wefood/back/config/DatabaseConfig.java new file mode 100644 index 0000000..4dcd778 --- /dev/null +++ b/src/main/java/com/wefood/back/config/DatabaseConfig.java @@ -0,0 +1,97 @@ +package com.wefood.back.config; + +import jakarta.persistence.EntityManagerFactory; +import org.apache.commons.dbcp2.BasicDataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.Database; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; + +import javax.sql.DataSource; +import java.util.Properties; + +/** + * packageName : store.mybooks.resource.config + * fileName : DatabaseConfig + * author : Fiat_lux + * date : 2/20/24 + * description : + * =========================================================== + * DATE AUTHOR NOTE + * ----------------------------------------------------------- + * 2/20/24 Fiat_lux 최초 생성 + */ +@Configuration +public class DatabaseConfig { + private final DatabaseProperties databaseProperties; + + @Autowired + public DatabaseConfig(DatabaseProperties databaseProperties) { + this.databaseProperties = databaseProperties; + } + + @Bean + public DataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + + dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); + dataSource.setUrl(databaseProperties.getUrl()); + dataSource.setUsername(databaseProperties.getUserName()); + dataSource.setPassword(databaseProperties.getPassword()); + + dataSource.setInitialSize(databaseProperties.getInitialSize()); + dataSource.setMaxTotal(databaseProperties.getMaxTotal()); + dataSource.setMinIdle(databaseProperties.getMinIdle()); + dataSource.setMaxIdle(databaseProperties.getMaxIdle()); + + dataSource.setMaxWaitMillis(databaseProperties.getMaxWait()); + + dataSource.setTestOnBorrow(true); + dataSource.setTestOnReturn(true); + dataSource.setTestWhileIdle(true); + + return dataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { + LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); + emf.setDataSource(dataSource); + emf.setPackagesToScan("com.wefood.back"); + emf.setJpaVendorAdapter(jpaVendorAdapters()); + emf.setJpaProperties(jpaProperties()); + + return emf; + } + + private JpaVendorAdapter jpaVendorAdapters() { + HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); + hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); + return hibernateJpaVendorAdapter; + } + + private Properties jpaProperties() { + Properties jpaProperties = new Properties(); + jpaProperties.setProperty("hibernate.show_sql", "false"); + jpaProperties.setProperty("hibernate.format_sql", "true"); + jpaProperties.setProperty("hibernate.use_sql_comments", "true"); + jpaProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); + jpaProperties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false"); + + return jpaProperties; + } + + @Bean + public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory); + + return transactionManager; + } + +} \ No newline at end of file diff --git a/src/main/java/com/wefood/back/config/DatabaseProperties.java b/src/main/java/com/wefood/back/config/DatabaseProperties.java new file mode 100644 index 0000000..ba90f04 --- /dev/null +++ b/src/main/java/com/wefood/back/config/DatabaseProperties.java @@ -0,0 +1,20 @@ +package com.wefood.back.config; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + + +@ConfigurationProperties(prefix = "database.mysql") +@Getter +@Setter +public class DatabaseProperties { + private String url; + private String userName; + private String password; + private Integer initialSize; + private Integer maxTotal; + private Integer minIdle; + private Integer maxIdle; + private Integer maxWait; +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b026b43..47796e6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,10 +15,15 @@ cloud.aws.stack.auto-=false image.service.impl=s3Service -spring.datasource.url=${jdbc.url} -spring.datasource.username=${jdbc.username} -spring.datasource.password=${jdbc.password} -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +### mysql +database.mysql.url=${jdbc.url} +database.mysql.userName=${jdbc.username} +database.mysql.password=${jdbc.password} +database.mysql.initialSize=10 +database.mysql.maxTotal=10 +database.mysql.minIdle=10 +database.mysql.maxIdle=10 +database.mysql.maxWait=1000 spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=100MB \ No newline at end of file