Creating a new Spring Boot application that uses JPA is easy, straightforward, and does not involve complex configuration of any kind. You may have worked on other Spring Boot applications which you didn’t develop from scratch. Chances are those applications have a litter of custom configurations. But how do we quickly create a Spring application to work with JPA that is devoid of clutter?
This post uses Spring Initializr and Maven to create initial codes for a Spring Boot JPA application. Moreover, it uses MySQL, which we could set up locally or run as a docker container to test with the Spring Boot application.
Create a Spring Boot JPA application With Spring Initializr
The best way to quickly create a Spring Boot application that uses JPA is using Spring Initializr. Doing so enables us to compose the application swiftly by choosing relevant starter (and regular) dependencies. Also, the step allows us to generate project files automatically and with ready-to-run Java codes.
With Spring Initializr, we would only need two dependencies – the Spring Data JPA and the JDBC driver for whichever RDBMS we intend to use, e.g., MySQL.
On the other hand, the manual alternative may not be favorable because we would not have the initial complete list of dependencies. Meanwhile, creating new codes from existing Spring Boot applications may require manual changes by stripping off dependencies we don’t need in the new codebase.
How Does Our pom.xml Look?
When we generate the project files via the Spring Initializr, the pom.xml will have three dependencies – two are of our choosing, and another one (the spring-boot-starter-test) which the Spring Initializr includes by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.turreta.springbootjpa</groupId> <artifactId>springbootjpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootjpa</name> <description>springbootjpa project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Other Files In The Spring Boot JPA Application
Aside from the pom.xml file, we also get the main Java file and an empty application.properties file.
1 2 3 4 5 6 7 8 9 | package com.turreta.springbootjpa; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootjpaApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } |
Spring Boot Application Database Configuration
Lastly, we need to modify the empty application.properties file with the following settings.
1 2 3 4 5 6 7 8 | spring.datasource.url=jdbc:mysql://localhost:3306/mysqldatabase spring.datasource.username=mysqluser spring.datasource.password=mysqluserpassword spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa |
That’s how we can quickly create a Spring Boot application that uses JPA and with the minimum possible files and configuration.