一個專案中hibernate框架下配置多個數據庫
阿新 • • 發佈:2019-01-26
hibernate框架下配置多個數據庫
當我們在同一個專案中需要同時連線多個數據庫時,在hibernate框架下的我們可以通過配置多個sessionFactory與多個連線池配置實現獲取不同資料庫的連線session物件。
初始化hibernate的sessionFactory
通過spring全註解方式初始化hibernate
配置如下:
@Bean
public DataSource druidDataSourceForMySQL() {
DruidDataSource database = new DruidDataSource();
database.setDriverClassName (environment.getProperty("jdbc.mysql.driverClassName"));
database.setUrl(environment.getProperty("jdbc.mysql.url"));
database.setUsername(environment.getProperty("jdbc.mysql.username"));
database.setPassword(environment.getProperty("jdbc.mysql.password"));
database.setDefaultAutoCommit (false);
database.setTimeBetweenEvictionRunsMillis(3600000);
database.setMinEvictableIdleTimeMillis(3600000);
database.setInitialSize(1);
return database;
}
@Bean(name = "sessionFactory")
@Profile("mysql_develop")
public LocalSessionFactoryBean sessionFactoryForMySQL() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// 設定資料來源
sessionFactory.setDataSource(druidDataSourceForMySQL());
// 設定entity所在包,用於自動掃描
sessionFactory.setPackagesToScan(new String[]{"com.hcicloud.sap.model"});
// 設定Hibernate的配置屬性
Properties props = new Properties();
props.setProperty("hibernate.dialect", environment.getProperty("hibernate.mysql.dialect"));
props.setProperty("hibernate.default_schema", environment.getProperty("hibernate.mysql.default_schema"));
props.setProperty("hibernate.show_sql", environment.getProperty("hibernate.show_sql"));
props.setProperty("hibernate.format_sql", environment.getProperty("hibernate.format_sql"));
props.setProperty("hibernate.hbm2ddl.auto", environment.getProperty("hibernate.hbm2ddl.auto"));
props.setProperty("hibernate.use_sql_comments", environment.getProperty("hibernate.use_sql_comments"));
props.setProperty("hibernate.connection.deferPrepares", "false");
props.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
props.setProperty("hibernate.jdbc.batch_size", environment.getProperty("hibernate.jdbc.batch_size"));
props.setProperty("hibernate.jdbc.fetch_size", environment.getProperty("hibernate.jdbc.fetch_size"));
// 強制Hibernate按照被更新資料的主鍵,為SQL更新排序。這麼做將減少在高併發系統中事務的死鎖。
props.setProperty("hibernate.order_updates", "true");
// session在事務完成後將被自動清洗(flush)
props.setProperty("hibernate.transaction.flush_before_completion", "true");
props.setProperty("hibernate.query.substitutions", environment.getProperty("hibernate.query.substitutions"));
props.setProperty("hibernate.default_batch_fetch_size", environment.getProperty("hibernate.default_batch_fetch_size"));
// 為單向關聯(一對一, 多對一)的外連線抓取(outer join fetch)樹設定最大深度. 值為0意味著將關閉預設的外連線抓取
props.setProperty("hibernate.max_fetch_depth", environment.getProperty("hibernate.max_fetch_depth"));
props.setProperty("hibernate.generate_statistics", environment.getProperty("hibernate.generate_statistics"));
props.setProperty("hibernate.bytecode.use_reflection_optimizer", environment.getProperty("hibernate.bytecode.use_reflection_optimizer"));
props.setProperty("hibernate.cache.use_second_level_cache", "true");
props.setProperty("hibernate.cache.use_query_cache", "true");
props.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.EhCacheRegionFactory");
props.setProperty("net.sf.ehcache.configurationResourceName", environment.getProperty("hibernate.net.sf.ehcache.configurationResourceName"));
props.setProperty("hibernate.cache.use_structured_entries", environment.getProperty("hibernate.cache.use_structured_entries"));
sessionFactory.setHibernateProperties(props);
return sessionFactory;
}
注入sessionFactory
通過如讓的配置我們獲取了指定連線池的sessionFactory
在我們編寫的hibernateDao中通過@Autowired與@Qualifier(byName)將sessionFactory進行載入獲取session物件
程式碼如下:
@Autowired
@Qualifier("sessionFactory")
public void setSessionFactory(final SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
獲取session物件
不同的資料庫配置不同的hibernateDao類,在操作不同的資料庫時,直接繼承指定的hibernateDao即可。
通過getSession方法獲取session物件進行查詢:
public Session getSession() {
return sessionFactory.getCurrentSession();
}
getSession().create... 獲取criteria 獲取HQL查詢物件 獲取sqlQuery物件