Prerequisites
Before you begin, ensure you have:- MySQL 8.0 or higher installed
- Java 17 or higher
- Maven for dependency management
Database Configuration
Create the Database
First, create the
ecommerce database in MySQL:The application uses UTC timezone for all database operations to ensure consistency across different server locations.
Configure application.properties
The database connection is configured in
src/main/resources/application.properties:JPA Configuration
Hibernate DDL Auto
The application usesspring.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
SQL Logging
The configuration enables SQL query logging for development:Disable SQL logging in production by setting
spring.jpa.show-sql=false and removing the logging configuration to improve performance.Entity Configuration
Usuario Entity
TheUsuarioEntity class maps to the Usuario table:
Table Schema
This entity creates the following table structure:| Column | Type | Constraints |
|---|---|---|
| idUsuario | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
| usuario | VARCHAR(255) | - |
| contrasena | VARCHAR(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:- Basic CRUD operations (save, findById, findAll, delete)
- Custom query methods based on method names
- Transaction management
Custom Query Method
ThefindByUsuarioAndContrasena method generates the following SQL:
Environment-Specific Configuration
Development Environment
Development Environment
Production Environment
Production Environment
Troubleshooting
Connection Issues
If you encounter connection errors:- Verify MySQL is running:
systemctl status mysql - Check credentials are correct
- Ensure the
ecommercedatabase exists - Verify MySQL is listening on port 3306
Schema Mismatch
If you see “Table doesn’t exist” errors:- Verify
spring.jpa.hibernate.ddl-auto=updateis set - Check entity class has
@Entityand@Tableannotations - Review application logs for schema creation SQL statements
Next Steps
Authentication
Learn how to implement user authentication
Error Handling
Understand the error handling system