Home > Java > javaTutorial > Spring Boot and Spring Data JPA integrate to implement ORM mapping

Spring Boot and Spring Data JPA integrate to implement ORM mapping

WBOY
Release: 2023-06-22 18:31:59
Original
1180 people have browsed it

With the development of the Internet, more and more applications need to deal with databases, and ORM (Object-Relational Mapping) technology has emerged. It maps data in relational databases to object models, allowing us to Manipulate database data like ordinary Java objects. Spring Boot and Spring Data JPA are currently the most popular ORM technologies. This article will introduce how to implement ORM mapping through their integration.

  1. Environment setup

Before starting the content of this article, you need to first understand Spring Boot and Spring Data JPA, and be equipped with the following environment:

  • JDK 1.8 or above
  • Maven 3.x or above
  • IDE tool
  1. Create Spring Boot application

Create a new Spring Boot project. For specific operations, you can create a Maven project in the IDE, select Spring Boot dependencies or use Spring Initializr to quickly build it.

  1. Configure the pom.xml file

Configure the pom.xml file to add Spring Data JPA and MySQL driver dependencies.

<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>
Copy after login
  1. Configure application.properties file

Configure application.properties file to set data source properties.

# 数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# JPA配置
spring.jpa.show-sql=true  # 显示SQL
spring.jpa.hibernate.ddl-auto=update # 自动创建表
spring.jpa.properties.hibernate.hbm2ddl.auto=update # 自动更新表
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect # 数据库方言
Copy after login
  1. Create entity class

Create an entity class, use the @Entity annotation to mark it as an entity class, and use the @Id annotation to mark the primary key.

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Integer age;
    // 省略getter和setter方法
}
Copy after login
  1. Create Repository

Create a Repository interface, inherit JpaRepository, which provides a large number of CRUD operation methods.

public interface UserRepository extends JpaRepository<User, Long> {
}
Copy after login
  1. Write test code

Inject UserRepository in the test class and use it to operate the database.

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testSave() {
        User user = new User();
        user.setName("test");
        user.setAge(20);
        userRepository.save(user);
        assertThat(user.getId()).isNotNull();
    }

    @Test
    public void testFindByName() {
        User user = new User();
        user.setName("test");
        user.setAge(20);
        userRepository.save(user);
        User foundUser = userRepository.findByName("test");
        assertThat(foundUser.getName()).isEqualTo(user.getName());
    }
}
Copy after login
  1. Run the test

Run the test class and view the test results. If the test passes, the integration is successful.

Through the above steps, we successfully completed the configuration and use of ORM mapping through the integration of Spring Boot and Spring Data JPA. Of course, this is just an introduction to basic usage. Spring Data JPA also provides many other functions, such as paging, sorting, conditional query, etc. I hope that readers can further understand and master the usage of Spring Boot and Spring Data JPA through the introduction of this article, and better develop excellent applications.

The above is the detailed content of Spring Boot and Spring Data JPA integrate to implement ORM mapping. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template