If you're gonna use spring
, i recommend to use Spring Boot
which offers many auto configurations. you can use a application.properties
for configuring dialect
s and stuff:
spring.datasource.url = <jdbcurl>
spring.datasource.username = <username>
spring.datasource.password = <password>
spring.datasource.driver-class-name = org.postgresql.Driver
Spring Boot provides a number of Starter Packages that make easy to add jars to your classpath. These Starter Packages simply provide dependencies that you are likely to need when developing a specific type of application. Since you're developing a possibly web application that requires data access, you should add these to your 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>
Basically, spring boot tries to guess how you will want to configure your application, based on the jar dependencies that you have added. spring-boot-starter-data-jpa
, provides the following key dependencies:
- Hibernate — One of the most popular JPA implementations.
- Spring Data JPA — Makes it easy to implement JPA-based repositories.
- Spring ORMs — Core ORM support from the Spring Framework.
You can explicitly configure JPA settings using spring.jpa.*
properties. For example, to create and drop tables you can add the following to your application.properties
:
spring.jpa.hibernate.ddl-auto=create-drop
You can read more about spring boot here