Careeroza
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: trueExample 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=trueH2 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: trueExample 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 message | Cause | Fix |
|---|---|---|
Failed to configure a DataSource: 'url' attribute is not specified | JPA starter added but no datasource configured | Add spring.datasource.url or the H2 dependency |
Communications link failure | MySQL not running or wrong host/port | Start MySQL, check the URL |
Access denied for user | Wrong username/password | Check credentials and grants |
Not a managed type: class ...Product | Entity outside the main application package | Move it under the package of the @SpringBootApplication class |