You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.4 KiB
112 lines
3.4 KiB
package db.config;
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.hibernate.SessionFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
|
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.sql.DataSource;
|
|
import java.io.IOException;
|
|
import java.util.Properties;
|
|
|
|
/**
|
|
* Created by reborn on 2017/7/28.
|
|
*/
|
|
public class HibernateConfig{
|
|
|
|
private Logger log=LogManager.getLogger();
|
|
|
|
@Value("${hibernate.dialect}")
|
|
private String dialect;
|
|
@Value("${hibernate.connection.driver_class}")
|
|
private String driver;
|
|
|
|
@Value("${hibernate.connection.url}")
|
|
private String url;
|
|
@Value("${hibernate.connection.username}")
|
|
private String username;
|
|
@Value("${hibernate.connection.password}")
|
|
private String password;
|
|
|
|
@Value("${hibernate.connection.url2}")
|
|
private String url2;
|
|
@Value("${hibernate.connection.username2}")
|
|
private String username2;
|
|
@Value("${hibernate.connection.password2}")
|
|
private String password2;
|
|
|
|
@Value("${sessionFactory.scan}")
|
|
private String packagesToScan[];
|
|
|
|
|
|
|
|
private LocalSessionFactoryBean aliyun = new LocalSessionFactoryBean();
|
|
|
|
private LocalSessionFactoryBean huaWeiCloud = new LocalSessionFactoryBean();
|
|
|
|
private DruidDataSource dataSource(String url,String username,String password){
|
|
DruidDataSource dataSource=new DruidDataSource();
|
|
dataSource.setUrl(url);
|
|
dataSource.setUsername(username);
|
|
dataSource.setPassword(password);
|
|
dataSource.setDriverClassName(driver);
|
|
log.info("数据源初始化\nurl="+url+"\nusername="+username+"\npassword="+password);
|
|
return dataSource;
|
|
}
|
|
|
|
|
|
|
|
@PostConstruct
|
|
private void init() {
|
|
initSessionFactory(aliyun,dataSource(url,username,password));
|
|
initSessionFactory(huaWeiCloud,dataSource(url2,username2,password2));
|
|
}
|
|
|
|
private void initSessionFactory(LocalSessionFactoryBean bean, DataSource dataSource){
|
|
Properties properties=new Properties();
|
|
properties.setProperty("hibernate.dialect",dialect);
|
|
properties.setProperty("hibernate.format_sql","true");
|
|
bean.setHibernateProperties(properties);
|
|
bean.setDataSource(dataSource);
|
|
bean.setPackagesToScan(packagesToScan);
|
|
try {
|
|
bean.afterPropertiesSet();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Bean("aliyun")
|
|
public SessionFactory aliyun(){
|
|
return aliyun.getObject();
|
|
}
|
|
|
|
|
|
@Bean("huaWeiCloud")
|
|
public SessionFactory huaWeiCloud(){
|
|
return huaWeiCloud.getObject();
|
|
}
|
|
|
|
@Bean("transactionManager1")
|
|
public HibernateTransactionManager aliyunTransaction() {
|
|
HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager();
|
|
hibernateTransactionManager.setSessionFactory(aliyun());
|
|
return hibernateTransactionManager;
|
|
}
|
|
|
|
@Bean("transactionManager2")
|
|
public HibernateTransactionManager huaweiTransaction() {
|
|
HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager();
|
|
hibernateTransactionManager.setSessionFactory(huaWeiCloud());
|
|
return hibernateTransactionManager;
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|