Element UI框架学习篇(五)
1 准备工作
1.1 在zlz包下创建数据传输对象类EmpDTO
package com.zlz.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class EmpDTO {
private String name;
private String zhiwei;
}
1.2 在vue实例中的data里面创建empDTO对象(前台传输数据给后台要用)
empDTO:{
name:null,
zhiwei:null,
}
1.3 在页面中加入搜索框的代码如下(需要绑定empDTO对象才行)
<el-form inline :model="empDTO">
<el-form-item label="按照姓名搜索">
<el-input v-model="empDTO.name" placeholder="请输入姓名"></el-input>
</el-form-item>
<el-form-item label="按照职位搜索">
<el-input v-model="empDTO.zhiwei" placeholder="请输入职位"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary">搜索</el-button>
</el-form-item>
<el-form-item >
<el-button type="primary">全部搜索</el-button>
</el-form-item>
</el-form>
2 按照条件查询(多重)
2.1 前台核心函数
2.1.1 find方法
find(){
axios.post("http://127.0.0.1:8080/emp/find/"+this.current,this.empDTO)
.then(jg=>{
console.log(jg.data);
this.current=jg.data.current;
this.size=jg.data.size;
this.total=jg.data.total;
this.emps=jg.data.records;
})
},
2.1.2 把搜索按钮绑定点击事件,执行find()方法
<el-button type="primary" @click="find()">搜索</el-button>
2.2 后台核心代码
QueryWrapper<Emp> qw = new QueryWrapper<>();
qw.like(empDTO.getName()!=null,"ename",empDTO.getName());
qw.like(empDTO.getZhiwei()!=null,"job",empDTO.getZhiwei());
return empService.page(iPage,qw);
3 全部搜索
3.1 前台核心代码
3.1.1 查询全部方法findAll()
findAll(){
this.empDTO.name=null,
this.empDTO.zhiwei=null,
this.current=1;
this.find();
},
3.1.2 设置全部搜索按钮点击时执行findAll方法
<el-button type="primary" @click="findAll()">全部搜索</el-button>
3.2 后台核心代码
QueryWrapper<Emp> qw = new QueryWrapper<>();
qw.like(empDTO.getName()!=null,"ename",empDTO.getName());
qw.like(empDTO.getZhiwei()!=null,"job",empDTO.getZhiwei());
3.3 完整的代码
3.3.1 前端部分
a 网页文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../elementUI/elementUI.min.css">
<script src="../js/vue.js"></script>
<script src="../js/axios.min.js"></script>
<script src="../elementUI/elementUI.min.js"></script>
<title></title>
</head>
<body>
<div id="app">
<el-form inline :model="empDTO">
<el-form-item label="按照姓名搜索">
<el-input v-model="empDTO.name" placeholder="请输入姓名" ></el-input>
</el-form-item>
<el-form-item label="按照职位搜索">
<el-input v-model="empDTO.zhiwei" placeholder="请输入职位"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="find()">搜索</el-button>
</el-form-item>
<el-form-item >
<el-button type="primary" @click="findAll()">全部搜索</el-button>
</el-form-item>
</el-form>
<el-table :data="emps" :cell-style="{'text-align':'center'}"
:header-cell-style="{'text-align':'center'}" border >
<el-table-column label="编号" prop="empno"></el-table-column>
<el-table-column label="姓名" prop="ename"></el-table-column>
<el-table-column label="工作" prop="job"></el-table-column>
<el-table-column label="工资" prop="sal"></el-table-column>
<el-table-column label="奖金" prop="comm"></el-table-column>
<el-table-column label="入职日期" prop="hiredate"></el-table-column>
<el-table-column label="操作">
<template slot-scope="s">
<el-button type="success" @click="showEmp(s.row.empno)">编辑</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination background :total="total" :current-page.sync="current"
:page-size.sync="size" @current-change="find()" layout="total,prev, pager, next, jumper">
</el-pagination>
<el-dialog title="编辑员工信息界面" :visible.sync="editStatus" center>
<center>
<el-form :model="emp" inline label-width="80px">
<el-form-item label="编号">
<el-input readonly v-model="emp.empno"></el-input>
</el-form-item>
<el-form-item label="姓名">
<el-input v-model="emp.ename"></el-input>
</el-form-item>
<el-form-item label="入职日期">
<el-input v-model="emp.hiredate"></el-input>
</el-form-item>
<el-form-item label="职位">
<el-input v-model="emp.job"></el-input>
</el-form-item>
<el-form-item label="工资">
<el-input v-model="emp.sal"></el-input>
</el-form-item>
<el-form-item label="奖金">
<el-input v-model="emp.comm"></el-input>
</el-form-item>
<el-form-item>
<el-button type="success" @click="editEmp()">修改</el-button>
<el-button type="info" @click="showEmp(emp.empno)">重置</el-button>
</el-form-item>
</el-form>
</center>
</el-dialog>
</div>
<script src="../js/edit.js"></script>
</body>
</html>
b js脚本文件
new Vue({
el:"#app",
data:{
emps:[],
current:0,
size:0,
total:0,
editStatus:false,
emp:{
empno:null,
ename:null,
hiredate:null,
job:null,
sal:null,
comm:null,
},
empDTO:{
name:null,
zhiwei:null,
}
},
mounted(){
this.find();
},
methods:{
find(){
axios.post("http://127.0.0.1:8080/emp/find/"+this.current,this.empDTO)
.then(jg=>{
console.log(jg.data);
this.current=jg.data.current;
this.size=jg.data.size;
this.total=jg.data.total;
this.emps=jg.data.records;
})
},
findAll(){
this.empDTO.name=null,
this.empDTO.zhiwei=null,
this.current=1;
this.find();
},
showEmp(empid){
axios.post("http://localhost:8080/emp/findid",{id:empid})
.then(jg=>{
this.emp=jg.data;
this.editStatus=true;
})
},
editEmp(){
this.$confirm('此操作将修改员工信息', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
axios.post("http://127.0.0.1:8080/emp/update",this.emp).
then(jg=>{
if(jg.data==true){
this.editStatus=false;
this.$message({
type: 'success',
message: '修改成功!'
});
this.find();
}else{
this.$message({
type: 'info',
message: '修改失败!'
});
}
})
}).catch(() => {
this.showEmp(this.emp.empno);
this.$message({
type: 'info',
message: '已取消修改'
});
});
}
}
})
3.3.2 后台部分
package com.zlz.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlz.dto.EmpDTO;
import com.zlz.entity.Emp;
import com.zlz.service.IEmpService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/emp")
@CrossOrigin
public class EmpController {
@Resource
IEmpService empService;
@RequestMapping({"find","find/{page}"})
public IPage<Emp> find(@PathVariable(required = false) Integer page, @RequestBody EmpDTO empDTO){
System.out.println(page+"当前页码");
System.out.println(empDTO);
if(page==null) {
page = 1;
}
IPage<Emp> iPage=new Page<>(page,3);
QueryWrapper<Emp> qw = new QueryWrapper<>();
qw.like(empDTO.getName()!=null,"ename",empDTO.getName());
qw.like(empDTO.getZhiwei()!=null,"job",empDTO.getZhiwei());
return empService.page(iPage,qw);
}
@RequestMapping("findid")
public Emp findid(@RequestBody Map<String,Object> map){
return empService.getById(map.get("id").toString());
}
@RequestMapping("update")
public boolean update(@RequestBody Emp emp){
return empService.updateById(emp);
}
@RequestMapping("delete/{empno}")
public boolean delete(@PathVariable Integer empno){
System.out.println(empno);
return empService.removeById(empno);
}
}
4 测试
4.1 条件搜素
4.1.1 条件搜素前
4.1.2 条件搜素后
4.2 全部搜素
4.2.1 全部搜索前(在第3页的位置)
4.2.2 全部搜索后