目录
applicationContext-mybatis.xml
一、前端代码
1、执行命令
在项目路径下执行
npm install element-ui -S
npm install axios -S
npm install qs -S
npm install vue-axios -S
2、main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import qs from 'qs'
import axios from '@/api/http' //vue项目对axios的全局配置
import VueAxios from 'vue-axios'
Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(VueAxios, axios);
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
3.连接后台
action.js
/**
* 对后台请求的地址的封装,URL格式如下:
* 模块名_实体名_操作
*/
export default {
'SERVER': 'http://localhost:8080/', //服务器
'SYSTEM_ARTICLE_list': '/bkk/list', //查询
'SYSTEM_ARTICLE_add': '/bkk/add', //增加
'SYSTEM_ARTICLE_del': '/bkk/del', //删除
'SYSTEM_ARTICLE_edit': '/bkk/edit', //修改
'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
return this.SERVER + this[k];
}
}
http.js
/**
* vue项目对axios的全局配置
*/
import axios from 'axios'
import qs from 'qs'
//引入action模块,并添加至axios的类属性urls上
import action from '@/api/action'
axios.urls = action
// axios默认配置
axios.defaults.timeout = 10000; // 超时时间
// axios.defaults.baseURL = 'http://localhost:8080/j2ee15'; // 默认地址
axios.defaults.baseURL = action.SERVER;
//整理数据
// 只适用于 POST,PUT,PATCH,transformRequest` 允许在向服务器发送前,修改请求数据
axios.defaults.transformRequest = function(data) {
data = qs.stringify(data);
return data;
};
export default axios;
4、index.vue
<template>
<div>
<!-- 搜索筛选-->
<el-form :inline="true" :model="formInline" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="formInline.bname" placeholder="输入文章标题"></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
<el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
</el-form-item>
</el-form>
<!--列表-->
<el-table size="small" :data="listData" highlight-current-row style="width: 100%;">
<el-table-column align="center" type="selection" min-width="60">
</el-table-column>
<el-table-column sortable prop="bid" label="文章ID" min-width="300">
</el-table-column>
<el-table-column sortable prop="bname" label="文章标题" min-width="300">
</el-table-column>
<el-table-column align="center" label="操作" min-width="300">
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页条 -->
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100"
layout="total, sizes, prev, pager, next, jumper" :total="formInline.total">
</el-pagination>
<!-- 编辑界面 -->
<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog">
<el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
<el-form-item label="文章内容" prop="bname">
<el-input size="small" v-model="editForm.bname" auto-complete="off" placeholder="请输入文章内容"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button size="small" @click="closeDialog">取消</el-button>
<el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'index',
data() {
return {
title: '',
editFormVisible: false,
editForm: {
title: '',
bname: '',
bid: ''
},
rules: {
bid: [{
required: true,
message: '请输入编号',
trigger: 'blur'
},
{
min: 5,
max: 10,
message: '长度须在 5 到 10 个字符之间',
trigger: 'blur'
}
],
bname: [{
required: true,
message: '请输入文章内容',
trigger: 'blur'
}],
},
listData: [],
formInline: {
bname: '',
page: 1,
total: 10
}
}
},
methods: {
handleSizeChange(rows) {
// console.log("页面发生改变")
this.formInline.page = 1;
this.formInline.rows = rows;
this.search();
},
handleCurrentChange(page) {
// console.log("当前页码发生改变")
this.formInline.page = page;
this.search();
},
//是为了代码复用
doSearch(param) {
// 获取树形节点的数据
let url = this.axios.urls.SYSTEM_ARTICLE_list;
//this指的是vue实例
this.axios.post(url, param)
.then(resp => { //代表成功 箭头函数 jdk8的语法
console.log(resp.data);
this.listData = resp.data.result;
this.formInline.total = resp.data.pageBean.total;
}).catch(function() { //代表失败
});
},
search() {
// 按照条件进行查询
this.doSearch(this.formInline);
},
closeDialog() {
// 关闭窗体
this.clearData();
},
clearData() {
// 清楚编辑窗体的缓存数据
this.editForm.bname = '';
this.editForm.bid = '';
this.editForm.bname = '';
this.title = '';
this.editFormVisible = false;
},
handleEdit(index, row) {
this.clearData();
// 展示新增文章的表单
this.editFormVisible = true;
if (row) {
// 编辑
this.title = '编辑窗体';
this.editForm.bid = row.bid;
this.editForm.bname = row.bname;
} else {
//新增
this.title = '新增窗体';
}
},
deleteUser(index, row) {
this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
let url = this.axios.urls.SYSTEM_ARTICLE_del;
this.axios.post(url, {
bid: row.bid
}).then(r => {
// 新增成功之后,1、关闭窗体 ,清空数据 2、重新查询
this.$message({
message: '操作成功',
type: 'success'
});
this.closeDialog();
this.search();
}).catch(e => {
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
let url;
if (this.editForm.bid == '') {
//新增
url = this.axios.urls.SYSTEM_ARTICLE_add;
} else {
url = this.axios.urls.SYSTEM_ARTICLE_edit;
}
this.axios.post(url, this.editForm).then(r => {
// 新增成功之后,1、关闭窗体 ,清空数据 2、重新查询
this.$message({
message: '操作成功',
type: 'success'
});
this.closeDialog();
this.search();
}).catch(e => {
})
} else {
console.log('error submit!!');
return false;
}
});
}
},
created() {
this.doSearch({});
}
}
</script>
<style >
</style>
二、后端代码
1、pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>20220915</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>untitled</artifactId>
<packaging>war</packaging>
<name>untitled Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<!--添加jar包依赖-->
<!--1.spring 5.0.2.RELEASE相关-->
<spring.version>5.0.2.RELEASE</spring.version>
<!--2.mybatis相关-->
<mybatis.version>3.4.5</mybatis.version>
<!--mysql-->
<mysql.version>5.1.44</mysql.version>
<!--pagehelper分页jar依赖-->
<pagehelper.version>5.1.2</pagehelper.version>
<!--mybatis与spring集成jar依赖-->
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<!--3.dbcp2连接池相关 druid-->
<commons.dbcp2.version>2.1.1</commons.dbcp2.version>
<commons.pool2.version>2.4.3</commons.pool2.version>
<!--4.log日志相关-->
<log4j2.version>2.9.1</log4j2.version>
<!--5.其他-->
<junit.version>4.12</junit.version>
<servlet.version>4.0.0</servlet.version>
<lombok.version>1.18.2</lombok.version>
</properties>
<dependencies>
<!--1.spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--2.mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mysql-