|
|
|
@ -1,11 +1,11 @@ |
|
|
|
|
package org.pqh.core.config; |
|
|
|
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource; |
|
|
|
|
import org.apache.commons.dbcp2.BasicDataSource; |
|
|
|
|
import org.pqh.core.util.LogManger; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.context.annotation.PropertySource; |
|
|
|
|
import org.springframework.orm.hibernate5.HibernateTemplate; |
|
|
|
|
import org.springframework.orm.hibernate5.HibernateTransactionManager; |
|
|
|
|
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; |
|
|
|
@ -19,14 +19,9 @@ import java.util.Properties; |
|
|
|
|
* Created by reborn on 2017/7/28. |
|
|
|
|
*/ |
|
|
|
|
@Configuration |
|
|
|
|
@PropertySource("classpath:config.properties") |
|
|
|
|
@EnableTransactionManagement |
|
|
|
|
public class HibernateConfig implements LogManger{ |
|
|
|
|
|
|
|
|
|
@Value("${hibernate.connection.pool_size}") |
|
|
|
|
private String pool_size; |
|
|
|
|
@Value("${hibernate.jdbc.fetch_size}") |
|
|
|
|
private String fetch_size; |
|
|
|
|
@Value("${hibernate.dialect}") |
|
|
|
|
private String dialect; |
|
|
|
|
@Value("${hibernate.connection.driver_class}") |
|
|
|
@ -37,6 +32,8 @@ public class HibernateConfig implements LogManger{ |
|
|
|
|
private String username; |
|
|
|
|
@Value("${hibernate.connection.password}") |
|
|
|
|
private String password; |
|
|
|
|
@Value("${sessionFactory.scan:org.pqh.core.model}") |
|
|
|
|
private String packagesToScan[]; |
|
|
|
|
|
|
|
|
|
private LocalSessionFactoryBean sessionFactoryBean; |
|
|
|
|
|
|
|
|
@ -49,16 +46,24 @@ public class HibernateConfig implements LogManger{ |
|
|
|
|
return dataSource; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public DruidDataSource druidDataSource(){ |
|
|
|
|
DruidDataSource dataSource=new DruidDataSource(); |
|
|
|
|
dataSource.setUrl(url); |
|
|
|
|
dataSource.setUsername(username); |
|
|
|
|
dataSource.setPassword(password); |
|
|
|
|
dataSource.setDriverClassName(driver); |
|
|
|
|
return dataSource; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
public void init() throws IOException { |
|
|
|
|
log.info("数据库配置初始化\nurl="+url+"\nusername="+username+"\npassword="+password); |
|
|
|
|
sessionFactoryBean=new LocalSessionFactoryBean(); |
|
|
|
|
Properties properties=new Properties(); |
|
|
|
|
properties.setProperty("hibernate.connection.pool_size",pool_size); |
|
|
|
|
properties.setProperty("hibernate.jdbc.fetch_size",fetch_size); |
|
|
|
|
properties.setProperty("hibernate.dialect",dialect); |
|
|
|
|
sessionFactoryBean.setDataSource(basicDataSource()); |
|
|
|
|
sessionFactoryBean.setPackagesToScan("org.pqh.core.model"); |
|
|
|
|
sessionFactoryBean.setHibernateProperties(properties); |
|
|
|
|
sessionFactoryBean.setDataSource(druidDataSource()); |
|
|
|
|
sessionFactoryBean.setPackagesToScan(packagesToScan); |
|
|
|
|
sessionFactoryBean.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|