Spring Data JPA 五分钟快速入门和实践
生活随笔
收集整理的這篇文章主要介紹了
Spring Data JPA 五分钟快速入门和实践
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring Data JPA(類似于Java Web 中的 DAO)
-
操作聲明持久層的接口(Repository)
-
三個核心接口:
- CrudRepository
- PagingAndSortingRepository
- JpaRepository
-
相應解釋:
- 該類提供基本的crud等接口;
- 該類提供基本分頁,排序等接口;
- 作為前二者的子類,繼承它們所有接口,在實際項目中,均為JapRepository或其子類進行基本的數據庫操作;
-
概念圖:
-
如何使用JPA:
-
引入相關JPA; -
使用JpaRepository接口; -
服務層實現; -
CRUD和分頁簡單實現; -
自定義查詢; -
集成測試;
-
具體實踐:
項目架構:
一:添加相應依賴
- 由于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
- 注意點:
- 關于連接池連接超時問題:應在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;@Columnprivate String phone;@Columnprivate 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. 添加@Entity依賴、添加@Table依賴;
2. 標記id,引入自動增長,使表格具有實體完整性;
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;
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 {@AutowiredUserRepository 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;}}
- 具體代碼講解:
- 注入相應的JpaRepository子接口;
@Autowired
UserRepository userRepository;
- 單個查詢:
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id) { User select = userRepository.findById(id).get(); return select;
}
- 插入數據:
@GetMapping("/user")
public User insertUser(User user) { User save = userRepository.save(user); return save;
}
- 查詢所有數據:
@GetMapping("/all")
public List<User> selectUser(User user){ return userRepository.findAll();
}
- 重新運行SpringApplication的主活動。
六:在Chrome中進行相應測試:
1.
2.
3.
注:本人制作一個具有完整功能的模板,包括分頁查詢功能,需要的朋友可以直接私信我,或者直接下載Spring Data JPA 2.0 標準模板。
本人學習Spring Boot 2.0 時間尚短,存在錯誤之處,請您斧正,共同進步。
總結
以上是生活随笔為你收集整理的Spring Data JPA 五分钟快速入门和实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java单例模式个人总结(实例变量和类变
- 下一篇: Xcode couldn‘t find