做者: 梁小生0101php
Spring Boot + Vue.js 先後端涉及基本概念介紹,搭建記錄,本文會列舉出用到環境和工具,而且提供源碼。css
前端工具和環境:html
後端工具和環境:前端
Demo 地址:http://101.132.124.171:8000/aboutvue
1. Vue是一套用於構建用戶界面的漸進式框架,網址:cn.vuejs.org/node
2. Vue在Github的歡迎度jquery
3. 不須要操做Dom,實現了MVVMios
// jquery的操做 $("#test3").val("Dolly Duck"); // Vue的操做 MVVM,實現了雙向綁定
4. 學習成本低,文檔淺顯易懂
vue create vue-hello-world (命令行) vue ui (界面)
3. 在建立好項目之後,運行如下命令將能看到初次項目建立的界面
cd vue-hello-world yarn serve
4. 默認狀況下,在 瀏覽器訪問 http://localhost:8080/ 將能看到以下界面: Vue 相關結構和生命週期介紹 目錄結構以下圖: 單個.vue文件的組成部介紹
<template> <!--html--> </template> <script> //js </script> <style> /* css style */ </style>
組件化應用構建git
使用小型、獨立和一般可複用的組件構建大型應用,一個頁面如搭積木同樣 Vue的生命週期以下圖:鉤子方法: 模板方法的執行結果,該方法就叫作鉤子方法,我的理解:影響了模板的執行,把函數勾住了,這個方法就是鉤子函數。
鉤子函數github
<div id="app"> {{ message }} </div> data: { message: 'Hello Vue!' }
條件渲染
<div id="app-3"> <p v-if="seen">如今你看到我了</p> </div> data: { seen: true }
循環渲染
<div id="app-4"> <ol> <li v-for="todo in todos"> {{ todo.text }} </li> </ol> </div> data: { todos: [ { text: '學習 JavaScript' }, { text: '學習 Vue' }, { text: '整個牛項目' } ] }
監聽事件
能夠用 v-on 指令監聽 DOM 事件,並在觸發時運行一些 JavaScript 代碼。
<div id="example-2"> <!-- `greet` 是在下面定義的方法名 --> <button v-on:click="greet">Greet</button> </div> methods: { greet: function () { // `this` 在方法裏指向當前 Vue 實例 alert('Hello ' + this.name + '!') } }
計算屬性緩存 vs 方法
<div id="example"> <p>Original message: "{{ message }}"</p> <p>Computed reversed message: "{{ reversedMessage }}"</p> </div> var vm = new Vue({ el: '#example', data: { message: 'Hello' }, computed: { // 計算屬性的 getter reversedMessage: function () { // `this` 指向 vm 實例 return this.message.split('').reverse().join('') } }, methods: { // 方法 reversedMessage: function () { return this.message.split('').reverse().join('') } } })
數據變化,watch
var vm = new Vue({ el: '#demo', data: { firstName: 'Foo', lastName: 'Bar' }, computed: { // 當兩個值變化時,將會觸發此函數 fullName: function () { return this.firstName + ' ' + this.lastName } } })
表單輸入綁定
<input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p>
縮寫 v-bind 縮寫
<!-- 完整語法 --> <a v-bind:href="url">...</a> <!-- 縮寫 --> <a :href="url">...</a>
v-on 縮寫
<!-- 完整語法 --> <a v-on:click="doSomething">...</a> <!-- 縮寫 --> <a @click="doSomething">...</a>
// 可提供懶加載 const router = new VueRouter({ routes: [ { path: '/user/:userId', name: 'user', component: User } ] }) <!--html跳轉--> <router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link> // js跳轉 router.push({ name: 'user', params: { userId: 123 }})
// get請求 axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); // post 請求 axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });在學習完以上知識之後,將能使用Vue作出簡單的頁面運用
擴展:TypeScript、Vue組件間傳值、Mock、Vuex、調試、JavaScript的同步異步,做用域、ES六、部署(打包、優化、部署在靜態服務器上、node中間層)、虛擬DOM、Http的get和post等。
梭哈 PC端:iview、小程序:mpvue,移動端:muse-ui,桌面端:Electron + Vue JS走天下。
ivied:https://www.iviewui.com/components/page
mpvue:http://mpvue.com/
muse-ui:https://muse-ui.org/#/zh-CN/list
Electron + Vue JS:https://github.com/SimulatedGREG/electron-vue
JVM 虛擬機
和前端交互 1. 前端的Http請求會到controller這一層,而controller層根據相應路由信息註解會跳轉到相應的類。
// 如:/api/user 的get請求將會被 UserQry() 函數處理 @RequestMapping("/api") public class UserController { @RequestMapping(value ="/user", method = RequestMethod.GET) public List<User> UserQry() { return userService.getUser(); } }
2. 在框架通過處理之後,最終調用的是mapper層。
@Select("select * from user") List<User> getUser();
3. 在執行相應的Sql之後,將會依次返回到controller層,而後在Http的返回中將會以Json串對象返回給前端的調用方。
4. 前端在Http的response中拿到返回的值,而後再進行一些處理。
// Spring 的操做 package com.yiibai.common; public class Customer { private Person person; public Customer(Person person) { this.person = person; } public void setPerson(Person person) { this.person = person; } //... } package com.yiibai.common; public class Person { //... } // Spring 的配置Bean的xml <bean id="customer" class="com.yiibai.common.Customer"> <property name="person" ref="person" /> </bean> <bean id="person" class="com.yiibai.common.Person" /> // Spring 的註解方式 public class Customer { @Autowired private Person person; }
5. @RequestMapping
告訴Spring這是一個用來處理請求地址映射的註解。 6. @Autowired 能夠對類成員變量、方法及構造函數進行標註。從IoC容器中去查找,並自動裝配。(去除@Autowired能夠運行一下試試) 7. Mybatis的@Mapper 註解的接口生成一個實現類// 非RESTful接口 api/getfile.php - 獲取文件信息,下載文件 api/uploadfile.php - 上傳建立文件 api/deletefile.php - 刪除文件 // 只須要api/users這一個接口 GET http://localhost:8080/api/users (查詢用戶) POST http://localhost:8080/api/users (新增用戶) PUT http://localhost:8080/api/users (更新用戶) DELETE http://localhost:8080/api/users (刪除用戶)
Restful好處:
URL具備很強可讀性的,具備自描述性
規範化請求過程和返回結果
資源描述與視圖的鬆耦合
可提供OpenAPI,便於第三方系統集成,提升互操做性
提供無狀態的服務接口,下降複雜度,可提升應用的水平擴展性
擴展
JAVA的內存模型(非線程安全)、Linq、JWT、Redis、WebSocket、單點登陸(SSO)、消息隊列// 定義服務接口標準 public interface DemoService { String sayHello(String name); } // 生產者項目引用並實現 @Service public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "Hello, " + name + " (from Spring Boot)"; } } // 消費者引用而後調用 @RestController public class DemoConsumerController { @Reference private DemoService demoService; @RequestMapping("/sayHello/{name}") public String sayHello(@PathVariable("name") String name) { return demoService.sayHello(name); } }
Hystrix 斷路器。爲了保證其高可用,單個服務一般會集羣部署。若是單個服務出現問題,調用這個服務就會出現線程阻塞,此時如有大量的請求涌入,Servlet容器的線程資源會被消耗完畢,致使服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統形成災難性的嚴重後果。
zuul 路由網關。Zuul的主要功能是路由轉發和過濾器。好比/api/user轉發到到user服務,/api/shop轉發到到shop服務
Spring Cloud Config 在服務數量巨多時,爲了方便服務配置文件統一管理,實時更新,須要分佈式配置中心組件。
Spring Cloud Sleuth 功能就是在分佈式系統中提供追蹤解決方案。
有熱門推薦????
酒後系列:被某廠面試官吊打後酒後整理的JVM乾貨
Google 6面,最終仍是掛了…
爲何單線程的Redis可以達到百萬級的QPS?
乾貨:一文讀懂客戶端請求是如何到達服務器的