Kai Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 六角鼠年鐵人賽 Week 25 - Spring Boot - Spring Data & JPA 介紹 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ## Bill Gates :::info If you can't make it good, at least make it look good. ::: ## 主題 Spring Data JPA 是在 Spring 框架中最常被用來與資料庫溝通的套件,但許多人可能常常會搞混 ORM、Hibernate/myBatis、JPA、Spring Data JPA 之間的關係,因此 Kai 在這周特別開一篇想來跟大家一起理解清楚到底四種東西之間的關係。 希望大家在看完文章後能夠跟 Kai 一樣豁然開朗~ ## JPA **Jakarta Persistence API**,我們熟悉的是稱呼為 **Java Persistence API**,它是昇陽公司(SUN) 於 2006 年提出的一套基於 ORM 的規範,後被納入 JCP 組織發布的 Java EE 標準中。 內部是一系列的介面與抽象類程式,負責定義與 DB 連接、指令執行、持久化的方式等。但它並不提供任何實作,因此必須倚賴於其他實作這些規範的 ORM 套件,例如:Hibernate、myBatis 等。 這些 ORM 透過註解或 XML 的方式讓開發者得以讓物件去映射 DB 的資料表,並在執行期間將其取得的實體物件給持久化,達到快速開發,資料持久化的目的。 JPA 的特性如下: 1. 標準化 JPA 是 JCP 組織發佈的 Java EE 標準之一,因此任何聲稱符合 JPA 標準的框架都遵循同樣的架構,提供相同的訪問API,這保證了基於JPA開發的企業應用能夠經過少量的修改就能夠在不同的JPA框架下運行。 2. 容器級特性的支持 JPA框架中支持大數據集、事務、併發等容器級事務,這使得 JPA 超越了簡單持久化框架的侷限,在企業應用發揮更大的作用。 3. 簡單方便 JPA的主要目標之一就是提供更加簡單的編程模型:在JPA框架下創建實體和創建Java 類一樣簡單,沒有任何的約束和限制,只需要使用 javax.persistence.Entity進行註釋,JPA的框架和接口也都非常簡單,沒有太多特別的規則和設計模式的要求,開發者可以很容易的掌握。JPA基於非侵入式原則設計,因此可以很容易的和其它框架或者容器集成 4. 查詢能力 JPA的查詢語言是面向對象而非面向數據庫的,它以面向對象的自然語法構造查詢語句,可以看成是Hibernate HQL的等價物。JPA定義了獨特的JPQL(Java Persistence Query Language),JPQL是EJB QL的一種擴展,它是針對實體的一種查詢語言,操作對象是實體,而不是關係數據庫的表,而且能夠支持批量更新和修改、JOIN、GROUP BY、HAVING 等通常只有 SQL 才能夠提供的高級查詢特性,甚至還能夠支持子查詢。 5. 高級特性 JPA 中能夠支持面向對象的高級特性,如類之間的繼承、多態和類之間的複雜關係,這樣的支持能夠讓開發者最大限度的使用面向對象的模型設計企業應用,而不需要自行處理這些特性在關係數據庫的持久化。 ## ORM 為 **Object-Relational Mapping** 的簡稱,表示物件映射。 在以往不使用 ORM 的方式時,開發人員需要透過這些動作:連結 DB、組合 SQL 指令、設定取出資料的欄位與其類型、切斷連結(or 保存進 connection pool),去執行 CRUD 的 DB指令。 在少數需要操作 DB 的需求狀況下,這樣的開發方式也許比使用 ORM 的方式還要快。 但在比較大型、需求往往多面且複雜的系統架構中,尤其現在多採用分散式架構、前後分離的方式,快速的實作每一個 API 成了首頁目標,因此並沒有太多時間讓開發者慢慢寫著連接、設定取值等重複的工。 為此,ORM 替開發者省下這些開發時間,開發者直接專注於開發需求接口、處理 SQL、回送需求資料等部分,快速完成一個 API。 Hibernate 和 MyBatis 都是 Spring 專案中常見的 ORM 套件。 ORM 實際上的處理的部分包含了: 1. 通過 ORM 的方式將物件映射到 RDBMS 中的資料表 2. 透過實作 JPA 的方法直接對資料表進行資料處理上的操作,這當中包含了連結 DB、取資料等重工,也是幫助開發者省下時間的部分。 3. 若於 ORM 的物件內有設定與資料表中相同的 Key 或 default 等限制條件訊息,則相關部分的 SQL 處理,ORM 也可以在執行前就先行判斷並跳出相關警告或錯誤訊息 簡單來說,ORM 意在建立一個物件與 RDBMS 中資料表的關係,透過這層關係,讓開發者可以做到操作物件等同於操作資料表資料的目的。 ## Spring Data Spring Data 的目的是為 DB 操作提供一個一致的、基於 Spring 開發的模型。背後由許多公司和開發人員共同開發完成的項目,支援 RDBMS、NoSQL、Map-Reduce 框架,甚至是 Cloud 服務的數據服務。 簡單來說,Spring Data 可以說是一個傘狀的、廣納的套件,底下包含了支援目前大多數的數據儲存軟體或服務的操作方法可以使用。 目前來說 Spring Data 並沒有停止開發,它還在持續擴展中,這使得使用 Spring Data 的開發人員幾乎不用擔心會遇到無法操作的數據軟體或服務。從而使得開發人員能夠更加專注於開發需求,不需要因為面對不同的項目還需要費心費神找適合的套件。 Spring Data 有以下幾個特徵: 1. 強大的 Repository 和自定義的映射抽象類 2. Repository 直接提供動態查詢的處理 3. 提供已實現 domain 為基礎的類物件的基本設定 4. 支持透明審核 5. Repository 可以集成其他客製化程式 6. Spring 可以透過 Java Config 設置或和自定義 XML 名稱方式進行集成 7. 與 Spring MVC 高度的相性 8. 實驗性支持跨儲存物件的資料持久化 ### #補充何謂 Repository **Repository** 原意為倉庫,在 Spring 中用於隔開業務與資料面的類別,內部封裝的是對資料操作的邏輯,實作交由底層 ORM 套件完成,在真正意義上實現物件導向設計,實現所有物件的持久化處理,不因底層 ORM 套件更動而改變。原先常用的 **DAO 類** 是 MVC 模式中與資料處理密切相關的類別,相比 **Repository** 來說,會面臨無法完全切隔開業務邏輯與資料操作的部分。 ## Spring Data JPA 說了這麼多終於進入主題了... Spring Data JPA 到底是什麼呢? 它就是 -- Spring 基於 ORM 框架以及 JPA 的規範下,實作了 JPA 功能的框架,並專給 Spring 使用,意在於取代傳統太多的 ORM 套件。 > Spring 就是 Spring,看哪個東西好,就自己幹一個取代掉... (汗) 簡單來說,你完全可以直接忽略上面長長一段 ORM、JPA、Spring Data 的介紹,直接使用 Spring Data JPA 做為系統與 DB 溝通的橋樑套件就行了! 而 Spring Data JPA,基於 ORM 框架的原理與 JPA 規範下,自然具備了所有的優點: 1. 與 Spring 的高度相性 2. 支持 Querydsl 以便保護 JPA 的指令 3. Domain 類的透明審核 4. 支持分頁、動態查詢、自定義指令等功能 5. 支持 XML 的實體映射方式 6. 支援 @Query 的 annotation 處理方式 7. 支援 @EnableJpaRepositories 的 annotation 進行 config 配置 > Querydsl 是一種透過 QueryFactory 物件處理其 Query 組成的方式,開發人員不再需要寫重複的 SQL 指令,只需要透過 QueryFactory 提供的方法,將值或條件帶入方法中即可完成參數的設定,在執行前,這些設定好的參數會被自動組裝成完整的 SQL 指令 最重要的是!! 不需要再寫 DAO 啦!! ## 結語 :::danger 這篇很簡單的介紹了一下幾個常見的名詞,以及最後帶出的 Spring Data JPA 部分,下一篇就來實作演練一番,來看看 Spring Data JPA 到底幫助了開發者多少事情! [六角鼠年鐵人賽 Week 26 - Spring Boot - Spring Data & JPA 範例](/upa5YkRRQ8qyAW5gieM85A) ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Spring Boot`,`Spring Data`,`w3HexSchool`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully