Skip to main content
This guide walks you through setting up the MySQL database and configuring Spring Data JPA for the E-Commerce Backend API.

Prerequisites

Before you begin, ensure you have:
  • MySQL 8.0 or higher installed
  • Java 17 or higher
  • Maven for dependency management

Database Configuration

1

Create the Database

First, create the ecommerce database in MySQL:
CREATE DATABASE ecommerce;
The application uses UTC timezone for all database operations to ensure consistency across different server locations.
2

Configure application.properties

The database connection is configured in src/main/resources/application.properties:
spring.application.name=demo
spring.datasource.url=jdbc:mysql://localhost:3306/ecommerce?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=sasa
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
logging.level.org.hibernate.SQL=debug
Update the spring.datasource.username and spring.datasource.password with your MySQL credentials before running the application.
3

Add Required Dependencies

The application uses the following dependencies in pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>
These dependencies provide:
  • spring-boot-starter-data-jpa: JPA support with Hibernate
  • mysql-connector-j: MySQL JDBC driver

JPA Configuration

Hibernate DDL Auto

The application uses spring.jpa.hibernate.ddl-auto=update which automatically:
  • Creates tables if they don’t exist
  • Updates existing table schemas when entities change
  • Preserves existing data during schema updates
In production environments, consider using validate instead of update to prevent automatic schema changes. Use database migration tools like Flyway or Liquibase for production deployments.

SQL Logging

The configuration enables SQL query logging for development:
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=debug
This displays all SQL queries in the console, making it easier to debug database operations.
Disable SQL logging in production by setting spring.jpa.show-sql=false and removing the logging configuration to improve performance.

Entity Configuration

Usuario Entity

The UsuarioEntity class maps to the Usuario table:
@Entity
@Table(name = "Usuario")
public class UsuarioEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idUsuario;
 
    @Column(name = "usuario")
    private String usuario;

    @Column(name = "contrasena")
    private String contrasena;
}

Table Schema

This entity creates the following table structure:
ColumnTypeConstraints
idUsuarioBIGINTPRIMARY KEY, AUTO_INCREMENT
usuarioVARCHAR(255)-
contrasenaVARCHAR(255)-
The idUsuario field uses GenerationType.IDENTITY which leverages MySQL’s AUTO_INCREMENT feature for automatic ID generation.

Repository Layer

JPA Repository Interface

The application uses Spring Data JPA repositories:
@Repository
public interface UsuarioJPARepository extends JpaRepository<UsuarioEntity, Long> {
    UsuarioEntity findByUsuarioAndContrasena(String usuario, String contrasena);
}
Spring Data JPA automatically implements:
  • Basic CRUD operations (save, findById, findAll, delete)
  • Custom query methods based on method names
  • Transaction management

Custom Query Method

The findByUsuarioAndContrasena method generates the following SQL:
SELECT * FROM Usuario WHERE usuario = ? AND contrasena = ?

Environment-Specific Configuration

spring.datasource.url=jdbc:mysql://localhost:3306/ecommerce?serverTimezone=UTC
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
logging.level.org.hibernate.SQL=debug
spring.datasource.url=jdbc:mysql://${DB_HOST}:${DB_PORT}/ecommerce?serverTimezone=UTC
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate
Use environment variables to avoid hardcoding credentials.

Troubleshooting

Connection Issues

If you encounter connection errors:
  1. Verify MySQL is running: systemctl status mysql
  2. Check credentials are correct
  3. Ensure the ecommerce database exists
  4. Verify MySQL is listening on port 3306

Schema Mismatch

If you see “Table doesn’t exist” errors:
  1. Verify spring.jpa.hibernate.ddl-auto=update is set
  2. Check entity class has @Entity and @Table annotations
  3. Review application logs for schema creation SQL statements

Next Steps

Authentication

Learn how to implement user authentication

Error Handling

Understand the error handling system

Build docs developers (and LLMs) love