Spring使用Hibernate和再加SpringData时配置的差别基于Java配置

时间:2021-10-25 20:35:41

只使用Spring+Hibernate

配置DataSource、LocalSessionFactoryBean、HibernateTransactionManager

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan({
"com.websystique.springmvc.configuration"}) //配置包名
@PropertySource(value
= {"classpath:application.properties"})
public class HibernateConfiguration {

@Autowired
private Environment environment;

private Properties hibernateProperties() {
Properties properties
= new Properties();
properties.put(
"hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put(
"hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put(
"hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put(
"hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty(
"jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty(
"jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty(
"jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty(
"jdbc.password"));
return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory
= new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
new String[]{"com.websystique.springmvc.model"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager
= new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}

 

 

使用Spring+Hibernate+SpringData

配置:DataSource、LocalContainerEntityManagerFactoryBean、JpaTransactionManager

 1 import org.springframework.beans.factory.annotation.Autowired;
2 import org.springframework.context.annotation.Bean;
3 import org.springframework.context.annotation.Configuration;
4 import org.springframework.context.annotation.PropertySource;
5 import org.springframework.core.annotation.Order;
6 import org.springframework.core.env.Environment;
7 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8 import org.springframework.jdbc.datasource.DriverManagerDataSource;
9 import org.springframework.orm.jpa.JpaTransactionManager;
10 import org.springframework.orm.jpa.JpaVendorAdapter;
11 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
13 import org.springframework.transaction.annotation.EnableTransactionManagement;
14
15 import javax.sql.DataSource;
16 import java.util.Properties;
17
18 @Configuration
19 @EnableTransactionManagement
20 @EnableJpaRepositories({"com.example.repository"}) //持久层包名
21 @PropertySource(value = {"classpath:application.properties"})
22 public class HibernateConfig {
23
24 @Autowired
25 private Environment environment;
26
27 private Properties hibernateProperties() {
28 Properties properties = new Properties();
29 properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
30 properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
31 properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
32 properties.put("hibernate.hbm2ddl.auto",environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
33 return properties;
34 }
35 @Bean
36 public DataSource dataSource() {
37 DriverManagerDataSource dataSource = new DriverManagerDataSource();
38 dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
39 dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
40 dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
41 dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
42 return dataSource;
43 }
44
45 @Bean
46 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
47 LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
48 entityManagerFactory.setDataSource(dataSource());
49 entityManagerFactory.setPackagesToScan(new String[] { "com.example.model" });
50 JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
51 entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
52 entityManagerFactory.setJpaProperties(hibernateProperties());
53 return entityManagerFactory;
54 }
55
56 @Bean
57 public JpaTransactionManager transactionManager() {
58 JpaTransactionManager transactionManager = new JpaTransactionManager();
59 transactionManager.setDataSource(dataSource());
60 transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
61 return transactionManager;
62 }
63
64 }