Spring Data JPA(类似于Java Web 中的 DAO)
操作声明持久层的接口(Repository)
三个核心接口:
CrudRepository
PagingAndSortingRepository
JpaRepository
相应解释:
该类提供基本的crud等接口;
该类提供基本分页,排序等接口;
作为前二者的子类,继承它们所有接口,在实际项目中,均为JapRepository或其子类进行基本的数据库操作;
概念图:
在这里插入图片描述
如何使用JPA:
引入相关JPA;
1
使用JpaRepository接口;
1
服务层实现;
1
CRUD和分页简单实现;
1
自定义查询;
1
集成测试;
1
具体实践:
项目架构:在这里插入图片描述
一:添加相应依赖
由于IDEA2020最新版本,可以直接勾选依赖:
Web下SpringWeb
SQL下Spring Data JPA
SQL下Spring Data JDBC
MySQL Driver
在这里插入图片描述
二:配置properties文件:
在这里插入图片描述
spring.datasource.url=jdbc:mysql://localhost:3306/redbook?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=mysq35663
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
1
2
3
4
5
6
7
注意点:
关于连接池连接超时问题:应在url后加上:serverTimezone=UTC;
增加更新数据表的操作:spring.jpa.hibernate.ddl-auto=update
在控制台显示SQL语句:spring.jpa.show-sql=true
根据自身主机的端口,数据库账号和密码;输入正确的配置信息;
三:构造Entity类:
在这里插入图片描述
package com.example.test.sharebook2.entity;
import javax.persistence.*;
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String phone;
@Column
private String password;
public Integer getId() {
return id;
}
public String getPhone() {
return phone;
}
public String getPassword() {
return password;
}
public void setId(Integer id) {
this.id = id;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setPassword(String password) {
this.password = password;
}
}
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
注意点:
- 添加@Entity依赖、添加@Table依赖;
- 标记id,引入自动增长,使表格具有实体完整性;
1
2
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
1
2
3
3.正常列应添加@Column;
4.添加相应读写方法(Set,Get);
5.配置之后,自动在相应的数据库中生成表格;
四:引入相应接口:在这里插入图片描述
引入JpaRepository接口,创建其子类,方便后期直接注入;
JpaRepository<User,Integer>中第一个参数,代表实体类名,第二个参数代表表中主键类型;
此时可以运行SpringApplication的主活动,向数据库注入相应的实体类(表格);
五:编写DAO(CRUD):在这里插入图片描述
package com.example.test.sharebook2.controller;
import com.example.test.sharebook2.entity.User;
import com.example.test.sharebook2.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserTest {
@Autowired
UserRepository userRepository;
@GetMapping("/all")
public List<User> selectUser(User user){
return userRepository.findAll();
}
@GetMapping("/user")
public User insertUser(User user){
User save = userRepository.save(user);
return save;
}
@GetMapping("/select/{phone}")
public List<User> getUser(@PathVariable("phone") String phone){
List<User> userlist = userRepository.findByPhone(phone);
return userlist;
}
}
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
具体代码讲解:
注入相应的JpaRepository子接口;
@Autowired
UserRepository userRepository;
1
2
单个查询:
@GetMapping("/user/{id}")
public User getUser(@PathVariable(“id”) Integer id) {
User select = userRepository.findById(id).get();
return select;
}
1
2
3
4
5
插入数据:
@GetMapping("/user")
public User insertUser(User user) {
User save = userRepository.save(user);
return save;
}
1
2
3
4
5
查询所有数据:
@GetMapping("/all")
public List selectUser(User user){
return userRepository.findAll();
}
1
2
3
4
重新运行SpringApplication的主活动。
六:在Chrome中进行相应测试:
1.在这里插入图片描述
2.在这里插入图片描述
3.在这里插入图片描述