Accessing Data with Spring Data JPA and MySQL
In this article, you will learn how to access data with Spring Data JPA and MySQL. You might have found the previous article very much useful and this article is an extension of Building a RESTful Web Service in Spring Boot.
Spring Data JPA is a method to implement JPA repositories to add the data access layer in applications easily. We will see an example of how to access data from a database in a spring boot application using spring data JPA.
Dependencies
You need spring-data-starter-data-jpa and mysql-connector-java dependencies to access MySQL database with Spring Data JPA.
If you are using Maven, add the following dependencies to your pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>For the Gradle project, add the following dependencies to your build.gradle file:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'You may like: How to add & update Canonical URL in Angular
Configuring Database Connection Properties
Update application.properties file under the src/main/resources directory with the following content:
# MySQL connection properties
spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://localhost:3306/rest-service spring.datasource.username=root
spring.datasource.password=root@passYou will need to create a database named rest-service in MySQL, and change the spring.datasource.username & spring.datasource.password properties as per your MySQL installation.
Create User Model Class
@Entity annotation defines that a class can be mapped to a table.
@Table annotation is used to provide the details of the table that this entity will be mapped to.
@GeneratedValue annotation is used to define the primary key generation strategy. In the above case, we have declared the primary key to be an Auto Increment field.
@Column annotation is used to define the properties of the column that will be mapped to the annotated field.
Create a Repository
@Repository is a stereotype for the persistence layer and tells Spring to bootstrap the repository during the component scan.
You will be now able to use save(), findOne(), findAll(), count(), delete() etc.
Create Request File
We have used the UserRequest class file to handle POST data.
You may like: Handle CORS error in Lumen
Create User Controller
The Final Step — We’ll now create the REST APIs for creating, retrieving a User.
The @RequestBody annotation is used to bind the request body with a method parameter.
You may like: How to write Graphql Query for Strapi application using Angular
Run the Application
As we are using Maven build, we can run the application by using ./mvnw spring-boot:run. Alternatively, you can build the JAR file with ./mvnw clean package and then run the JAR file, as follows:
java -jar target/rest-service-0.0.1-SNAPSHOT.jarIf you use Gradle, you can run the application by using ./gradlew bootRun. Alternatively, we can build the JAR file by using ./gradlew build and then run the JAR file:
java -jar build/libs/rest-service-0.0.1-SNAPSHOT.jarSummary
Congratulations! You have successfully developed a Spring Boot application that uses Spring Data JPA with MySQL database to store, access, delete and update data.
Originally published at https://www.codinghub.net.
