柚子鈞
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    2
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 什麼是 ORM , 認識物件關聯對映 *本篇文章為自我複習整理,參考並擷取以下標註的三篇文章* ## ORM( Object Relational Mapping)物件關聯對映 在網站開發結構中,「資料庫」和「 Model 」兩者之間, ORM 的一個特性 : 透過程式語言(Ruby, Java), 去操作資料庫語言( SQL ),操作上並非直接去寫原始的SQL, 寫起來有點像 SQL 的 Ruby 程式碼: ```shell= users = User.where(age: 30) ``` 簡單來說,它是一個幫助使用者更簡便、安全的去從資料庫讀取資料。 並讓開發者得以像操作物件那般存取資料庫中的數據。使用 ORM 可以讓其專注在邏輯撰寫 具體來說就是: * 將類別 (Classes) 對應到 資料表 (Tables) * 將物件 (Objects) 對應到 資料表中的一筆記錄 (Rows in a table) * 將屬性 (Attributes) 對應到資料表中的欄位 (Columns in a table) ![](https://i.imgur.com/MA18v0s.png) [圖片源自](https://ithelp.ithome.com.tw/articles/10241920) # ORM 優缺點 以下收集了幾項優缺點 ### 優點 * 通用、廣泛性 意思就是透過 ORM 可以簡單輕鬆在各語言間,因 ORM 而不需要為資料庫不同差異性煩惱重寫,所以說,以相同的語法來操作各種資料庫系統,讓開發者可以更專注於邏輯上。 ![](https://i.imgur.com/1KkcA2W.png) [圖片擷取自](https://ithelp.ithome.com.tw/articles/10207752) * 安全性 有一種常見的網路攻擊叫做 SQL 注入( SQL injection ), 舉例來說: 就是駭客在傳輸到網站伺服器裡的資料直接寫 SQL , 而我們網站某段 SQL 直接讀取該駭客傳來的資料並執行, 如果傳來的是正常的資料如 Email ,就會沒事, 但如果傳來的是 SQL 語句,且包含『 Delete 』這種會害死大家的詞, 那網站的資料就可能會被惡意移除。 所以說使用 ORM 最大的優點就是在此,隱藏了 SQL 語法,進而規避直接撰寫 SQL 時容易發生的 SQL Injection 問題。 但如果是透過 ORM 的方式, 就變成是操作程式語言,像文章開頭的例子, where 的 age 可以改成吃變數,如 query_age, 但程式就會自動判斷,query_age 內如果是奇怪的值, 像是 “Update…” 等等之類的,就會自動擋掉。 有了這些初步的防堵或者安全性檢查,工程師就只需要專注於如何處理資料。 * 簡化、程式碼乾淨 對於開發團隊來說,採用 ORM 可以讓成員彼此之間的程式碼的風格趨於統一易讀。 如果用 SQL 語句完整寫完會是: ```shell= Select * From users Where age = 30 ``` 比用 ORM 版本的囉唆多了,ORM 版本會簡化,幾乎都會用到的 From 和 Select 的部分。這個優點也是頗直覺的,因為ORM比原生的SQL看起來簡化了許多。 ### 缺點 * 效能 多了『把程式語言轉譯成 SQL語言』這項工作,多做了一層自然會有代價, 當達成了方便性,通常會反映在效能上。不過各大程式語言的 ORM 都有持續改善。 但隨著時間過去,效能高低與否反倒跟如何撰寫出易懂的邏輯比較相關,緩慢的效能大多都是 SQL 語句設計不良,而較少是因為使用 ORM 的關係。 * 遇到複雜的SQL... 雖說 ORM 可以大幅降低開發者操作資料庫的難度,但若有較為複雜的查詢需求,那麼撰寫原生的 SQL 還是必須的。對於複雜的查詢,ORM 的使用上就較為力不從心。 如果是跨好幾個表格,且要針對部分欄位做 Sum, count 等工作, 那就變成除了 ORM 的寫法外,還要額外寫入原生的 SQL 語法。 例如,SQL 有個詞 between ,可以篩選某個區間的值, 但是 Ruby 的 ORM 沒有支援,所以只能寫成這樣: ```shell= User.where(“age between ? and ?”,10,30) ``` 在 where 內,直接寫原生的 SQL 語句,所以說遇到複雜的還是要好好熟悉 SQL 語法。 # 參考文獻 [資料庫設計概念 - ORM](https://ithelp.ithome.com.tw/articles/10207752) [蝦米是ORM?](https://ithelp.ithome.com.tw/articles/10241383) [ORM vs. SQL](https://ithelp.ithome.com.tw/articles/10241920)

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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