Configuring the Database

MySQL

application.yml (MySQL)

Spring Boot reads the connection settings from application.yml (or application.properties). The dialect is detected automatically in Hibernate 6 — you do not need to set it.

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/shopease?createDatabaseIfNotExist=true
    username: shopease
    password: ${DB_PASSWORD}          # read from an environment variable
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true

Example output

INFO  o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
INFO  org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.6.15.Final
INFO  com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
INFO  com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
INFO  j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
INFO  o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'

PostgreSQL

spring.datasource.url=jdbc:postgresql://localhost:5432/shopease
spring.datasource.username=shopease
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

H2 In-Memory (for Learning and Tests)

application.yml (H2)

H2 runs inside your app, needs no installation, and is wiped when the app stops. Perfect for practice:

application.yml

spring:
  datasource:
    url: jdbc:h2:mem:shopease
    username: sa
    password:
  h2:
    console:
      enabled: true        # browse tables at http://localhost:8080/h2-console
  jpa:
    show-sql: true

Example output

INFO  o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:shopease'

Common Startup Errors

Error messageCauseFix
Failed to configure a DataSource: 'url' attribute is not specifiedJPA starter added but no datasource configuredAdd spring.datasource.url or the H2 dependency
Communications link failureMySQL not running or wrong host/portStart MySQL, check the URL
Access denied for userWrong username/passwordCheck credentials and grants
Not a managed type: class ...ProductEntity outside the main application packageMove it under the package of the @SpringBootApplication class