|
|
@ -1,4 +1,4 @@
|
|
|
|
package com.xit.core.config;
|
|
|
|
package com.xit.core.config.database;
|
|
|
|
|
|
|
|
|
|
|
|
import com.xit.core.support.CamelCaseLinkedMap;
|
|
|
|
import com.xit.core.support.CamelCaseLinkedMap;
|
|
|
|
import com.xit.core.support.CamelCaseMap;
|
|
|
|
import com.xit.core.support.CamelCaseMap;
|
|
|
@ -15,6 +15,9 @@ import org.mybatis.spring.SqlSessionFactoryBean;
|
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
|
|
|
|
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
|
|
|
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
@ -23,12 +26,16 @@ import org.springframework.context.ApplicationContext;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
|
|
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
|
|
|
|
|
|
import org.springframework.data.transaction.ChainedTransactionManager;
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|
|
|
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 org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
|
|
|
|
|
|
@ -38,6 +45,7 @@ import java.sql.Time;
|
|
|
|
import java.sql.Timestamp;
|
|
|
|
import java.sql.Timestamp;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
@ -67,10 +75,9 @@ public class DataSourceConfig {
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
@Bean
|
|
|
|
@Bean(name = "apiDataSource")
|
|
|
|
|
|
|
|
@ConfigurationProperties("spring.datasource.hikari")
|
|
|
|
@ConfigurationProperties("spring.datasource.hikari")
|
|
|
|
public DataSource apiDataSource() {
|
|
|
|
public DataSource dataSource() {
|
|
|
|
return DataSourceBuilder.create()
|
|
|
|
return DataSourceBuilder.create()
|
|
|
|
.type(HikariDataSource.class)
|
|
|
|
.type(HikariDataSource.class)
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
@ -79,54 +86,64 @@ public class DataSourceConfig {
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// JPA
|
|
|
|
// JPA
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
@Primary
|
|
|
|
@Bean
|
|
|
|
@Bean(name = "jpaProperties")
|
|
|
|
|
|
|
|
@ConfigurationProperties(prefix = "spring.jpa")
|
|
|
|
@ConfigurationProperties(prefix = "spring.jpa")
|
|
|
|
public JpaProperties jpaProperties() {
|
|
|
|
public JpaProperties jpaProperties() {
|
|
|
|
return new JpaProperties();
|
|
|
|
return new JpaProperties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
@Profile("!test")
|
|
|
|
@Bean(name = "entityManagerFactory")
|
|
|
|
@Bean
|
|
|
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
|
|
|
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${spring.jpa.properties.hibernate.hbm2ddl.auto}") String hbm2ddlAuto) {
|
|
|
|
EntityManagerFactoryBuilder builder,
|
|
|
|
// return builder
|
|
|
|
@Qualifier("apiDataSource") DataSource apiDataSource,
|
|
|
|
// .dataSource(dataSource)
|
|
|
|
@Qualifier("jpaProperties") JpaProperties jpaProperties
|
|
|
|
// .properties(jpaProperties.getProperties())
|
|
|
|
) {
|
|
|
|
// .packages(ENTITY_PACKAGES)
|
|
|
|
return builder
|
|
|
|
// .persistenceUnit("default")
|
|
|
|
.dataSource(apiDataSource)
|
|
|
|
// .build();
|
|
|
|
.properties(jpaProperties.getProperties())
|
|
|
|
|
|
|
|
.packages(ENTITY_PACKAGES)
|
|
|
|
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
|
|
.persistenceUnit("default")
|
|
|
|
vendorAdapter.setDatabasePlatform(jpaProperties().getDatabasePlatform());
|
|
|
|
.build();
|
|
|
|
vendorAdapter.setDatabase(jpaProperties().getDatabase());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HibernateProperties hibernateProperties = new HibernateProperties();
|
|
|
|
|
|
|
|
hibernateProperties.setDdlAuto(hbm2ddlAuto);
|
|
|
|
|
|
|
|
Map<String, Object> propMap = hibernateProperties.determineHibernateProperties(
|
|
|
|
|
|
|
|
jpaProperties().getProperties()
|
|
|
|
|
|
|
|
, new HibernateSettings());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
|
|
|
|
|
|
|
emf.setDataSource(dataSource());
|
|
|
|
|
|
|
|
emf.setPackagesToScan(ENTITY_PACKAGES);
|
|
|
|
|
|
|
|
emf.setPersistenceUnitName("default");
|
|
|
|
|
|
|
|
emf.setJpaVendorAdapter(vendorAdapter);
|
|
|
|
|
|
|
|
emf.setJpaPropertyMap(propMap); //jpaProperties().getProperties());
|
|
|
|
|
|
|
|
return emf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
@Primary
|
|
|
|
@Bean(name = "jpaTransactionManager")
|
|
|
|
@Bean
|
|
|
|
public PlatformTransactionManager jpaTransactionManager(
|
|
|
|
public PlatformTransactionManager jpaTransactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
|
|
|
|
@Qualifier(value = "entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
|
|
|
|
|
|
|
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
|
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
|
|
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
|
|
|
|
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
|
|
|
|
transactionManager.setNestedTransactionAllowed(true);
|
|
|
|
transactionManager.setNestedTransactionAllowed(true);
|
|
|
|
return transactionManager;
|
|
|
|
return transactionManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
|
|
|
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
|
|
|
return new PersistenceExceptionTranslationPostProcessor();
|
|
|
|
return new PersistenceExceptionTranslationPostProcessor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Mybatis
|
|
|
|
// Mybatis
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
@Bean(name="sqlSessionFactory")
|
|
|
|
@Bean
|
|
|
|
public SqlSessionFactory sqlSessionFactory(@Qualifier("apiDataSource") DataSource apiDataSource) throws Exception{
|
|
|
|
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
|
|
|
|
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
|
|
|
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
|
|
|
sessionFactory.setDataSource(apiDataSource);
|
|
|
|
sessionFactory.setDataSource(dataSource);
|
|
|
|
//sessionFactory.setConfiguration(mybatisConfiguration());
|
|
|
|
//sessionFactory.setConfiguration(mybatisConfiguration());
|
|
|
|
sessionFactory.setConfigLocation(applicationContext.getResource("classpath:/config/mybatis-config.xml"));
|
|
|
|
sessionFactory.setConfigLocation(applicationContext.getResource("classpath:/config/mybatis-config.xml"));
|
|
|
|
//sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
|
|
|
|
//sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
|
|
|
@ -134,14 +151,14 @@ public class DataSourceConfig {
|
|
|
|
return sessionFactory.getObject();
|
|
|
|
return sessionFactory.getObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean(name="sqlSessionTemplate")
|
|
|
|
@Bean
|
|
|
|
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory hikariSqlSessionFactory) {
|
|
|
|
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory hikariSqlSessionFactory) {
|
|
|
|
return new SqlSessionTemplate(hikariSqlSessionFactory);
|
|
|
|
return new SqlSessionTemplate(hikariSqlSessionFactory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean(name = "mybatisTransactionManager")
|
|
|
|
@Bean
|
|
|
|
public PlatformTransactionManager mybatisTransactionManager() {
|
|
|
|
public PlatformTransactionManager mybatisTransactionManager() {
|
|
|
|
DataSourceTransactionManager dstm = new DataSourceTransactionManager(apiDataSource());
|
|
|
|
DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource());
|
|
|
|
dstm.setGlobalRollbackOnParticipationFailure(false);
|
|
|
|
dstm.setGlobalRollbackOnParticipationFailure(false);
|
|
|
|
return dstm;
|
|
|
|
return dstm;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -160,7 +177,7 @@ public class DataSourceConfig {
|
|
|
|
conf.setMapUnderscoreToCamelCase(true);
|
|
|
|
conf.setMapUnderscoreToCamelCase(true);
|
|
|
|
conf.setLocalCacheScope(LocalCacheScope.SESSION); // SESSION / STATEMENT
|
|
|
|
conf.setLocalCacheScope(LocalCacheScope.SESSION); // SESSION / STATEMENT
|
|
|
|
conf.setJdbcTypeForNull(JdbcType.OTHER); // NULL / VARCHAR / OTHER 파라메터에 null 값이 있는경우 처리
|
|
|
|
conf.setJdbcTypeForNull(JdbcType.OTHER); // NULL / VARCHAR / OTHER 파라메터에 null 값이 있는경우 처리
|
|
|
|
conf.setLazyLoadTriggerMethods(new HashSet<String>(Arrays.asList("equals","clone","hashCode","toString")));
|
|
|
|
conf.setLazyLoadTriggerMethods(new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString")));
|
|
|
|
conf.setCallSettersOnNulls(true); // 조회시 null값 필드 set
|
|
|
|
conf.setCallSettersOnNulls(true); // 조회시 null값 필드 set
|
|
|
|
conf.setAggressiveLazyLoading(true);
|
|
|
|
conf.setAggressiveLazyLoading(true);
|
|
|
|
conf.setReturnInstanceForEmptyRow(true);
|
|
|
|
conf.setReturnInstanceForEmptyRow(true);
|
|
|
@ -186,6 +203,61 @@ public class DataSourceConfig {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ChainedTransactionManager : trsnsaction binding
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
|
|
|
// * jap & mybatis Transaction binding
|
|
|
|
|
|
|
|
// * @param entityManagerFactory LocalContainerEntityManagerFactoryBean
|
|
|
|
|
|
|
|
// * @return PlatformTransactionManager
|
|
|
|
|
|
|
|
// * @throws Exception Exception
|
|
|
|
|
|
|
|
// */
|
|
|
|
|
|
|
|
// @Bean
|
|
|
|
|
|
|
|
// public PlatformTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) throws Exception {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // JPA transactionManager
|
|
|
|
|
|
|
|
// JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
|
|
|
|
|
|
|
|
// jpaTransactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
|
|
|
|
|
|
|
|
// jpaTransactionManager.setNestedTransactionAllowed(true);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // MYBATIS transactionManager
|
|
|
|
|
|
|
|
// DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
|
|
|
|
|
|
|
|
// dataSourceTransactionManager.setDataSource(dataSource());
|
|
|
|
|
|
|
|
// dataSourceTransactionManager.setGlobalRollbackOnParticipationFailure(false);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // creates chained transaction manager
|
|
|
|
|
|
|
|
// return new ChainedTransactionManager(jpaTransactionManager, dataSourceTransactionManager);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Test
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Profile("test")
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public LocalContainerEntityManagerFactoryBean testManagerFactory(@Value("${spring.jpa.properties.hibernate.hbm2ddl.auto}") String hbm2ddlAuto) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
|
|
|
|
|
|
vendorAdapter.setDatabasePlatform(jpaProperties().getDatabasePlatform());
|
|
|
|
|
|
|
|
vendorAdapter.setDatabase(jpaProperties().getDatabase());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HibernateProperties hibernateProperties = new HibernateProperties();
|
|
|
|
|
|
|
|
hibernateProperties.setDdlAuto(hbm2ddlAuto);
|
|
|
|
|
|
|
|
Map<String, Object> propMap = hibernateProperties.determineHibernateProperties(
|
|
|
|
|
|
|
|
jpaProperties().getProperties()
|
|
|
|
|
|
|
|
, new HibernateSettings());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
|
|
|
|
|
|
|
emf.setDataSource(dataSource());
|
|
|
|
|
|
|
|
emf.setPackagesToScan(ENTITY_PACKAGES);
|
|
|
|
|
|
|
|
emf.setPersistenceUnitName("test");
|
|
|
|
|
|
|
|
emf.setJpaVendorAdapter(vendorAdapter);
|
|
|
|
|
|
|
|
emf.setJpaPropertyMap(propMap); //jpaProperties().getProperties());
|
|
|
|
|
|
|
|
return emf;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|