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.
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:
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.
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>
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 # 数据库方言
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方法 }
Create a Repository interface, inherit JpaRepository, which provides a large number of CRUD operation methods.
public interface UserRepository extends JpaRepository<User, Long> { }
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()); } }
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!