|
@ -0,0 +1,112 @@
|
|
|
package com.yihu.wlyy.figure.label.config.db;
|
|
|
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
import com.yihu.wlyy.figure.label.config.db.properties.DataSourceProperties;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
import org.springframework.orm.jpa.JpaVendorAdapter;
|
|
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
|
import javax.persistence.EntityManagerFactory;
|
|
|
import javax.sql.DataSource;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/4/6.
|
|
|
*/
|
|
|
@Configuration
|
|
|
@EnableTransactionManagement
|
|
|
@EnableJpaRepositories(
|
|
|
entityManagerFactoryRef = "wlyyEntityManagerFactory",
|
|
|
transactionManagerRef = "wlyyTransactionManager",
|
|
|
basePackages = {"com.yihu.wlyy.repository"}) //设置Repository所在位置
|
|
|
public class WlyyJpa {
|
|
|
|
|
|
@Autowired
|
|
|
private HibernateProperties hibernateProperties;
|
|
|
@Autowired
|
|
|
private DataSourceProperties dataSourceProperties;
|
|
|
@Autowired
|
|
|
private DruidConfig druidConfig;
|
|
|
|
|
|
|
|
|
@Bean(name = "wlyyDataSource")
|
|
|
@Primary
|
|
|
public DataSource wlyyDataSource() throws SQLException {
|
|
|
DruidDataSource datasource = new DruidDataSource();
|
|
|
datasource.setUrl(dataSourceProperties.getWlyy().getUrl());
|
|
|
datasource.setUsername(dataSourceProperties.getWlyy().getUsername());
|
|
|
datasource.setPassword(dataSourceProperties.getWlyy().getPassword());
|
|
|
datasource.setDriverClassName(dataSourceProperties.getDriverClassName());
|
|
|
//configuration
|
|
|
datasource.setInitialSize(dataSourceProperties.getInitialSize());
|
|
|
datasource.setMinIdle(dataSourceProperties.getMinIdle());
|
|
|
datasource.setMaxActive(dataSourceProperties.getMaxActive());
|
|
|
datasource.setMaxWait(dataSourceProperties.getMaxWait());
|
|
|
datasource.setTimeBetweenEvictionRunsMillis(dataSourceProperties.getTimeBetweenEvictionRunsMillis());
|
|
|
datasource.setMinEvictableIdleTimeMillis(dataSourceProperties.getMinEvictableIdleTimeMillis());
|
|
|
datasource.setValidationQuery(dataSourceProperties.getValidationQuery());
|
|
|
datasource.setTestWhileIdle(dataSourceProperties.getTestWhileIdle());
|
|
|
datasource.setTestOnBorrow(dataSourceProperties.getTestOnBorrow());
|
|
|
datasource.setTestOnReturn(dataSourceProperties.getTestOnReturn());
|
|
|
datasource.setPoolPreparedStatements(dataSourceProperties.getPoolPreparedStatements());
|
|
|
datasource.setMaxPoolPreparedStatementPerConnectionSize(dataSourceProperties.getMaxPoolPreparedStatementPerConnectionSize());
|
|
|
datasource.setRemoveAbandoned(dataSourceProperties.getRemoveAbandoned());
|
|
|
datasource.setRemoveAbandonedTimeout(dataSourceProperties.getRemoveAbandonedTimeout());
|
|
|
datasource.setLogAbandoned(dataSourceProperties.getLogAbandoned());
|
|
|
datasource.setFilters(dataSourceProperties.getFilters());
|
|
|
datasource.setConnectProperties(properties());//;# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
|
|
|
datasource.setUseGlobalDataSourceStat(true);// 合并多个DruidDataSource的监控数据
|
|
|
|
|
|
List proxyFilters = new ArrayList<>();
|
|
|
proxyFilters.add(druidConfig.statFilter());
|
|
|
datasource.setProxyFilters(proxyFilters);
|
|
|
return datasource;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Bean(name = "wlyyEntityManagerFactory")
|
|
|
@Primary
|
|
|
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(
|
|
|
@Qualifier("wlyyDataSource") DataSource dataSource) {
|
|
|
|
|
|
LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
|
|
|
emfb.setDataSource(dataSource);
|
|
|
emfb.setPackagesToScan("com.yihu.wlyy.entity");
|
|
|
emfb.setPersistenceUnitName("wlyy");
|
|
|
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
|
emfb.setJpaVendorAdapter(vendorAdapter);
|
|
|
emfb.setJpaProperties(hibernateProperties.hibProperties());
|
|
|
|
|
|
return emfb;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean(name = "wlyyTransactionManager")
|
|
|
@Primary
|
|
|
JpaTransactionManager transactionManagerSecondary(
|
|
|
@Qualifier("wlyyEntityManagerFactory") EntityManagerFactory builder) {
|
|
|
return new JpaTransactionManager(builder);
|
|
|
}
|
|
|
|
|
|
private Properties properties() {
|
|
|
Properties properties = new Properties();
|
|
|
properties.put("druid.stat.mergeSql", "true");
|
|
|
properties.put("slowSqlMillis", "1000");
|
|
|
return properties;
|
|
|
}
|
|
|
}
|