|
|
|
@ -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.CamelCaseMap;
|
|
|
|
@ -15,6 +15,9 @@ import org.mybatis.spring.SqlSessionFactoryBean;
|
|
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
|
|
|
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.context.properties.ConfigurationProperties;
|
|
|
|
|
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.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
|
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
|
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
|
|
|
import org.springframework.data.transaction.ChainedTransactionManager;
|
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
|
|
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.annotation.EnableTransactionManagement;
|
|
|
|
|
|
|
|
|
@ -38,6 +45,7 @@ import java.sql.Time;
|
|
|
|
|
import java.sql.Timestamp;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Configuration
|
|
|
|
@ -67,10 +75,9 @@ public class DataSourceConfig {
|
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
|
@Bean(name = "apiDataSource")
|
|
|
|
|
@Bean
|
|
|
|
|
@ConfigurationProperties("spring.datasource.hikari")
|
|
|
|
|
public DataSource apiDataSource() {
|
|
|
|
|
public DataSource dataSource() {
|
|
|
|
|
return DataSourceBuilder.create()
|
|
|
|
|
.type(HikariDataSource.class)
|
|
|
|
|
.build();
|
|
|
|
@ -79,54 +86,64 @@ public class DataSourceConfig {
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// JPA
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Primary
|
|
|
|
|
@Bean(name = "jpaProperties")
|
|
|
|
|
@Bean
|
|
|
|
|
@ConfigurationProperties(prefix = "spring.jpa")
|
|
|
|
|
public JpaProperties jpaProperties() {
|
|
|
|
|
return new JpaProperties();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
|
@Bean(name = "entityManagerFactory")
|
|
|
|
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
|
|
|
|
|
EntityManagerFactoryBuilder builder,
|
|
|
|
|
@Qualifier("apiDataSource") DataSource apiDataSource,
|
|
|
|
|
@Qualifier("jpaProperties") JpaProperties jpaProperties
|
|
|
|
|
) {
|
|
|
|
|
return builder
|
|
|
|
|
.dataSource(apiDataSource)
|
|
|
|
|
.properties(jpaProperties.getProperties())
|
|
|
|
|
.packages(ENTITY_PACKAGES)
|
|
|
|
|
.persistenceUnit("default")
|
|
|
|
|
.build();
|
|
|
|
|
@Profile("!test")
|
|
|
|
|
@Bean
|
|
|
|
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${spring.jpa.properties.hibernate.hbm2ddl.auto}") String hbm2ddlAuto) {
|
|
|
|
|
// return builder
|
|
|
|
|
// .dataSource(dataSource)
|
|
|
|
|
// .properties(jpaProperties.getProperties())
|
|
|
|
|
// .packages(ENTITY_PACKAGES)
|
|
|
|
|
// .persistenceUnit("default")
|
|
|
|
|
// .build();
|
|
|
|
|
|
|
|
|
|
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("default");
|
|
|
|
|
emf.setJpaVendorAdapter(vendorAdapter);
|
|
|
|
|
emf.setJpaPropertyMap(propMap); //jpaProperties().getProperties());
|
|
|
|
|
return emf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
|
@Bean(name = "jpaTransactionManager")
|
|
|
|
|
public PlatformTransactionManager jpaTransactionManager(
|
|
|
|
|
@Qualifier(value = "entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
|
|
|
|
|
@Bean
|
|
|
|
|
public PlatformTransactionManager jpaTransactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory) {
|
|
|
|
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
|
|
|
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
|
|
|
|
|
transactionManager.setNestedTransactionAllowed(true);
|
|
|
|
|
return transactionManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Primary
|
|
|
|
|
@Bean
|
|
|
|
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
|
|
|
|
|
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
|
|
|
|
return new PersistenceExceptionTranslationPostProcessor();
|
|
|
|
|
}
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Mybatis
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@Bean(name="sqlSessionFactory")
|
|
|
|
|
public SqlSessionFactory sqlSessionFactory(@Qualifier("apiDataSource") DataSource apiDataSource) throws Exception{
|
|
|
|
|
@Bean
|
|
|
|
|
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
|
|
|
|
|
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
|
|
|
|
sessionFactory.setDataSource(apiDataSource);
|
|
|
|
|
sessionFactory.setDataSource(dataSource);
|
|
|
|
|
//sessionFactory.setConfiguration(mybatisConfiguration());
|
|
|
|
|
sessionFactory.setConfigLocation(applicationContext.getResource("classpath:/config/mybatis-config.xml"));
|
|
|
|
|
//sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
|
|
|
|
@ -134,14 +151,14 @@ public class DataSourceConfig {
|
|
|
|
|
return sessionFactory.getObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean(name="sqlSessionTemplate")
|
|
|
|
|
@Bean
|
|
|
|
|
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory hikariSqlSessionFactory) {
|
|
|
|
|
return new SqlSessionTemplate(hikariSqlSessionFactory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean(name = "mybatisTransactionManager")
|
|
|
|
|
@Bean
|
|
|
|
|
public PlatformTransactionManager mybatisTransactionManager() {
|
|
|
|
|
DataSourceTransactionManager dstm = new DataSourceTransactionManager(apiDataSource());
|
|
|
|
|
DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource());
|
|
|
|
|
dstm.setGlobalRollbackOnParticipationFailure(false);
|
|
|
|
|
return dstm;
|
|
|
|
|
}
|
|
|
|
@ -160,7 +177,7 @@ public class DataSourceConfig {
|
|
|
|
|
conf.setMapUnderscoreToCamelCase(true);
|
|
|
|
|
conf.setLocalCacheScope(LocalCacheScope.SESSION); // SESSION / STATEMENT
|
|
|
|
|
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.setAggressiveLazyLoading(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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|