經過第一部分,瞭解了Maven項目的文件分佈。前端
經過第二部分,瞭解了Spring MVC。數據庫
到了Spring boot中,一切都獲得了簡化,能夠將立刻要用到的文件與Spring MVC進行對應,這樣比較好理解MVC設計模式。設計模式
Spring boot層次
DAO層:data acess object。是數據庫的訪問的對象。(M層)數組
Service層:對數據庫的對象進行統一的操做。業務邏輯層。(M層)瀏覽器
Controller層:接受用戶請求並返回數據。(C層)app
Base層:若Service層和Controller層鏈接過與緊密,則加入Base層。網站
View層:負責前端顯示。(V層)ui
增長url訪問路徑
1 @Controller 2 public class deal { 3 @ResponseBody 4 @RequestMapping(path = {"/", "/hello"}) 5 public String index() { 6 return "Hello World"; 7 } 8 }
探究:url
Ctrl+左鍵進入RequestMapping,它對應到RequestMapping接口。spa
調用@RequestMapping(path = {"/", "/hello"})方法,根據視頻它對應到下面的方法:
1 @AliasFor("value") 2 String[] path() default {};
其方法能夠將{"/", "/hello"}轉化成一個String數組。用戶訪問/或者/hello均可行。
其效果是:
在瀏覽器中輸入127.0.0.1:8080/或者是127.0.0.1:8080/hello均可以看到hello world的頁面。
值得注意的是,在controller文件中沒有定義path變量,可是在@RequestMapping卻能夠用,說明@RequestMapping實際是在其餘定義好了path變量的文件中執行,而不是controller文件中執行。
並且更有意思的是,將path變量改爲其它變量dd,出錯:The attribute dd is undefined for the annotation type。
給url增長參數
有時候網站url欄出現的網址爲:127.0.0.1:8080/profile/2/1?key=12334x&type=12 這種形式。實現代碼以下:
1 //{goupid},{userid}爲路徑變量,訪問時能夠自行修改。而profile則是固定的路徑了。
2 @RequestMapping(path = {"/profile/{groupid}/{userid}"}) 3 @ResponseBody
4 //PathVariable是路徑中的一個變量。路徑中的值類型在本程序中轉成對應的gp,ui的String類型 5 public String profile(@PathVariable("groupid") String gp, 6 @PathVariable("userid") String ui,
7 //RequestParam不是路徑中的變量,而是額外加上的變量。url中的值轉化成本程序的int類型和String類型的type和key變量。 8 @RequestParam(value = "type", defaultValue = "1") int type, 9 @RequestParam(value = "key", defaultValue = "nowcoder") String key) { 10 //最後把url變量顯示在網頁中。
11 return String.format("GID{%s}, UID{%s}, TYPE{%d}, KEY{%s}", gp, ui, type, key); 12 }
效果:
訪問http://127.0.0.1:8080/profile/2/1?key=12334x&type=1
顯示:
GID{2}, UID{1}, TYPE{1}, KEY{12334x}