Create a new spring project with database config
Here's a step-by-step guide to creating a new Spring Boot project with a database configuration:
Prerequisites:
- Java 8 or later installed on your machine
- Spring Boot 2.3.0 or later
- A code editor or IDE of your choice (e.g., Eclipse, IntelliJ IDEA, Visual Studio Code)
Step 1: Create a new Spring Boot project
- Open your code editor or IDE and create a new project.
- Choose "Spring Boot" as the project type and select the "Web" and "Java" options.
- Name your project (e.g., "spring-boot-database-example").
- Choose the project location and package name (e.g., "com.example").
Step 2: Add dependencies
- In the
pom.xml
file (if you're using Maven) orbuild.gradle
file (if you're using Gradle), add the following dependencies:<!-- Maven --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies>
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' runtimeOnly 'com.h2database:h2' }
These dependencies include:
* `spring-boot-starter-web` for web-related functionality
* `spring-boot-starter-data-jpa` for JPA (Java Persistence API) support
* `h2` for an in-memory database
**Step 3: Create a database configuration class**
* Create a new Java class in the `com.example` package (or the package you chose earlier):
```java
// com/example/DatabaseConfig.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories
public class DatabaseConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create()
.driverClassName("org.h2.Driver")
.url("jdbc:h2:mem:testdb")
.username("sa")
.password("")
.build();
}
}
This class configures a DataSource
bean using the spring.datasource
properties. In this example, we're using an in-memory H2 database.
Step 4: Create a JPA entity
- Create a new Java class in the
com.example
package (or the package you chose earlier):// com/example/User.java
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;
@Entity public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters
}
This class represents a simple `User` entity with an `id`, `name`, and `email` attributes.
**Step 5: Create a repository interface**
* Create a new Java interface in the `com.example` package (or the package you chose earlier):
```java
// com/example/UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
This interface extends JpaRepository
and defines a User
entity with a Long
primary key.
Step 6: Run the application
- Run the Spring Boot application using your preferred method (e.g.,
mvn spring-boot:run
for Maven orgradle bootRun
for Gradle). - The application will start and create an in-memory H2 database.
- You can use a tool like
jdbc:h2:mem:testdb
to connect to the database and verify that it's working.
That's it! You now have a basic Spring Boot project with a database configuration using JPA and an in-memory H2 database.