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:

Step 1: Create a new Spring Boot project

Step 2: Add 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

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

That's it! You now have a basic Spring Boot project with a database configuration using JPA and an in-memory H2 database.