2016-01-06 47 views
2

我回到了Java世界,我試圖用JPA,Hibernate和PostgreSQL配置一個新的Spring Web應用程序。不使用XML配置JPA/Hibernate/PostgreSQL

我發現很多具有各種XML配置文件的舊例子,我想知道是否有一種首選的新方法來執行此配置而不依賴於XML文件創作。

一些我需要配置的東西都是休眠SQL方言,司機等

回答

6

將下面的片段與@Configuration@EnableTransactionManagement

休眠/ JPA註釋的類(編輯packagesToScan字符串):

@Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 
    em.setDataSource(dataSource()); 
    em.setPackagesToScan(new String[] { "com.XY.model" }); 
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
    em.setJpaVendorAdapter(vendorAdapter); 
    em.setJpaProperties(additionalProperties()); 
    return em; 
} 

Properties additionalProperties() { 
    Properties properties = new Properties(); 
    properties.setProperty("hibernate.hbm2ddl.auto", "update"); 
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect"); 
    properties.setProperty("hibernate.show_sql", "true"); 
    return properties; 
} 

數據源(編輯用戶名,密碼和主機地址):

@Bean 
public DataSource dataSource() { 
    DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName("org.postgresql.Driver"); 
    dataSource.setUrl("jdbc:postgresql://localhost:port/DB_NAME"); 
    dataSource.setUsername("root"); 
    dataSource.setPassword(""); 
    return dataSource; 
} 

事務管理器:

@Bean 
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { 
    JpaTransactionManager transactionManager = new JpaTransactionManager(); 
    transactionManager.setEntityManagerFactory(emf); 
    return transactionManager; 
} 
+0

是從javax.sql中導入的數據源類? – BlakeH

+0

是的,它是javax.sql.DataSource。實現是org.springframework.jdbc.datasource.DriverManagerDataSource – Lukehey

+0

你能否儘快在這個問題上指導? HTTP://計算器。com/questions/35685804/how-to-make-database-configuration-persistence-xml-file-through/35686103#35686103 – Prateek

3

如果你要使用spring,我推薦使用Spring Boot它提供了許多自動配置。你可以使用一個application.properties配置dialect S和東東:

spring.datasource.url = <jdbcurl> 
spring.datasource.username = <username> 
spring.datasource.password = <password> 
spring.datasource.driver-class-name = org.postgresql.Driver 

春季啓動提供了大量的啓動優惠包,使易於jar添加到類路徑中。這些Starter Packages僅提供開發特定類型應用程序時可能需要的依賴關係。既然你正在開發一個需要數據訪問一個可能的Web應用程序,你應該把它們添加到您的pom.xml

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.1.RELEASE</version> 
</parent> 

<properties> 
     <java.version>1.8</java.version> 
</properties> 

<dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
     </dependency> 
</dependencies> 

基本上,春天開機試圖猜測你將如何來配置您的應用程序的基礎上,您添加的jar依賴項。提供以下關鍵依存關係:

  • Hibernate - 最流行的JPA實現之一。
  • Spring Data JPA - 可以輕鬆實現基於JPA的存儲庫。
  • Spring ORM - Spring框架的核心ORM支持。

您可以使用spring.jpa.*屬性顯式配置JPA設置。例如,創建和刪除您可以添加以下表格,您的application.properties

spring.jpa.hibernate.ddl-auto=create-drop 

你可以閱讀更多關於春天開機here

+0

感謝您的詳盡回覆。 application.properties是一個配置文件嗎? – BlakeH

+0

Yeap,spring引導允許你以多種方式配置你的應用程序,其中就是'application.properties'。您也可以使用YAML文件,環境變量和命令行參數來配置您的應用程序。 –

+1

再次感謝您的回答,我希望我可以將兩者都標記爲已接受。我將使用Lukehey的回答來解決當前的項目問題,並且可能會在下一個項目中利用您的方法進行比較。 – BlakeH