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

本文内容纲要:

- 先在类路径application.properties

- 只使用Spring+Hibernate配置

- 使用Spring+Hibernate+SpringData配置

先在类路径application.properties

1 jdbc.driverClassName = org.postgresql.Driver

2 jdbc.url = jdbc:postgresql://localhost:5432/数据库

3 jdbc.username = 用户名

4 jdbc.password = 密码

5

6 hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

7 hibernate.show_sql = true

8 hibernate.format_sql = true

9 hibernate.hbm2ddl.auto = update //自动更新数据库

只使用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 }

本文内容总结:先在类路径application.properties,只使用Spring+Hibernate配置,使用Spring+Hibernate+SpringData配置,

原文链接:https://www.cnblogs.com/woncode/p/7355340.html

以上是 基于Java配置Spring加Hibernate和再加SpringData时的差别 的全部内容, 来源链接: utcz.com/z/296484.html

回到顶部