package com.myfutech.market.service.provider.config;
import com.myfutech.common.spring.jpa.base.impl.BaseJpaRepositoryImpl;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@ConditionalOnClass(HikariDataSource.class)
@EnableConfigurationProperties(ConnectionPoolProperties.class)
@EnableJpaRepositories(entityManagerFactoryRef = "marketServiceEntityManagerFactory",
transactionManagerRef = "marketServiceTransactionManager", basePackages="com.myfutech.market.service.provider.dao",
repositoryBaseClass = BaseJpaRepositoryImpl.class)
public class JpaConfig {
@Value("${marketService.url}")
private String url;
@Value("${marketService.username}")
private String username;
@Value("${marketService.password}")
private String password;
@Autowired
private ConnectionPoolProperties properties;
@Bean("marketServiceTransactionManager")
PlatformTransactionManager marketServiceTransactionManager() {
return new JpaTransactionManager(marketServiceEntityManagerFactory().getObject());
}
@Bean("marketServiceEntityManagerFactory")
LocalContainerEntityManagerFactoryBean marketServiceEntityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.jdbc.batch_size", "20");
properties.setProperty("current_session_context_class", "jpa");
properties.setProperty("hibernate.ejb.entitymanager_factory_name", "marketServiceEntityManagerFactory");
properties.setProperty("hibernate.hbm2ddl.auto", "none");
factoryBean.setJpaProperties(properties);
factoryBean.setDataSource(marketServiceDataSource());
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setPackagesToScan("com.myfutech.market.service.provider.model");
return factoryBean;
}
@Bean
JdbcTemplate initJdbcTemplate(){
return new JdbcTemplate(marketServiceDataSource());
}
@Bean("marketServiceDataSource")
DataSource marketServiceDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setJdbcUrl(url);
dataSource.setDriverClassName(properties.getDriverClass());
dataSource.addDataSourceProperty("cachePrepStmts", properties.isCachePrepStmts());
dataSource.addDataSourceProperty("prepStmtCacheSize", properties.getPrepStmtCacheSize());
dataSource.addDataSourceProperty("prepStmtCacheSqlLimit", properties.getPrepStmtCacheSqlLimit());
dataSource.addDataSourceProperty("useServerPrepStmts", properties.isUseServerPrepStmts());
dataSource.addDataSourceProperty("useLocalSessionState", properties.isUseLocalSessionState());
dataSource.addDataSourceProperty("rewriteBatchedStatements", properties.isRewriteBatchedStatements());
dataSource.addDataSourceProperty("cacheResultSetMetadata", properties.isCacheResultSetMetadata());
dataSource.addDataSourceProperty("cacheServerConfiguration", properties.isCacheServerConfiguration());
dataSource.addDataSourceProperty("elideSetAutoCommits", properties.isElideSetAutoCommits());
dataSource.addDataSourceProperty("maintainTimeStats", properties.isMaintainTimeStats());
return dataSource;
}
}
从别的地方扣来的,请问哪些是需要自己配置的啊大佬