|
|
|
@ -3,17 +3,18 @@ package org.pqh.core.config; |
|
|
|
|
import org.apache.commons.dbcp2.BasicDataSource; |
|
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
|
|
import org.apache.logging.log4j.Logger; |
|
|
|
|
import org.pqh.core.util.SpringUtil; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.context.annotation.DependsOn; |
|
|
|
|
import org.springframework.context.annotation.PropertySource; |
|
|
|
|
import org.springframework.orm.hibernate5.HibernateTemplate; |
|
|
|
|
import org.springframework.orm.hibernate5.HibernateTransactionManager; |
|
|
|
|
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; |
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement; |
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Created by reborn on 2017/7/28. |
|
|
|
|
*/ |
|
|
|
@ -30,39 +31,40 @@ public class HibernateConfig{ |
|
|
|
|
private String username; |
|
|
|
|
@Value("${hibernate.connection.password}") |
|
|
|
|
private String password; |
|
|
|
|
@Value("${hibernate.connection.driver_class}") |
|
|
|
|
private String driver; |
|
|
|
|
|
|
|
|
|
private LocalSessionFactoryBean sessionFactoryBean; |
|
|
|
|
|
|
|
|
|
public BasicDataSource basicDataSource(){ |
|
|
|
|
BasicDataSource dataSource = new BasicDataSource(); |
|
|
|
|
dataSource.setUrl(url); |
|
|
|
|
dataSource.setUsername(username); |
|
|
|
|
dataSource.setPassword(password); |
|
|
|
|
dataSource.setUrl(url); |
|
|
|
|
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); |
|
|
|
|
dataSource.setDriverClassName(driver); |
|
|
|
|
return dataSource; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean("sessionFactoryBean") |
|
|
|
|
public LocalSessionFactoryBean sessionFactoryBean(){ |
|
|
|
|
LocalSessionFactoryBean sessionFactoryBean=new LocalSessionFactoryBean(); |
|
|
|
|
log.info("数据库配置初始化\nurl="+sessionFactoryBean.getHibernateProperties()+"\nusername="+username+"\npassword="+password); |
|
|
|
|
@PostConstruct |
|
|
|
|
public void init() throws IOException { |
|
|
|
|
log.info("数据库配置初始化\nurl="+url+"\nusername="+username+"\npassword="+password); |
|
|
|
|
sessionFactoryBean=new LocalSessionFactoryBean(); |
|
|
|
|
sessionFactoryBean.setDataSource(basicDataSource()); |
|
|
|
|
sessionFactoryBean.setPackagesToScan("org.pqh.core.model"); |
|
|
|
|
return sessionFactoryBean; |
|
|
|
|
sessionFactoryBean.afterPropertiesSet(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean("transactionManager") |
|
|
|
|
@DependsOn("sessionFactoryBean") |
|
|
|
|
public HibernateTransactionManager hibernateTransactionManager() { |
|
|
|
|
HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager(); |
|
|
|
|
hibernateTransactionManager.setSessionFactory(SpringUtil.getBean(LocalSessionFactoryBean.class).getObject()); |
|
|
|
|
hibernateTransactionManager.setSessionFactory(sessionFactoryBean.getObject()); |
|
|
|
|
return hibernateTransactionManager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Bean("hibernateTemplate") |
|
|
|
|
@DependsOn("sessionFactoryBean") |
|
|
|
|
public HibernateTemplate hibernateTemplate(){ |
|
|
|
|
HibernateTemplate hibernateTemplate=new HibernateTemplate(); |
|
|
|
|
hibernateTemplate.setSessionFactory(SpringUtil.getBean(LocalSessionFactoryBean.class).getObject()); |
|
|
|
|
hibernateTemplate.setSessionFactory(sessionFactoryBean.getObject()); |
|
|
|
|
return hibernateTemplate; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|