---
# System prepended metadata

title: Lombok介紹
tags: [Mybatis-Lombok]

---

# Lombok介紹
###### tags: `Mybatis-Lombok`

```
* Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more. 
```

* java library
* plugs
* build tools
* with one annotation your class

### 使用步驟
#### 1.在IDEA中安裝Lombok插件
![](https://i.imgur.com/5VBe4x8.png)

#### 2.在項目中導入lombok的jar包
```xml=
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>
```

#### 3.在實體類加註解即可
把User下方的方法都刪除，只留屬性並添加@Data
```java=
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private int id;
    private String name;
    private String password;
}
```
就能看到這些沒被寫出來的方法，都透過@Data出現在構造中了
![](https://i.imgur.com/9DBFJuT.png)


### 可使用註解一覽
```java=
@Getter and @Setter
@FieldNameConstants
@ToString
@EqualsAndHashCode
@AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor
@Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger, @CustomLog
@Data
@Builder
@SuperBuilder
@Singular
@Delegate
@Value
@Accessors
@Wither
@With
@SneakyThrows
@val
@var
experimental @var
@UtilityClass
Lombok config system
```
@Data：無參構造、get、set、toString、hasCode、equals
@AllArgsConstructor：有參構造
@NoArgsConstructor：無參構造
@ToString：toString


![](https://i.imgur.com/QhgvUhr.png)
![](https://i.imgur.com/bxfJTvO.png)
