本文項目參考自:https://github.com/boylegu/SpringBoot-vue
這個項目主要講的是一些概念,想要自己實現代碼操作請看:https://www.cnblogs.com/wlovet/p/10980579.html
為了完成此項目你需要會springBoot,mybatis的一些基本操作
運行界面



第一步:搭建前端服務器
①安裝node.js,下載相應版本的node.js,下載地址:https://nodejs.org/en/download/,下載完雙擊安裝,點擊下一步直到安裝完成 ②安裝完成后,附件里選擇命令提示符(或者在開始的搜索框里輸入cmd回車調出命令面板)輸入:node -v回車,出現相應版本證明安裝成功,
node環境已經安裝完成,由於有些npm有些資源被屏蔽或者是國外資源的原因,經常會導致用npm安裝依賴包的時候失敗,所有我還需要npm的
國內鏡像---cnpm。在命令行中輸入:npm install -g cnpm –registry=https://registry.npm.taobao.org回車,大約需要3分鍾,
如果報錯或沒反應,則卸掉node.js重新安裝 ③安裝全局vue-cli腳手架,用於幫助搭建所需的模板框架。輸入命令:cnpm install -g vue-cli回車等待完成。 ④創建項目,首先我們要選定目錄,然后再命令行中把目錄轉到選定的目錄,假如我們打算把項目新建在e盤下的vue文件夾中則輸入下面的命令:
e:回車,然后cd vue,然后輸入命令:vue init webpack 項目文件夾名稱,回車,運行初始化命令的時候會讓用戶輸入幾個基本的選項,
如項目名稱,描述,作者等信息,如果不想填直接回車默認就好。(一頓回車+N鍵就完成了) ⑤安裝項目所需的依賴包,首先輸入:cd 項目名回車,然后輸入:cnpm install回車等待安裝,安裝完成之后,會在我們的項目目錄firstVue
文件夾中多出一個node_modules文件夾,這里邊就是我們項目需要的依賴包資源。安裝完依賴包之后,就可以運行整個項目了。 ⑥測試環境是否搭建成功,在cmd里輸入:cnpm run dev回車,項目運行成功后,瀏覽器會自動打開localhost:8080(如果瀏覽器沒有自動打開
,可以手動輸入)。運行成功后,會看到Welcome to Your Vue.js App頁面。
第二步:創建數據庫表:persons<mysql>
CREATE TABLE persons
(id integer,
create_datetime datetime,
email varchar(255),
phone varchar(255),
sex varchar(255),
username varchar(255),
zone blob,
primary key (id));
INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'gubaoer@hotmail.com', 08613000001111, 'male', 'gubaoer', 'HongKou District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'boyle.gu@hotmail.com', 08613000001112, 'male', 'baoer.gu', 'JingAn District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'yoyo.wu@gmail.com', 08613000001113, 'female', 'yoyo.wu', 'JingAn District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'stacy.gao@126.com', 08613000001114, 'female', 'stacy.gao', 'MinHang District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'yomiko.zhu@qq.com', 08613000001115, 'female', 'yomiko.zhu', 'PuDong District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'hong.zhu@163.com', 08613000001116, 'male', 'hong.zhu', 'YangPu District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'leon.lai@qq.com', 08613000001117, 'male', 'leon.lai', 'JinShan District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'mark.lei@sohu.com', 08613000001118, 'male', 'mark.lei', 'HuangPu District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'wen.liu@360.com', 08613000001119, 'male', 'wen.liu', 'ChongMing District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'cai.lu@baidu.com', 08613000001120, 'female', 'cai.lu', 'BaoShan District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'alex.li@icee.com', 08613000001121, 'male', 'alex.li', 'ChangNing District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'sofia.xu@qq.com', 08613000001122, 'female', 'sofia.xu', 'ZhaBei District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'cora.zhang@qq.com', 08613000001123, 'female', 'cora.zhang', 'XuHui District'); INSERT INTO persons (create_datetime, email, phone, sex, username, zone) VALUES (now(), 'tom.gao@hotmail.com', 08613000001124, 'female', 'tom.gao', 'HuangPu District');
第三步:編寫前端服務器代碼:Vue框架
1、什么是vue框架
Vue (讀音 /vjuː/,類似於 view) 是一套用於構建用戶界面的漸進式框架與其它大型框架不同的是,Vue 被設計為可以自底向上逐層應用。Vue 的核心庫只關注視圖層,不僅易於上手,還便於與第三方庫或既有項目整合。另一方面,當與現代化的工具鏈以及各種支持類庫結合使用時,Vue 也完全能夠為復雜的單頁應用提供驅動
2、vue框架的特點
- 組件化的開發方式類似於java中的類調用
- 適用於前端部分的MVVM開發模式
- 可使用webpack打包發布
3、vue框架核心組成文件介紹
- App.vue:頁面入口文件
- main.js:程序入口文件,加載各種公共組件
- eventBus:作為各個組件的通道
- components包:存放公共組件的文件夾
4、整個前端項目文件介紹

5、前端項目結構圖

components包下的內容

6、App.vue內容
<template>
<div class="wrapper">
<db-header></db-header>
<el-row class="container">
<el-col :span="4" class="menu">
<db-sidebar></db-sidebar>
</el-col>
<el-col :span="20" class="content">
<db-filterinput></db-filterinput>
<db-table></db-table>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<footer class="footer">
<db-footer></db-footer>
</footer>
</el-col>
</el-row>
</div>
</template>
<script>
/*此處命名必須按照駝峰命名法,並且引入也需要遵守規則*/
import DbHeader from './components/DbHeader.vue'
import DbSidebar from './components/DbSidebar.vue'
import DbFilterinput from './components/DbFilterinput.vue'
import DbTable from './components/DbTable.vue'
import DbFooter from './components/DbFooter.vue'
import ElRow from "element-ui/packages/row/src/row";
export default {
name: 'app',
components: {
ElRow, DbHeader,
DbSidebar,
DbFilterinput,
DbTable,
DbFooter
},
}
</script>
<style>
element.style {
background-color: rgb(10, 47, 88);
}
body {
font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微軟雅黑", Arial, sans-serif;
margin: 0;
display: flex;
min-height: 100vh;
flex-direction: column;
}
.el-menu, body, html {
height: 100%;
}
.wrapper {
position: relative;
}
footer, div {
display: block;
}
.container {
padding-top: 70px;
flex: 1;
}
.container, .wrapper {
height: 100%;
}
.menu {
height: 100%;
background-color: #eef1f6;
}
.content {
padding-top: 25px;
padding-right: 25px;
padding-bottom: 125px;
padding-left: 25px;
}
.footer {
height: 120px;
background-color: #324057;
color: #a4aebd;
width: 100%;
z-index: 1000;
margin-top: -120px;
line-height: 1;
font-size: 22px;
}
</style>
vue文件中3大標簽的作用:
7、main.js中的內容
/**
* Created by wlovet on 18/1/17
*/
import Vue from 'vue'
import{
Button,
Select,
Row,
Col,
Pagination,
Table,
TableColumn,
Form,
FormItem,
Input,
Dialog,
Option
}from 'element-ui'
//導入App.vue頁面入口組件
import App from './App.vue'
//Element-UI使用指南 Element-UI是餓了么前端團隊
//推出的一款基於Vue.js 2.0 的桌面端UI框架,
//手機端有對應框架是 Mint UI
import 'element-ui/lib/theme-default/index.css'
import lang from 'element-ui/lib/locale/lang/en'
import locale from 'element-ui/lib/locale'
// 導入更多的第三方庫的組件
//Moment.js 是一個 JavaScript 日期處理類庫
//,用於解析、檢驗、操作、以及顯示日期
import moment from 'moment'
//Vue2.0的網絡請求庫
import axios from 'axios'
//curvejs 中文讀["克js"],是騰訊AlloyTeam
//打造的一款魔幻線條框架,
//讓線條成為一名優秀的舞者,
//讓線條們成為優秀的舞團,HTML5 Canvas就是舞台。
import curvejs from 'curvejs'
//Object.defineProperty 是vue中雙向綁定的基礎。
//vue是通過數據劫持的方式來做數據綁定的,
//最核心的方法是通過 Object.defineProperty()
//方法來實現對屬性的劫持,達到能監聽到數據的變動。
Object.defineProperty(Vue.prototype, '$moment', { value: moment });
Object.defineProperty(Vue.prototype, '$axios', { value: axios });
Object.defineProperty(Vue.prototype, '$curvejs', { value: curvejs });
//引入Element-UI組件的步驟
Vue.use(Button);
Vue.use(Select);
Vue.use(Row);
Vue.use(Col);
Vue.use(Pagination);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Input);
Vue.use(Dialog);
Vue.use(Option);
new Vue({
el:'#app',
render:h=>h(App)
})
8、eventBus.js的內容
//作為vue組件之間信息交流的通道 import Vue from 'vue' const Bus = new Vue(); export default Bus
各個組件的內容具體信息可去github下載:https://github.com/boylegu/SpringBoot-vue
第四步:編寫后台服務器代碼:SpringBoot
1、項目結構圖

2、pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sunyard</groupId> <artifactId>SpringBoot-MVVM-Vue</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3、application.properties配置文件
spring.application.name=springboot-mvvm-vue server.port=8088 spring.datasource.url=jdbc:mysql://localhost:3306/springboot-mvvm-vue2 spring.datasource.username=root spring.datasource.password=123 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#掃描項目中的pojo類 mybatis.typeAliasesPackage=com.mycom.pojo
#掃描mapper包下的xml文件 mybatis.mapperLocations=classpath\:com/mycom/mapper/*.xml
4、啟動入口類Application
package com.mycom;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
//掃描dao包下的dao接口
@MapperScan("com.mycom.dao")
//掃描config包下的CORSConfig類,用來解決axios的跨域請求問題
@Configuration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5、此處為基本springMVC編程,業務編寫請自行完成
這里認為你可以獲得persons表中所有數據
6、config包新建CORSConfig類解決axios的跨域請求問題
package com.mycom.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import static org.springframework.web.cors.CorsConfiguration.ALL;
/**
* CORS configuration
*/
@Configuration
public class CORSConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(ALL)
.allowedMethods(ALL)
.allowedHeaders(ALL)
.allowCredentials(true);
}
};
}
}
不建這個類的話在前端部分使用axios(一種ajax的封裝)請求數據是會失敗!
介紹一下webpack是什么?
•能夠實現多種不同的前端模塊系統
•webpack 打包前端資源(模塊)時能夠實現代碼分割,按需加載


