做者: 梁小生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/ 将能看到以下界面:
<template> <!--html--> </template> <script> //js </script> <style> /* css style */ </style>
组件化应用构建git
使用小型、独立和一般可复用的组件构建大型应用,一个页面如搭积木同样钩子方法: 模板方法的执行结果,该方法就叫作钩子方法,我的理解:影响了模板的执行,把函数勾住了,这个方法就是钩子函数。
钩子函数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?
干货:一文读懂客户端请求是如何到达服务器的