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) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL Drive (important) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency>
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) spring.jpa.hibernate.ddl-auto=update #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; @Entity @Data public class User implements Serializable { @Id @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; @RestController public class UserController { @Autowired private UserRepository userRepository; //Find all user objects @GetMapping("findall") public List<User> findall(){ List<User> userList=userRepository.findAll(); return userList; } //Add User object @RequestMapping("add") public void add(User user){ userRepository.save(user); } //Delete User object @RequestMapping("deleteById") public void delete(@Param("id") Long id){ userRepository.deleteById(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
http://localhost:8080/add?id=66&username=th6ree&password=thre6e®date=2022-04-16%2013:33:15
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