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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

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

本文展示如何通過(guò)JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)。

JPA全稱Java Persistence API,即Java持久化API,它為Java開發(fā)人員提供了一種對(duì)象/關(guān)系映射工具來(lái)管理Java應(yīng)用中的關(guān)系數(shù)據(jù),結(jié)合其他ORM的使用,能達(dá)到簡(jiǎn)化開發(fā)流程的目的,使開發(fā)者能夠?qū)W⒂趯?shí)現(xiàn)自己的業(yè)務(wù)邏輯上。

Spring boot結(jié)合Jpa 能夠簡(jiǎn)化創(chuàng)建 JPA 數(shù)據(jù)訪問(wèn)層和跨存儲(chǔ)的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,無(wú)需再寫實(shí)現(xiàn)類,就可以實(shí)現(xiàn)對(duì)象的CRUD操作以及分頁(yè)排序等功能。

環(huán)境要求 Mysql數(shù)據(jù)庫(kù)5.6以上 JDK1.8以上 開發(fā)工具使用STS創(chuàng)建項(xiàng)目

使用STS創(chuàng)建項(xiàng)目

Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

選擇web和JPA依賴

Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

添加MySQL數(shù)據(jù)庫(kù)驅(qū)動(dòng)依賴

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

application.properties中配置數(shù)據(jù)庫(kù)連接信息

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

以上數(shù)據(jù)庫(kù)連接信息根據(jù)實(shí)際情況進(jìn)行調(diào)整。

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

創(chuàng)建實(shí)體模型

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}

增加數(shù)據(jù)訪問(wèn)接口

com.yuny.jpademo.repository.UserRepository

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

此接口會(huì)自動(dòng)由spring實(shí)現(xiàn),并且產(chǎn)生對(duì)應(yīng)的實(shí)例放在容器中,該實(shí)例的名稱為類名首字母小寫userRepository。

創(chuàng)建Controller測(cè)試

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;//測(cè)試插入新的數(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 '保存成功'; }//測(cè)試獲取全部的數(shù)據(jù) @GetMapping(path='/all') public Iterable<User> getAllUsers() {return userRepository.findAll(); }}

測(cè)試

運(yùn)行SpringBootJpademoApplication后,訪問(wèn)http://localhost:8080/add測(cè)試。結(jié)果如下:

Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

數(shù)據(jù)庫(kù)顯示插入數(shù)據(jù)成功

Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

訪問(wèn)http://localhost:8080/all 測(cè)試

Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

總結(jié)

在沒用使用jpa支持的時(shí)候,我們的代碼要定義IUserDao(持久層接口)、IUserDaoImpl(持久層實(shí)現(xiàn)類)、IUserService(業(yè)務(wù)層接口)等,這樣每寫一個(gè)實(shí)體類,都要衍生出多個(gè)類來(lái)進(jìn)行操作。

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

案例代碼

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

到此這篇關(guān)于Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot JPA訪問(wèn)MySQL內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 精品伊人 | 国产一区二区高清在线 | 91影库 | 在线中文视频 | 欧美精品在线免费观看 | 污免费网站 | 日本aa毛片a级毛片免费观看 | 97在线播放| 欧美日韩一| 超黄视频网站 | 99热播精品 | 毛片免费观看视频 | av天天看 | 黄色一级大片在线观看 | 日韩国产黄色片 | 龙珠z在线观看 | www.成人.com | 国产成人高清视频 | 欧美日韩亚洲国产综合 | 色婷婷影院 | 狠狠干综合视频 | 亚洲综合视频 | 亚洲一区二区免费视频 | 国产不卡在线观看 | 亚洲美乳中文字幕 | 久久国产精品久久久久 | 一区二区三区国产 | 成av人电影在线 | 国产精品99久久久久久动医院 | 日韩欧美中文在线 | 黄色大片免费网站 | 日韩av在线一区 | 在线国产视频 | 精品一区电影 | 激情国产| 国产成人99av超碰超爽 | 日韩欧美在线观看一区 | 亚洲午夜精品 | 一级欧美 | 午夜精品一区二区三区在线 | 中文视频在线 |