This project demonstrates the use of Spring Boot with Hibernate JPA, Lombok, and MySQL . It also includes information about using H2 database as an alternative database.
- Technologies Used
- Project Setup
- Configuration
- Running the Application
- H2 Database
- Using MySQL
- Lombok
- Spring Boot
- Hibernate JPA
- Lombok
- MySQL
- H2 database (optional)
- Ensure you have Java JDK 11 or later installed.
- Clone this repository:
git clone https://github.com/yourusername/electra-spring-boot-hibernate-jpa.git
- Navigate to the project directory:
cd electra-spring-boot-hibernate-jpa
- Build the project:
./mvnw clean install
(ormvnw clean install
on Windows)
To use MySQL instead of H2, follow these steps:
-
Add the MySQL dependency to your
pom.xml
:<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
-
Update
application.properties
:spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
-
Create the MySQL database:
CREATE DATABASE your_database_name;
-
Restart your application.
The project uses application.properties
for configuration. Here's a sample configuration:
# H2 Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# H2 Console Configuration
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
To run the application, use the following command:
./mvnw spring-boot:run
The application will start on http://localhost:8080
.
This project uses H2 as the default in-memory database. You can access the H2 console at http://localhost:8080/h2-console
. Use the following details to log in:
- JDBC URL:
jdbc:h2:mem:testdb
- Username:
sa
- Password:
password
This project uses Lombok to reduce boilerplate code. Ensure your IDE has Lombok plugin installed for proper support.
To use Lombok in your entities:
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class YourEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Other fields...
}
This setup allows you to use Lombok annotations like @Data
, @NoArgsConstructor
, and @AllArgsConstructor
to automatically generate getters, setters, constructors, and other common methods.
For more information on Lombok, visit Project Lombok.
Thanks to the following people who have contributed to this project:
This project is licensed under the MIT License - see the LICENSE.md file for details.