springboot integrates jpa with detailed steps (explained with pictures and texts)

1. First, create a new springboot project named springboot JPA. After setting the java version, click next

2. Select three dependencies, lombok, spring Web and Spring Boot DevTools. Then click finish.

Lombok: getter,setter, parameterized construction, non parameterized construction, etc. can be omitted through annotation in entity class.

Sprang boot devtools: the application provides some development time features, including default setting, automatic restart, livereload, etc.

Spring Web: springboot depends on starting. The default server is tomcat.

The Spring Boot version number below is generally the default, regardless


3. In POM Add the dependency of SpringData jpa and MySql database to the XML file.

 <!-- Spring Data JPA Dependency (important) -->
        <!-- MySQL Drive (important) -->

Refresh dependency

4. In application Add the database connection configuration in the properties file: here, my database name is springbootjpa, and the user name and password are changed to your own.

#Automatically generate database tables (key)
#mysql database connection configuration (very important)
spring.datasource.url = jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=Asia/Shanghai
#Database user name
spring.datasource.username = root
#Database password
spring.datasource.password = x5
#mysql database driver (important)
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
#jpa configuration: display Hibernate SQL on the console (optional)
spring.jpa.show-sql = true
#Other configuration: turn off the cache of Thymeleaf
spring.thymeleaf.cache = false

5. Create the entity folder under the springbootjpa folder. In the entity folder, create the entity class User, which is used to map the database table.

package com.example.springbootjpa.entity;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false,unique = true,length = 20)
    private String username;

    @Column(nullable = false,length = 20)
    private String password;

    @Column(nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date regdate;


At the same time, a new database named springbootjpa is created in MySql. There is no need to create a table. The table will be automatically generated after running the project.

Startup project: run the startup class SpringbootjpaApplication

At this time, the user table will be automatically generated in the database springbootjpa we created. The fields of the table are our attributes in the entity class user.


6. Create a new folder under the springbootjpa folder, named repository, and create an interface class under the repository folder: named UserRepository. The interface should be extended to the jparepository < User, Long > interface, where User represents the entity model and Long represents the primary key type.

Add the annotation @ Repository on the class to indicate the interface containing functions such as addition, deletion, modification and query


Here, the dependency import and configuration of jpa are basically completed. There are many built-in addition, deletion, modification and query methods. We don't need to write our own sql statements, but we can call them directly.

7. Create a new folder controller under the springbootjpa folder and a new class UserController in the controller folder. Used for jpa testing.

package com.example.springbootjpa.controller;

import com.example.springbootjpa.entity.User;
import com.example.springbootjpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


public class UserController {
    private UserRepository userRepository;

    //Find all user objects

    public List<User> findall(){
        List<User> userList=userRepository.findAll();
        return userList;

    //Add User object
    public void add(User user){

    //Delete User object
    public void delete(@Param("id") Long id){

Here I write three methods: find, add and delete. Add the annotation @ RestController on the class to indicate that the output format is json format.

We manually add several pieces of test data to the table in the database

8. Run the project again, open the browser test, and enter http://localhost:8080/findall

You can get two pieces of data added to the database, which shows that jpa integration is successful.


Next, enter in the browser address bar


We found one more piece of data in the database, and the insertion method was successful

Enter in the browser http://localhost:8080/deleteById?id=3

At this time, it is found that a piece of data with id 2 is missing in the database, indicating that the deletion method test is successful.


The above is the spring boot integration jpa add, display, delete functions


Tags: JavaEE

Posted by darthbutternutz on Sat, 16 Apr 2022 13:45:01 +0930