|
|
|
@ -12,7 +12,7 @@ import org.springframework.orm.hibernate5.HibernateTransactionManager; |
|
|
|
|
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; |
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -21,9 +21,9 @@ import java.util.Properties; |
|
|
|
|
@Configuration |
|
|
|
|
@PropertySource("classpath:config.properties") |
|
|
|
|
@EnableTransactionManagement |
|
|
|
|
public class HibernateConfig { |
|
|
|
|
public class HibernateConfig extends LocalSessionFactoryBean{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private LocalSessionFactoryBean sessionFactoryBean; |
|
|
|
|
private BasicDataSource dataSource; |
|
|
|
|
private Logger log= LogManager.getLogger(); |
|
|
|
|
|
|
|
|
@ -34,36 +34,34 @@ public class HibernateConfig { |
|
|
|
|
@Value("${dbcp2.password}") |
|
|
|
|
private String password; |
|
|
|
|
|
|
|
|
|
public HibernateConfig() { |
|
|
|
|
@PostConstruct |
|
|
|
|
private void init(){ |
|
|
|
|
log.info("数据库配置初始化\ndburl="+url+",dbcp2.username="+username+",dbcp2.password="+password); |
|
|
|
|
dataSource = new BasicDataSource(); |
|
|
|
|
dataSource.setUsername(username); |
|
|
|
|
dataSource.setPassword(password); |
|
|
|
|
dataSource.setUrl(url); |
|
|
|
|
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); |
|
|
|
|
sessionFactoryBean = new LocalSessionFactoryBean(); |
|
|
|
|
sessionFactoryBean.setDataSource(dataSource); |
|
|
|
|
this.setDataSource(dataSource); |
|
|
|
|
Properties properties = new Properties(); |
|
|
|
|
properties.setProperty("hibernate.connection.pool_size", "5"); |
|
|
|
|
properties.setProperty("hibernate.jdbc.fetch_size", "50"); |
|
|
|
|
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect"); |
|
|
|
|
sessionFactoryBean.setHibernateProperties(properties); |
|
|
|
|
sessionFactoryBean.setPackagesToScan("org.pqh.model"); |
|
|
|
|
try { |
|
|
|
|
sessionFactoryBean.afterPropertiesSet(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
this.setHibernateProperties(properties); |
|
|
|
|
this.setPackagesToScan("org.pqh.model"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean("transactionManager") |
|
|
|
|
public HibernateTransactionManager hibernateTransactionManager() { |
|
|
|
|
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); |
|
|
|
|
hibernateTransactionManager.setSessionFactory(this.sessionFactoryBean.getObject()); |
|
|
|
|
hibernateTransactionManager.setSessionFactory(this.getObject()); |
|
|
|
|
return hibernateTransactionManager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean(name = "hibernateTemplate") |
|
|
|
|
public HibernateTemplate hibernateTemplate() { |
|
|
|
|
HibernateTemplate hibernateTemplate = new HibernateTemplate(); |
|
|
|
|
hibernateTemplate.setSessionFactory(sessionFactoryBean.getObject()); |
|
|
|
|
hibernateTemplate.setSessionFactory(this.getObject()); |
|
|
|
|
return hibernateTemplate; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|