av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring boot基于JPA訪問MySQL數據庫的實現

瀏覽:87日期:2023-03-01 15:36:07

本文展示如何通過JPA訪問MySQL數據庫。

JPA全稱Java Persistence API,即Java持久化API,它為Java開發人員提供了一種對象/關系映射工具來管理Java應用中的關系數據,結合其他ORM的使用,能達到簡化開發流程的目的,使開發者能夠專注于實現自己的業務邏輯上。

Spring boot結合Jpa 能夠簡化創建 JPA 數據訪問層和跨存儲的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,無需再寫實現類,就可以實現對象的CRUD操作以及分頁排序等功能。

環境要求 Mysql數據庫5.6以上 JDK1.8以上 開發工具使用STS創建項目

使用STS創建項目

Spring boot基于JPA訪問MySQL數據庫的實現

選擇web和JPA依賴

Spring boot基于JPA訪問MySQL數據庫的實現

添加MySQL數據庫驅動依賴

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>

application.properties中配置數據庫連接信息

spring.jpa.hibernate.ddl-auto=createspring.datasource.url=jdbc:mysql://localhost:3306/db_examplespring.datasource.username=springuserspring.datasource.password=ThePassword

以上數據庫連接信息根據實際情況進行調整。

注意pring.jpa.hibernate.ddl-auto的值可以是none、create、update、create-drop。具體參考hibernate的文檔。

創建實體模型

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 classpublic class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String name;private String email;//此處省略get和set}

增加數據訪問接口

com.yuny.jpademo.repository.UserRepository

public interface UserRepository extends PagingAndSortingRepository<User, Long> {}

此接口會自動由spring實現,并且產生對應的實例放在容器中,該實例的名稱為類名首字母小寫userRepository。

創建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;@RestControllerpublic class UserController { @Autowired private UserRepository userRepository;//測試插入新的數據 @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 '保存成功'; }//測試獲取全部的數據 @GetMapping(path='/all') public Iterable<User> getAllUsers() {return userRepository.findAll(); }}

測試

運行SpringBootJpademoApplication后,訪問http://localhost:8080/add測試。結果如下:

Spring boot基于JPA訪問MySQL數據庫的實現

數據庫顯示插入數據成功

Spring boot基于JPA訪問MySQL數據庫的實現

訪問http://localhost:8080/all 測試

Spring boot基于JPA訪問MySQL數據庫的實現

總結

在沒用使用jpa支持的時候,我們的代碼要定義IUserDao(持久層接口)、IUserDaoImpl(持久層實現類)、IUserService(業務層接口)等,這樣每寫一個實體類,都要衍生出多個類來進行操作。

而在Spring boot 中使用JPA,只需要聲明一個接口就可以了。

案例代碼

https://github.com/junyanghuang/spring-boot-samples/tree/master/spring-boot-jpademo

到此這篇關于Spring boot基于JPA訪問MySQL數據庫的實現的文章就介紹到這了,更多相關Springboot JPA訪問MySQL內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产精品一区二区在线 | 午夜久久久 | 黄色片免费网站 | 欧美视频三区 | 色婷婷六月 | 久久免费视频观看 | 亚洲国产日韩在线 | 亚洲精品乱码久久久久久蜜桃91 | 欧美一级特黄视频 | 91丨九色丨蝌蚪丨丝袜 | 乳大翘臀1v1h糙汉 | 国产黄网 | av一级在线 | 日本美女毛茸茸 | 性欧美8khd高清极品 | 午夜在线视频 | 亚洲狠狠干| aaa亚洲| 亚洲日本精品 | 荤话粗俗h高h重口 | 在线色综合| 国产a视频 | 成人福利在线观看 | 免费在线观看av网站 | 亚洲精彩视频 | 一区二区三区成人 | 亚洲一区二区国产精品 | 91亚洲视频| 亚洲天堂一区二区三区 | 国产欧美另类 | 激情高潮到大叫狂喷水 | 亚洲 欧美 激情 另类 校园 | 色爽 | 性做久久久久久久免费看 | 色综合天天综合网天天狠天天 | 天天射天天舔 | 天天看天天射 | 美国特色黄a大片 | 国产www| 亚洲免费视频观看 | 国产精品伦理一区 |