SpringBoot註解學習之 Accessors

2021-10-08 07:47:51 字數 1805 閱讀 2322

@accessors

包含的屬性

fluent(布林型)chain(布林型)prefix(string型)

作用域

既可以註解在類上也可以註解在屬性上

作用

用來配置lombok如何產生和顯示getter和setter的方法。

具體用法:

1:fluent(布林型)

預設為false,當設定為true時,表示生成的setter和getter方法沒有字首set和get,且setter方法返回的是當前物件。具體效果如下:

@data

@accessors(fluent = true)

public class person

public person id(int id) {}

public string name() {}

public person name(string name) {}

}

如**中所示,生成的getter方法不是getid(){},而是id(){};setter方法不是void setid(){},而是person id(int id){}

2:chain(布林型)

預設為false(注:但是當fluent為true時,其預設為true),生成的setter方法是void型別;如果設定為true生成的setter方法返回this(當前物件)。

@data

@accessors(chain = true)

public class person

public person setname(string name) {}}

//這裡加個舉例說明,按如下呼叫後 就將person物件的id設定成1,姓名設定成 "知其然"。

person person=new person();

person.setid(1).setname("知其然");

而且我們在進行屬性賦值的時候,由於set方法返回的是當前物件,所以可以進行連續呼叫set方法進行賦值,如上述**所示。一般我們在進行屬性賦值的時候,由於set方法返回值是void型別,所以需分開賦值person.setid(1);person.setname("知其然");

但是當設定chain為true的時候,set方法返回的是當前物件,則在進行屬性賦值則可以連續進行,如:person.setid(1).setname("知其然");

3:prefix(string型)

可以指定字首,生成getter和setter方法時會去掉指定的字首(遵守駝峰命名)。效果如下:

@data

@accessors(prefix = "zqr")

class person

public void setid(int id) {}

public string getname() {}

public void setname(string name) {}

}

相當於字串擷取功能,在生成getter和setter方法的時候,會自動擷取去除指定字首,然後加上get與set;

如上**,生成的不是getzqrid(),而是getid()。遵守駝峰命名規則

springBoot學習之註解小記

controller responsebody restcontroller 當方法或者類上增加了 responsebody註解時,所返回的引數為json格式。當沒有此註解時,引數會以鍵值對的形式傳入。data 提供該類所有屬性的getting和setting方法。cookievalue 用來獲取c...

學習筆記之springboot註解

2,controller 程式入口 import org.springframework.stereotype.controller 處理請求位址對映,可以用在類或方法上。用於類上,表示類中的所有響應請求的方法都是以該位址作為父路徑。4,responsebody import org.springf...

springboot 註解總結

springboot註解知識點歸納 當實現rest ful web services時,response將一直通過response body傳送。controller 用於定義控制器類,在spring 專案中由控制器負責將使用者發來的url請求 到對應的服務介面 service層 restcontr...