代碼地址
前端地址:https://github.com/Snowstorm0/SpringAndVue-vue
后端地址:https://github.com/Snowstorm0/SpringAndVue-spring
1 數據庫
創建MySQL數據庫。
表名為user_data,人員信息有number、name。
效果如下:
2 前端
2.1 創建項目
打開cmd,輸入ui命令:
vue ui
若沒有反應,可能是版本太低,需要卸載后重裝:
npm uninstall vue-cli -g #卸載
npm install @vue/cli -g #安裝
執行ui命令成功后,會出現提示:
正在啟動圖形用戶界面... 準備就緒 http://localhost:8000
并會自動打開頁面:
創建名為SpringAndVue-vue
的項目,預設選擇“手動”; 功能開啟 Babel、Router、Vuex、Linter/Formatter; 配置選擇“ESLint with error prevention only”; 版本建議使用 “vue2.0”。 創建新項目。
通過cd
進入目錄,啟動項目:
npm run serve
2.2 安裝插件
安裝 element-ui 插件。
打開cmd,輸入ui命令:
vue ui
在插件項搜索,并點擊安裝。
vue2.0 選擇安裝 “vue-cli-plugin-element”; vue3.0 選擇安裝 “vue-cli-plugin-element-plus”。
Terminal安裝axios,每個新項目都需要安裝:
# vue-cli2.0命令
npm install axios
# vue-cli3.0命令
npm add axios
2.3 添加模塊
2.3.1 主頁
在views文件夾下創建文件 HomePage.vue,內容如下:
<template>
<div>
用戶列表
<el-table
:data="tableData"
border
style="width: 40%">
<el-table-column
prop="number"
label="編號"
width="150">
el-table-column>
<el-table-column
prop="name"
label="姓名"
width="150">
el-table-column>
<el-table-column
fixed="right"
label="操作"
width="160">
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small">修改el-button>
<el-button @click="deleteClick(scope.row)" type="text" size="small">刪除el-button>
template>
el-table-column>
el-table>
<el-pagination
background
layout="prev, pager, next"
:total="total"
page-size="2"
@current-change="page">
el-pagination>
div>
template>
<script>
import axios from 'axios';
export default {
name: "HomePage",
methods: {
handleClick(row) {
this.$router.push({
path: '/useredit',
query:{
number: row.number,
name: row.name
}
})
},
deleteClick(row){
// var that=this;
axios.delete('http://localhost:8081/homepage/delete/'+row.number).then(function (response) {
console.log(response)
})
},
//當被點擊頁數的時候,跳轉
page(currPage){
var that=this;
axios.get('http://localhost:8081/homepage/query/'+(currPage-1)*3+'/3').then(function (response) {
that.tableData=response.data;
})
}
},
//被創建的時候,顯示第一頁
created() {
var that=this;
axios.get('http://localhost:8081/homepage/query/0/3').then(function (response) {
//給數據
that.tableData=response.data;
// console.log(response.data)
}),
axios.get('http://localhost:8081/homepage/all').then(function (response) {
//獲得總長度
that.total=response.data.length ;
})
},
data() {
return {
total: 10,
tableData: [{
number: '編號',
name: '姓名'
}]
}
}
}
script>
<style scoped>
style>
2.3.2 查看用戶
在views文件夾下創建文件:UserView.vue,內容如下:
<template>
<div>
<table>
<tr>
<td>編號td>
<td>姓名td>
tr>
<tr v-for="user in users" :key="user">
<td>{{user.number}}td>
<td>{{user.name}}td>
tr>
table>
div>
template>
<script>
import axios from 'axios';
export default {
name: "UserView",
data(){
return {
users:[
{
number: 1003,
name: '張三',
},
{
number: 1004,
name: '李四',
}
]
}
},
created() {
var that=this;
axios.get('http://localhost:8081/homepage/view').then(function (resp) {
that.users=resp.data;
})
}
}
script>
<style scoped>
style>
2.3.3 添加用戶
在views文件夾下創建文件:UserAdd.vue,內容如下:
<template>
<div>
<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="用戶編號" prop="number">
<el-input v-model="ruleForm.number" >el-input>
el-form-item>
<el-form-item label="用戶名" prop="name">
<el-input v-model="ruleForm.name">el-input>
el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">提交el-button>
<el-button @click="resetForm('ruleForm')">重置el-button>
el-form-item>
el-form>
div>
template>
<script>
import axios from 'axios';
export default {
name: "UserAdd",
data() {
var validateNumber = (rule, value, callback) => {
if (value === '') {
callback(new Error('請輸入用戶編號'));
} else {
if (this.ruleForm.number !== '') {
//如果不為空
}
callback();
}
};
var validateName = (rule, value, callback) => {
if (value === '') {
callback(new Error('請輸入用戶名'));
} else {
if (this.ruleForm.name !== '') {
//如果不為空
}
callback();
}
};
return {
ruleForm: {
number: '',
name: ''
},
rules: {
number: [
{ validator: validateNumber, trigger: 'blur' }
],
name: [
{ validator: validateName, trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
var that=this;
this.$refs[formName].validate((valid) => {
if (valid) {
//提交成功后要做的事情
// alert('submit!');
console.log(that.ruleForm)
axios.post('http://localhost:8081/homepage/add',that.ruleForm).then(function (response) {
console.log(response);
})
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
script>
<style scoped>
style>
2.3.4 修改App
修改App.vue為:
<template>
<div id="app">
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<el-menu :default-openeds="['1']" router>
<el-submenu index="1">
<template slot="title"><i class="el-icon-message">i>用戶管理template>
<el-menu-item-group>
<el-menu-item index="/">首頁el-menu-item>
<el-menu-item index="/userView">全部用戶el-menu-item>
<el-menu-item index="/userAdd">添加用戶el-menu-item>
el-menu-item-group>
el-submenu>
el-menu>
el-aside>
<el-container>
<el-header style="text-align: right; font-size: 12px">
<span>Snowstormspan>
el-header>
<br><br>
<router-view>router-view>
el-container>
el-container>
div>
template>
<style>
.el-header {
background-color: #B3C0D1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
style>
<script>
export default {
data() {
const item = {
};
return {
tableData: Array(20).fill(item)
}
}
};
script>
2.3.5 修改index
在 router/index.js 中 const routes 函數修改為:
const routes = [
{
path: '/',
name: '首頁',
component: HomePage
},
{
path: '/userview',
name: '全部用戶',
component: () =>import(/* webpackChunkName: "user" */'../views/UserView.vue')
},
{
path: '/useradd',
name: '添加用戶',
component: () =>import(/* webpackChunkName: "user" */'../views/UserAdd.vue')
},
{
path: '/useredit',
name: '編輯用戶',
component: () =>import(/* webpackChunkName: "user" */'../views/UserAdd.vue')
}
]
3 后端
3.1 控制器
內容為:
@RestController
@RequestMapping("/homepage")
publicclass MyController {
@Resource
MyService myService;
// 查看全部數據
@GetMapping("/view")
public List userView(){
return myService.userView();
}
// 增
@PostMapping("/add")
public int userAdd(@RequestBody UserDTO user){
myService.userAdd(user);
return0;
}
// 刪
@DeleteMapping("/delete/{number}")
public int deleteBook(@PathVariable("number") Integer number){
return myService.userDelete(number);
}
// 改
@PutMapping("/edit")
public int userEdit(@RequestBody UserDTO user){
return myService.userEdit(user);
}
// 查
@GetMapping("/query/{start}/{length}")
public List userQuery(@PathVariable("start") Integer start,@PathVariable("length") Integer length){
System.out.println("users:" + myService.userQuery(start, length) + "\\n");
return myService.userQuery(start, length);
}
}
3.2 服務
內容為:
@Service
@EnableScheduling
publicclass MyServiceImpl implements MyService {
@Resource
MyMapper myMapper;
// 返回全部用戶類
public List userView(){
System.out.println("users:" + myMapper.userView() + "\\n");
return myMapper.userView();
}
public Integer userAdd(UserDTO user){
System.out.println("users:" + user + "\\n");
return myMapper.userAdd(user);
}
public Integer userDelete(int number){
System.out.println("number:" + number + "\\n");
return myMapper.userDelete(number);
}
public Integer userEdit(UserDTO user){
System.out.println("user:" + user + "\\n");
return myMapper.userEdit(user);
}
public List userQuery(int start, int length){
System.out.println("start:" + start + "high:" + length + "\\n");
return myMapper.userQuery(start, length);
}
}
3.3 刀
內容為:
@Mapper
publicinterface MyMapper {
List userView();
Integer userAdd(UserDTO user);
Integer userDelete(int number);
Integer userEdit(UserDTO user);
List userQuery(int start, int length);
}
3.4 模型
文件名UserDTO,內容為:
@Data
publicclass UserDTO {
private Integer number;
private String name;
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.5 跨域配置
文件名Config,內容為:
@Configuration
publicclass Config implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET","HEAD","POST","DELETE","OPTIONS","PUT")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
4 運行效果
4.1 主頁
4.2 全部用戶
4.3 添加用戶
-
數據庫
+關注
關注
7文章
3794瀏覽量
64362 -
spring
+關注
關注
0文章
340瀏覽量
14338 -
GitHub
+關注
關注
3文章
469瀏覽量
16428 -
vue
+關注
關注
0文章
58瀏覽量
7837
發布評論請先 登錄
相關推薦
評論