At the beginning of using the spring boot development project, the Spring Data JPA used by the ORM framework was only used intermittently for more than a year, without making some summary. Taking advantage of the current serious epidemic situation and little work, I will take a note and record the general steps of integrating Spring Data JPA with spring boot.
1. First, create a new springboot project named springdata. The version of springboot uses 2.0.5.RELEASE.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> </parent>
2. Import the corresponding jar package
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
3. Create a new User entity class and add comments
import lombok.Data; import lombok.ToString; import javax.persistence.*; import java.io.Serializable; @Data @Entity @Table(name = "user") @ToString public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "user_name") private String userName; @Column(name = "user_age") private Integer userAge; }
Note:
@Entity: indicates that the class is an entity class
@Table: indicates the table name of the database
@Id: Specifies the primary key of the table
@Generated value: Specifies the generation policy of the primary key
@Column: indicates the correspondence between the member properties of the class and the fields in the table
4. Create a new UserDao interface, integrate the JpaRepository class, add the sel method to query users, and update the user update method
import com.springdata.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @Repository public interface UserDao extends JpaRepository<User, Integer> { @Query(value = "select id, user_name, user_age from user where id = :id", nativeQuery = true) User sel(@Param("id") int id); @Transactional @Modifying @Query(value = "update user set user_name = :userName, user_age = :userAge where id = :id", nativeQuery = true) void update(@Param("userName") String userName, @Param("userAge") Integer userAge, @Param("id") Integer id); }
5. Add mysql database configuration and jpa configuration under application.properties configuration file in resources directory
server.port=8080 # mysql database configuration spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver # JPA configuration # Show executed sql statements spring.jpa.show-sql=true
6. Create UserService interface class and implementation class UserServiceImpl
import com.springdata.model.User; import java.util.List; public interface UserService { public User sel(int id); public void add(User user); public void update(User user); } import com.springdata.dao.UserDao; import com.springdata.model.User; import com.springdata.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public User sel(int id) { return userDao.sel(id); } @Override public void add(User user) { userDao.save(user); } @Override public void update(User user) { userDao.update(user.getUserName(), user.getUserAge(), user.getId()); } }
7. Create controller class UserController
import com.springdata.model.User; import com.springdata.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping(value = "/getUser/{id}") public String getUser(@PathVariable int id) { return userService.sel(id).toString(); } @PostMapping(value = "/addUser", produces = "application/json;charset=UTF-8") public String addUser(@RequestBody User user) { try { userService.add(user); } catch (Exception e) { e.printStackTrace(); return "error"; } return "success"; } @PostMapping(value = "/updateUser", produces = "application/json;charset=UTF-8") public String updateUser(@RequestBody User user) { try { userService.update(user); } catch (Exception e) { e.printStackTrace(); return "error"; } return "success"; } }
8. Create the user table under the test library of mysql
CREATE TABLE `user` ( `id` INT(32) NOT NULL AUTO_INCREMENT, `user_name` VARCHAR(32) NOT NULL, `user_age` INT(3) NOT NULL, PRIMARY KEY (`id`) USING BTREE )
9. Start the spring data project and test with postman
New users:
Modify user:
Query user:
So far, the integration of Spring Data JPA with spring boot has been completed, and the test is also very successful, isn't it very simple!
I hope you can make some improvements. Thank you very much!