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