Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實現(xiàn)
本文展示如何通過JPA訪問MySQL數(shù)據(jù)庫。
JPA全稱Java Persistence API,即Java持久化API,它為Java開發(fā)人員提供了一種對象/關系映射工具來管理Java應用中的關系數(shù)據(jù),結(jié)合其他ORM的使用,能達到簡化開發(fā)流程的目的,使開發(fā)者能夠?qū)W⒂趯崿F(xiàn)自己的業(yè)務邏輯上。
Spring boot結(jié)合Jpa 能夠簡化創(chuàng)建 JPA 數(shù)據(jù)訪問層和跨存儲的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,無需再寫實現(xiàn)類,就可以實現(xiàn)對象的CRUD操作以及分頁排序等功能。
環(huán)境要求
- Mysql數(shù)據(jù)庫5.6以上
- JDK1.8以上
- 開發(fā)工具使用STS
創(chuàng)建項目
使用STS創(chuàng)建項目
選擇web和JPA依賴
添加MySQL數(shù)據(jù)庫驅(qū)動依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
application.properties中配置數(shù)據(jù)庫連接信息
spring.jpa.hibernate.ddl-auto=create spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=springuser spring.datasource.password=ThePassword
以上數(shù)據(jù)庫連接信息根據(jù)實際情況進行調(diào)整。
注意pring.jpa.hibernate.ddl-auto的值可以是none、create、update、create-drop。具體參考hibernate的文檔。
創(chuàng)建實體模型
com.yuny.jpademo.pojo.User
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity // This tells Hibernate to make a table out of this class public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String name; private String email; //此處省略get和set }
增加數(shù)據(jù)訪問接口
com.yuny.jpademo.repository.UserRepository
public interface UserRepository extends PagingAndSortingRepository<User, Long> { }
此接口會自動由spring實現(xiàn),并且產(chǎn)生對應的實例放在容器中,該實例的名稱為類名首字母小寫userRepository。
創(chuàng)建Controller測試
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.yuny.jpademo.pojo.User; import com.yuny.jpademo.repository.UserRepository; @RestController public class UserController { @Autowired private UserRepository userRepository; //測試插入新的數(shù)據(jù) @GetMapping(path="/add") public @ResponseBody String addNewUser (@RequestParam String name , @RequestParam String email) { User n = new User(); n.setName(name); n.setEmail(email); userRepository.save(n); return "保存成功"; } //測試獲取全部的數(shù)據(jù) @GetMapping(path="/all") public Iterable<User> getAllUsers() { return userRepository.findAll(); } }
測試
運行SpringBootJpademoApplication后,訪問http://localhost:8080/add測試。結(jié)果如下:
數(shù)據(jù)庫顯示插入數(shù)據(jù)成功
訪問http://localhost:8080/all 測試
總結(jié)
在沒用使用jpa支持的時候,我們的代碼要定義IUserDao(持久層接口)、IUserDaoImpl(持久層實現(xiàn)類)、IUserService(業(yè)務層接口)等,這樣每寫一個實體類,都要衍生出多個類來進行操作。
而在Spring boot 中使用JPA,只需要聲明一個接口就可以了。
案例代碼
https://github.com/junyanghuang/spring-boot-samples/tree/master/spring-boot-jpademo
到此這篇關于Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實現(xiàn)的文章就介紹到這了,更多相關Springboot JPA訪問MySQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- JPA之映射mysql text類型的問題
- Springboot2.0配置JPA多數(shù)據(jù)源連接兩個mysql數(shù)據(jù)庫方式
- 解決springboot的JPA在Mysql8新增記錄失敗的問題
- Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼
- springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫
- Spring-Data-JPA整合MySQL和配置的方法
- SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進行操作
- Spring Boot 添加MySQL數(shù)據(jù)庫及JPA實例
- 在JPA項目啟動時如何新增MySQL字段
相關文章
spring的TransactionSynchronizationAdapter事務源碼解析
這篇文章主要介紹了spring的TransactionSynchronizationAdapter事務源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Java中的NumberFormatException異常原因以及解決方案詳解
這篇文章主要介紹了Java中的NumberFormatException異常原因以及解決方案詳解,NumberFormatException 是 Java 中的一個異常類,通常在字符串轉(zhuǎn)換為數(shù)字的過程中發(fā)生,它表示一個無效的數(shù)字格式,即字符串無法被正確解析為數(shù)字,需要的朋友可以參考下2024-02-02spring中WebClient如何設置連接超時時間以及讀取超時時間
這篇文章主要給大家介紹了關于spring中WebClient如何設置連接超時時間以及讀取超時時間的相關資料,WebClient是Spring框架5.0引入的基于響應式編程模型的HTTP客戶端,它提供一種簡便的方式來處理HTTP請求和響應,需要的朋友可以參考下2024-08-08