SpringBoot集成JPA持久層框架,簡化數(shù)據(jù)庫操作
與SpringBoot2.0整合
1、核心依賴
<!-- JPA框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
2、配置文件
spring: application: name: node09-boot-jpa datasource: url: jdbc:mysql://localhost:3306/data_jpa?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true username: root password: root driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true
ddl-auto幾種配置說明
1)create
每次加載hibernate時都刪除上一次的生成的表,然后根據(jù)bean類重新來生成新表,容易導(dǎo)致數(shù)據(jù)丟失,(建議首次創(chuàng)建時使用)。
2)create-drop
每次加載hibernate時根據(jù)bean類生成表,但是sessionFactory一關(guān)閉,表就自動刪除。
3)update
第一次加載hibernate時根據(jù)bean類會自動建立起表的結(jié)構(gòu),以后加載hibernate時根據(jù)bean類自動更新表結(jié)構(gòu),即使表結(jié)構(gòu)改變了但表中的行仍然存在不會刪除以前的行。
4)validate
每次加載hibernate時,驗證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),只會和數(shù)據(jù)庫中的表進行比較,不會創(chuàng)建新表,但是會插入新值。
3、實體類對象
就是根據(jù)這個對象生成的表結(jié)構(gòu)。
@Table(name = "t_user") @Entity public class User { @Id @GeneratedValue private Integer id; @Column private String name; @Column private Integer age; // 省略 GET SET }
4、JPA框架的用法
定義對象的操作的接口,繼承JpaRepository核心接口。
import com.boot.jpa.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User,Integer> { // 但條件查詢 User findByAge(Integer age); // 多條件查詢 User findByNameAndAge(String name, Integer age); // 自定義查詢 @Query("from User u where u.name=:name") User findSql(@Param("name") String name); }
5、封裝一個服務(wù)層邏輯
import com.boot.jpa.entity.User; import com.boot.jpa.repository.UserRepository; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserService { @Resource private UserRepository userRepository ; // 保存 public void addUser (User user){ userRepository.save(user) ; } // 根據(jù)年齡查詢 public User findByAge (Integer age){ return userRepository.findByAge(age) ; } // 多條件查詢 public User findByNameAndAge (String name, Integer age){ return userRepository.findByNameAndAge(name,age) ; } // 自定義SQL查詢 public User findSql (String name){ return userRepository.findSql(name) ; } // 根據(jù)ID修改 public void update (User user){ userRepository.save(user) ; } //根據(jù)id刪除一條數(shù)據(jù) public void deleteStudentById(Integer id){ userRepository.deleteById(id); } }
測試代碼塊
import com.boot.jpa.JpaApplication; import com.boot.jpa.entity.User; import com.boot.jpa.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = JpaApplication.class) public class UserJpaTest { @Resource private UserService userService ; @Test public void addUser (){ User user = new User() ; user.setName("知了一笑"); user.setAge(22); userService.addUser(user); User user1 = new User() ; user1.setName("cicada"); user1.setAge(23); userService.addUser(user1); } @Test public void findByAge (){ Integer age = 22 ; // User{id=3, name='知了一笑', age=22} System.out.println(userService.findByAge(age)); } @Test public void findByNameAndAge (){ System.out.println(userService.findByNameAndAge("cicada",23)); } @Test public void findSql (){ // User{id=4, name='cicada', age=23} System.out.println(userService.findSql("cicada")); } @Test public void update (){ User user = new User() ; // 如果這個主鍵不存在,會以主鍵自增的方式新增入庫 user.setId(3); user.setName("哈哈一笑"); user.setAge(25); userService.update(user) ; } @Test public void deleteStudentById (){ userService.deleteStudentById(5) ; } }
源代碼地址
GitHub地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
碼云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base
以上就是SpringBoot集成JPA持久層框架,簡化數(shù)據(jù)庫操作的詳細內(nèi)容,更多關(guān)于SpringBoot集成JPA持久層框架的資料請關(guān)注腳本之家其它相關(guān)文章!
- springboot + jpa實現(xiàn)刪除數(shù)據(jù)的操作代碼
- Spring?Boot?整合JPA?數(shù)據(jù)模型關(guān)聯(lián)使用操作(一對一、一對多、多對多)
- SpringDataJPA詳解增刪改查操作方法
- Spring?Data?JPA映射自定義實體類操作
- SpringDataJpa多表操作的實現(xiàn)
- Springboot使用Spring Data JPA實現(xiàn)數(shù)據(jù)庫操作
- springboot-jpa的實現(xiàn)操作
- springboot 之jpa高級查詢操作
- Springboot JPA級聯(lián)操作的實現(xiàn)(一對一、一對多、多對多)
相關(guān)文章
淺談hibernate中對象的3種狀態(tài)_瞬時態(tài)、持久態(tài)、脫管態(tài)
下面小編就為大家?guī)硪黄獪\談hibernate中對象的3種狀態(tài)_瞬時態(tài)、持久態(tài)、脫管態(tài)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08SpringBoot集成Swagger構(gòu)建api文檔的操作
這篇文章主要介紹了SpringBoot集成Swagger構(gòu)建api文檔的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12SpringBoot整合Redis及Redis工具類撰寫實例
這篇文章主要介紹了SpringBoot整合Redis及Redis工具類撰寫實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Spring?RestTemplate遠程調(diào)用過程
這篇文章主要介紹了Spring?RestTemplate遠程調(diào)用過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11JSON字符串轉(zhuǎn)成java的Map對象詳細步驟
這篇文章主要介紹了如何將JSON字符串轉(zhuǎn)換為Java對象的步驟,包括定義Element類、使用Jackson庫解析JSON和添加依賴,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-01-01SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式
這篇文章主要介紹了SpringBoot整合liquibase的相關(guān)資料,文中給大家介紹了liquibase生成初始化腳本的兩種方式,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02