JPA顧名思義就是Java Persistence API的意思,是JDK 5.0注解或XML描述對象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實體對象持久化到數(shù)據(jù)庫中。
依賴
- spring-boot-starter-data-jdbc
- spring-boot-starter-data-jpa
- mysql-connector-java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jdbc</ artifactId > </ dependency > < 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 > |
application.yml
spring.jpa.hibernate.ddl-auto=update
- 如果數(shù)據(jù)庫內(nèi)沒有表或表結(jié)構(gòu)改變時根據(jù)Entity創(chuàng)建/更新
spring.jpa.show-sql=true
- 控制臺打印sql
spring.jpa.database
- 指定數(shù)據(jù)庫類型,可寫可不寫
1
2
3
4
5
6
7
8
9
10
11
|
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8 username: root password: root jpa: hibernate: ddl-auto: update show-sql: true database: mysql |
POJO
- @Data是Lombok
- @Entity告訴JPA這是一個實體類(和數(shù)據(jù)表映射的類)
- @Table(name = “jpa_user”) 給表起名,不寫默認(rèn)為類名小寫(user)
- @Id設(shè)置主鍵
- @GeneratedValue(strategy = GenerationType.IDENTITY) 使用自增
- @Column(name = “jpa_username”,length = 40) 給列起名,不寫默認(rèn)為屬性名(username)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import lombok.Data; import javax.persistence.*; @Entity @Table (name = "jpa_user" ) @Data public class User { @Id @GeneratedValue (strategy = GenerationType.IDENTITY) private Integer id; @Column (name = "jpa_username" ,length = 40 ) private String username; @Column (name = "jpa_password" ) private String password; } |
Repository
JpaRepository的繼承關(guān)系
所以繼承JpaRepository
- JpaRepository<T, ID>
- 第一個泛型就是要操作的Entity
- 第二個泛型就是Entity的Id主鍵類型
- JpaRepository<User, Integer>
1
2
3
4
5
6
7
|
package com.live.repository; import com.live.model.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User,Integer> { } |
Controller(測試)
1
2
|
@Autowired @Autowired UserRepository userRepository; |
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
|
package com.live.controller; import com.live.model.User; import com.live.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; import java.util.Optional; @RestController public class UserJPAController { @Autowired UserRepository userRepository; @GetMapping ( "/findAll" ) public List<User> findAll() { return userRepository.findAll(); } @GetMapping ( "/findOne/{id}" ) public Optional<User> findOne( @PathVariable (value = "id" ) Integer id) { return userRepository.findById(id); } @GetMapping ( "/insert" ) public User insertOne(User user) { return userRepository.save(user); } } |
測試
注意:
- 對字段操作時,使用的是實體類的屬性名(username,password)
- 而不是在@Column(name=“jpa_username”)設(shè)置的jpa_username
查詢所有
插入
到此這篇關(guān)于SpringBoot2.3.0配置JPA的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot2.3.0配置JPA內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_44216706/article/details/106467023