lwliu
    • 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 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

    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Lombok 使用教學 ## 前言 使用 Lombok 可以完成平常開發常使用的邏輯,可以自動完成,而不需要重複撰寫很多同樣程式碼,加快開發速度,專注商業邏輯。 ## 目錄 * [介紹/基本概念](#介紹/基本概念) * [在 pom.xml 新增依賴](#在-pom.xml-新增依賴) * [新增以下檔案](#新增以下檔案) * [常用註解](#常用註解) * [參考資料](#參考資料) ## 介紹/基本概念 ### 在 pom.xml 新增依賴 ```xml= <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> ``` ### 新增以下檔案 ![Lombok](https://i.imgur.com/7Ze3t5H.jpg) Food.java ```java= package data; public class Food { private Long id; private String name; } ``` Main.java ```java= package main; public class Main { public static void main(String[] args) { } } ``` ### 常用註解 以下將以 Food.java 實作 Lombok #### 1. @Getter / @Setter ```java= package data; import lombok.Getter; import lombok.Setter; @Getter @Setter public class Food { private Long id; private String name; } ``` 等同於 ```java= package data; public class Food { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ``` 會自動產生 getter 和 setter ,不需要另外撰寫。 :::info 🔔 也可以將 @Getter 和 @Setter 寫在參數上,針對該參數產生 getter 和 setter ::: #### 2. @ToString ```java= package data; import lombok.ToString; @ToString public class Food { private Long id; private String name; } ``` 等同於 ```java= package data; public class Food { private Long id; private String name; @Override public String toString() { return "Food{" + "id=" + id + ", name='" + name + '\'' + '}'; } } ``` 會自動產生 toString ,不需要另外撰寫 #### 3. @EqualsAndHashCode ```java= package data; import lombok.EqualsAndHashCode; @EqualsAndHashCode public class Food { private Long id; private String name; } ``` 等同於 ```java= package data; import java.util.Objects; public class Food { private Long id; private String name; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Food food = (Food) o; return Objects.equals(id, food.id) && Objects.equals(name, food.name); } @Override public int hashCode() { return Objects.hash(id, name); } } ``` 會自動產生 equals 和 hashCode ,不需要另外撰寫 :::info 🔔 Q : 為什麼只有一個整體的 @EqualsAndHashCode 注解,而不是分開的兩個 @Equals 和 @HashCode? A : 在 Java 中有規定,當兩個 object equals 時,它們的 hashcode 一定要相同,反之,當 hashcode 相同時,object 不一定 equals。所以 equals 和 hashcode 要一起 implement,以免發生違反 Java 規定的情形 ::: #### 4. @NoArgsConstructor ```java= package data; import lombok.NoArgsConstructor; @NoArgsConstructor public class Food { private Long id; private String name; } ``` 等同於 ```java= package data; public class Food { private Long id; private String name; public Food() { } } ``` 會自動生成一個沒有參數的 constructor #### 5. @AllArgsConstructor ```java= package data; import lombok.AllArgsConstructor; @AllArgsConstructor public class Food { private Long id; private String name; } ``` 等同於 ```java= package data; public class Food { private Long id; private String name; public Food(Long id, String name) { this.id = id; this.name = name; } } ``` 會自動生成一個所有參數的 constructor :::danger 🔔 使用 @AllArgsConstructor 如果需要使用無參數建構子 constructor ,要記得補上 @NoArgsConstructor 。 ::: #### 6. @RequiredArgsConstructor 只有在第 6 節先將 id 加上 final ```java= package data; public class Food { private final Long id; private String name; } ``` 加上 @RequiredArgsConstructor ```java= package data; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public class Food { private final Long id; private String name; } ``` 等同於 ```java= package data; public class Food { private final Long id; private String name; public Food(Long id) { this.id = id; } } ``` 會自動將 final 參數建立 constructor #### 7. @Data ```java= package data; import lombok.Data; @Data public class Food { private Long id; private String name; } ``` 等同於 ```java= package data; import lombok.*; @Getter @Setter @ToString @EqualsAndHashCode @RequiredArgsConstructor public class Food { private Long id; private String name; } ``` 會自動產生 @Getter , @Setter , @ToString , @EqualsAndHashCode , @RequiredArgsConstructor ,這個註解常被使用在 Spring Data JPA 的 Entity 上 #### 8. @Value 跟 @Data 相似,但是他會把所有的變數都設成 final 的,其他的就跟 @Data 一樣 , 少了 @Setter 功能。 會自動產生 @Getter , @ToString , @EqualsAndHashCode , @RequiredArgsConstructor #### 9. @Builder ```java= package data; import lombok.Builder; @Builder public class Food { private Long id; private String name; } ``` Main.java ```java= package main; import data.Food; public class Main { public static void main(String[] args) { Food food = Food.builder().id(1L).name("PORK").build(); } } ``` 即可使用 builder 建立物件,不需要特別使用 set 或建構子建立。 #### 10. @Slf4j 請見[Log4J 和 Slf4J 的關係與結合使用-第 2 種方式](https://hackmd.io/7qmmjTg5QASduGNkMpmqBg) ## 參考資料 [Java - 五分鐘學會 Lombok 用法 ](https://kucw.github.io/blog/2020/3/java-lombok/)

    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