Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范。它为 Java 开发人员提供了一种对象/关联映射工具来管理 Java 应用中的关系数据。它的出现主要是为了简化现有的持久化开发工作和整合 ORM 技术,结束现在 Hibernate,TopLink,JDO 等 ORM 框架各自为营的局面。
下面我们来看看,Spring Boot 怎样集成 JPA。
添加 JPA 依赖
1 | <dependency> |
2 | <groupId>org.springframework.boot</groupId> |
3 | <artifactId>spring-boot-starter-data-jpa</artifactId> |
4 | </dependency> |
配置数据源信息
1 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
2 | spring.datasource.url=jdbc:mysql://localhost:3306/game?useSSL=false&serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8 |
3 | spring.datasource.username=root |
4 | spring.datasource.password=root |
5 |
|
6 | spring.jpa.show-sql=true |
7 | spring.jpa.hibernate.ddl-auto=update |
8 | spring.jpa.open-in-view=false |
使用 JPA 配置数据源
1 | @Configuration |
2 | @EnableTransactionManagement |
3 | @EnableJpaAuditing |
4 | @EnableJpaRepositories(basePackages = "com.example.jpa.repository") |
5 | @EntityScan(basePackages = "com.example.jpa.entity") |
6 | public class DataSourceConfig { |
7 |
|
8 | } |
创建抽象审计类
1 | @MappedSuperclass |
2 | @EntityListeners(AuditingEntityListener.class) |
3 | public abstract class AbstractAuditingEntity implements Serializable { |
4 |
|
5 | private static final long serialVersionUID = -8636810082784692918L; |
6 |
|
7 | @CreatedDate |
8 | @Temporal(TIMESTAMP) |
9 | private Date createdDate; |
10 |
|
11 | @LastModifiedDate |
12 | @Temporal(TIMESTAMP) |
13 | private Date lastModifiedDate; |
14 |
|
15 | public Date getCreatedDate() { |
16 | return createdDate; |
17 | } |
18 |
|
19 | public void setCreatedDate(Date createdDate) { |
20 | this.createdDate = createdDate; |
21 | } |
22 |
|
23 | public Date getLastModifiedDate() { |
24 | return lastModifiedDate; |
25 | } |
26 |
|
27 | public void setLastModifiedDate(Date lastModifiedDate) { |
28 | this.lastModifiedDate = lastModifiedDate; |
29 | } |
30 | } |
创建映射实体类
1 | @Entity |
2 | @Table(name = "people") |
3 | public class People extends AbstractAuditor { |
4 |
|
5 | private static final long serialVersionUID = -2189163594057781698L; |
6 |
|
7 | @Id |
8 | @GeneratedValue(strategy = GenerationType.IDENTITY) |
9 | private Long id; |
10 |
|
11 | @Column(name = "name", columnDefinition = "nvarchar(20)") |
12 | private String name; |
13 |
|
14 | @Column(name = "age", columnDefinition = "int") |
15 | private Integer age; |
16 |
|
17 | public Long getId() { |
18 | return id; |
19 | } |
20 |
|
21 | public void setId(Long id) { |
22 | this.id = id; |
23 | } |
24 |
|
25 | public String getName() { |
26 | return name; |
27 | } |
28 |
|
29 | public void setName(String name) { |
30 | this.name = name; |
31 | } |
32 |
|
33 | public Integer getAge() { |
34 | return age; |
35 | } |
36 |
|
37 | public void setAge(Integer age) { |
38 | this.age = age; |
39 | } |
40 | } |
编写一个继承 JpaRepository 的接口完成数据访问
1 | public interface PeopleRepository extends JpaRepository<People, Long> { |
2 |
|
3 | } |
编写 Service 相关类
调用 PeopleRepository 类相关方法,进行数据库操作。
1 | @Service |
2 | public class PeopleService { |
3 |
|
4 | @Autowired |
5 | private PeopleRepository peopleRepository; |
6 |
|
7 | @Autowired |
8 | private PeopleMapper peopleMapper; |
9 |
|
10 | public void savePeople(PeopleDTO dto) { |
11 | People people = peopleMapper.convertToPeople(dto); |
12 | peopleRepository.save(people); |
13 | } |
14 | } |
参考
Spring Data JPA 官方文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference
源码地址
springboot-jpa