溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2021-06-24 13:44:43 來源:億速云 閱讀:146 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

JPA全稱Java Persistence API,即Java持久化API,它為Java開發(fā)人員提供了一種對(duì)象/關(guān)系映射工具來管理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ù)訪問層和跨存儲(chǔ)的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,無需再寫實(shí)現(xiàn)類,就可以實(shí)現(xiàn)對(duì)象的CRUD操作以及分頁排序等功能。

環(huán)境要求

  • Mysql數(shù)據(jù)庫5.6以上

  • JDK1.8以上

  • 開發(fā)工具使用STS

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

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

Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法

選擇web和JPA依賴

Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法

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

<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ù)實(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ù)訪問接口

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后,訪問http://localhost:8080/add測(cè)試。結(jié)果如下:

Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法

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

Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法

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

Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法

總結(jié)

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

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

案例代碼

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

“Spring boot中JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI