欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年06月24日 11:25:17   作者:鈞陽(yáng)  
本文主要介紹了Spring boot基于JPA訪問(wèn)MySQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn),Spring boot結(jié)合Jpa 能夠簡(jiǎn)化創(chuàng)建 JPA 數(shù)據(jù)訪問(wèn)層和跨存儲(chǔ)的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,感興趣的可以了解一下

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

JPA全稱Java Persistence API,即Java持久化API,它為Java開(kāi)發(fā)人員提供了一種對(duì)象/關(guān)系映射工具來(lái)管理Java應(yīng)用中的關(guān)系數(shù)據(jù),結(jié)合其他ORM的使用,能達(dá)到簡(jiǎn)化開(kāi)發(fā)流程的目的,使開(kāi)發(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以上
  • 開(kāi)發(fā)工具使用STS

創(chuàng)建項(xiàng)目

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

選擇web和JPA依賴

添加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=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.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 class
public 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;
@RestController
public 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é)果如下:

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

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

總結(jié)

在沒(méi)用使用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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring的TransactionSynchronizationAdapter事務(wù)源碼解析

    spring的TransactionSynchronizationAdapter事務(wù)源碼解析

    這篇文章主要介紹了spring的TransactionSynchronizationAdapter事務(wù)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java中的NumberFormatException異常原因以及解決方案詳解

    Java中的NumberFormatException異常原因以及解決方案詳解

    這篇文章主要介紹了Java中的NumberFormatException異常原因以及解決方案詳解,NumberFormatException 是 Java 中的一個(gè)異常類,通常在字符串轉(zhuǎn)換為數(shù)字的過(guò)程中發(fā)生,它表示一個(gè)無(wú)效的數(shù)字格式,即字符串無(wú)法被正確解析為數(shù)字,需要的朋友可以參考下
    2024-02-02
  • Java緩存ehcache的使用步驟

    Java緩存ehcache的使用步驟

    這篇文章主要介紹了Java緩存ehcache的使用步驟,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • tomcat請(qǐng)求流程源碼解進(jìn)階篇

    tomcat請(qǐng)求流程源碼解進(jìn)階篇

    這篇文章主要為大家介紹了tomcat請(qǐng)求流程源碼解進(jìn)階,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • SpringBoot多環(huán)境配置方式的新手教程

    SpringBoot多環(huán)境配置方式的新手教程

    我們平時(shí)做項(xiàng)目的時(shí)候,一般都會(huì)分幾套環(huán)境,每一套環(huán)境的配置都是不一樣的,所以這篇文章就來(lái)為大家詳細(xì)介紹一下SpringBoot多環(huán)境配置方式,希望對(duì)大家有所幫助
    2023-11-11
  • 詳解如何使用maven生成可以執(zhí)行的jar

    詳解如何使用maven生成可以執(zhí)行的jar

    這篇文章主要介紹了詳解如何使用maven生成可以執(zhí)行的jar,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Java實(shí)現(xiàn)簡(jiǎn)單無(wú)界面五子棋

    Java實(shí)現(xiàn)簡(jiǎn)單無(wú)界面五子棋

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單無(wú)界面五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • spring中WebClient如何設(shè)置連接超時(shí)時(shí)間以及讀取超時(shí)時(shí)間

    spring中WebClient如何設(shè)置連接超時(shí)時(shí)間以及讀取超時(shí)時(shí)間

    這篇文章主要給大家介紹了關(guān)于spring中WebClient如何設(shè)置連接超時(shí)時(shí)間以及讀取超時(shí)時(shí)間的相關(guān)資料,WebClient是Spring框架5.0引入的基于響應(yīng)式編程模型的HTTP客戶端,它提供一種簡(jiǎn)便的方式來(lái)處理HTTP請(qǐng)求和響應(yīng),需要的朋友可以參考下
    2024-08-08
  • Mybatis-Plus注入SQL原理分析

    Mybatis-Plus注入SQL原理分析

    本文主要介紹了Mybatis-Plus注入SQL原理分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • maven工程打包引入本地jar包的實(shí)現(xiàn)

    maven工程打包引入本地jar包的實(shí)現(xiàn)

    我們需要將jar包發(fā)布到一些指定的第三方Maven倉(cāng)庫(kù),本文主要介紹了maven工程打包引入本地jar包的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02

最新評(píng)論