全称Java Persistence API,通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

​ JPA的出现有两个原因:
其一,简化现有Java EE和Java SE应用的对象持久化的开发工作;
其二,Sun希望整合对ORM技术,实现持久化领域的统一。

​ JPA提供的技术:

1)ORM映射元数据:JPA支持XML和JDK 5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;

2)JPA 的API:用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。

3)查询语言:通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。

<The rest of contents | 余下全文>

pom.xml中导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.caicai</groupId>
<artifactId>spring-boot-jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jpa</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

创建实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.caicai.springbootjpa.entity;

import javax.persistence.*;

/**
* @author liu
* @title: User
* @projectName spring-boot-jpa
* @description: TODO
* @date 2019/3/3121:14
*/

@Entity
public class User {
@GeneratedValue(strategy = GenerationType.AUTO)//设置自增长策略
@Id
private Integer id;
@Column(length = 20,nullable = false) //column可以设置属性
private String name;
@Column(length = 20,nullable = true)
private Integer age;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public User() {
}

public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}

创建UserRepository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.caicai.springbootjpa.repository;

import com.caicai.springbootjpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* @author liu
* @title: UserRepository
* @projectName spring-boot-jpa
* @description: TODO
* @date 2019/3/3121:06
*/
public interface UserRepository extends JpaRepository<User,Integer>{
}

配置数据源和自动建表:

1
2
3
4
5
6
7
8
9
10
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jpa
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.caicai.springbootjpa;

import com.caicai.springbootjpa.entity.User;
import com.caicai.springbootjpa.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootJpaApplicationTests {
@Autowired
UserRepository userRepository;
@Test
public void test() {
User user = new User();
user.setAge(10);
user.setName("caicai");
userRepository.save(user);
}

}