Element UI框架学习篇(七)
1 新增员工
1.1 前台部分
1.1.1 在vue实例的data里面准备好需要的对象以及属性
addStatus : false ,
depts : [ ] ,
mgrs : [ ] ,
jobs : [ ] ,
newEmp : {
ename : null ,
hiredate : null ,
job : null ,
sal : null ,
comm : null ,
mgr : null ,
deptno : null ,
} ,
1.1.2 在el-table上方写下如下代码
< el- button type= "primary" > 新增员工< / el- button>
1.1.3 新增用户的弹窗界面
< el-dialog title = " 新增员工信息界面" :visible.sync = " addStatus" center >
< center>
< el-form :model = " emp" inline label-width = " 80px" >
< el-form-item label = " 姓名" >
< el-input v-model = " newEmp.ename" > </ el-input>
</ el-form-item>
< el-form-item label = " 入职日期" >
< el-date-picker v-model = " newEmp.hiredate" placeholder = " 请选择日期" value-format = " yyyy-MM-dd" > </ el-date-picker>
</ el-form-item>
< el-form-item label = " 职位" >
< el-select v-model = " newEmp.job" >
< el-option v-for = " j in jobs" :value = " j.job" :label = " j.job" > </ el-option>
</ el-select>
</ el-form-item>
< el-form-item label = " 上级领导" >
< el-select v-model = " newEmp.mgr" placeholder = " " >
< el-option v-for = " m in mgrs" :value = " m.empno" :label = " m.ename" > </ el-option>
</ el-select>
</ el-form-item>
< el-form-item label = " 工资" >
< el-input v-model = " newEmp.sal" > </ el-input>
</ el-form-item>
< el-form-item label = " 奖金" >
< el-input v-model = " newEmp.comm" > </ el-input>
</ el-form-item>
< el-form-item label = " 部门" >
< el-select v-model = " newEmp.deptno" >
< el-option v-for = " d in depts" :value = " d.deptno" :label = " d.dname" > </ el-option>
</ el-select>
</ el-form-item>
< br>
< el-form-item>
< el-button type = " success" @click = " addEmp()" > 新增</ el-button>
< el-button type = " info" native-type = " reset" > 重置</ el-button>
</ el-form-item>
</ el-form>
</ center>
</ el-dialog>
1.1.4 进入添加界面的方法addView
addView ( ) {
axios. post ( "http://127.0.0.1:8080/dept/find" ) .
then ( jg => {
this . depts= jg. data;
console. log ( this . depts) ;
} )
axios. post ( "http://127.0.0.1:8080/emp/findAllMgrs" ) .
then ( jg => {
console. log ( "上级领导信息" ) ;
console. log ( jg. data) ;
this . mgrs= jg. data;
} )
axios. post ( "http://localhost:8080/emp/findAllJobs" ) .
then ( jg => {
this . jobs= jg. data;
} )
this . addStatus= true ;
} ,
1.1.5 新增方法addEmp
addEmp ( ) {
axios. post ( "http://127.0.0.1:8080/emp/add" , this . newEmp) .
then ( jg => {
if ( jg. data== true ) {
this . find ( ) ;
this . addStatus= false ;
this . $message ( {
type : 'success' ,
message : '新增员工成功!'
} ) ;
} else {
this . $message ( {
type : "info" ,
mesaage : "新增员工失败"
} )
}
} )
} ,
1.1.4 把新增员工按钮绑定addView方法
< el-button type = " primary" @click = " addView()" > 新增员工</ el-button>
1.1.5 把添加员工弹窗中的新增按钮绑定addEmp方法
< el-button type = " success" @click = " addEmp()" > 新增</ el-button>
1.1.6 完整的前台代码
a 网页文件addEmp.html
<! 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-button type = " primary" @click = " addView()" > 新增员工</ el-button>
< 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>
< el-button type = " danger" @click = " del(s.row)" > 删除</ 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>
< el-dialog title = " 新增员工信息界面" :visible.sync = " addStatus" center >
< center>
< el-form :model = " emp" inline label-width = " 80px" >
< el-form-item label = " 姓名" >
< el-input v-model = " newEmp.ename" > </ el-input>
</ el-form-item>
< el-form-item label = " 入职日期" >
< el-date-picker v-model = " newEmp.hiredate" placeholder = " 请选择日期" value-format = " yyyy-MM-dd" > </ el-date-picker>
</ el-form-item>
< el-form-item label = " 职位" >
< el-select v-model = " newEmp.job" >
< el-option v-for = " j in jobs" :value = " j.job" :label = " j.job" > </ el-option>
</ el-select>
</ el-form-item>
< el-form-item label = " 上级领导" >
< el-select v-model = " newEmp.mgr" placeholder = " " >
< el-option v-for = " m in mgrs" :value = " m.empno" :label = " m.ename" > </ el-option>
</ el-select>
</ el-form-item>
< el-form-item label = " 工资" >
< el-input v-model = " newEmp.sal" > </ el-input>
</ el-form-item>
< el-form-item label = " 奖金" >
< el-input v-model = " newEmp.comm" > </ el-input>
</ el-form-item>
< el-form-item label = " 部门" >
< el-select v-model = " newEmp.deptno" >
< el-option v-for = " d in depts" :value = " d.deptno" :label = " d.dname" > </ el-option>
</ el-select>
</ el-form-item>
< br>
< el-form-item>
< el-button type = " success" @click = " addEmp()" > 新增</ el-button>
< el-button type = " info" native-type = " reset" > 重置</ el-button>
</ el-form-item>
</ el-form>
</ center>
</ el-dialog>
</ div>
< script src = " ../js/edit01.js" > </ script>
</ body>
</ html>
b js文件
var v= 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 ,
} ,
addStatus : false ,
depts : [ ] ,
mgrs : [ ] ,
jobs : [ ] ,
newEmp : {
ename : null ,
hiredate : null ,
job : null ,
sal : null ,
comm : null ,
mgr : null ,
deptno : 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 : '已取消修改'
} ) ;
} ) ;
} ,
del ( t ) {
console. log ( t) ;
this . $confirm ( '确定删除员工编号为:' + t. empno+ ',姓名为' + t. ename+ '的员工吗?' , '提示' , {
confirmButtonText : '确定' ,
cancelButtonText : '取消' ,
type : 'warning'
} ) . then ( ( ) => {
axios. post ( "http://127.0.0.1:8080/emp/delete/" + t. empno) .
then ( jg => {
if ( jg. data== true ) {
this . find ( ) ;
this . $message ( {
type : 'success' ,
message : '删除成功'
} ) ;
} else {
this . $message ( {
type : 'danger' ,
message : '删除失败'
} ) ;
}
} )
} ) . catch ( ( ) => {
this . showEmp ( this . emp. empno) ;
this . $message ( {
type : 'info' ,
message : '已取消修改'
} ) ;
} ) ;
} ,
addView ( ) {
axios. post ( "http://127.0.0.1:8080/dept/find" ) .
then ( jg => {
this . depts= jg. data;
console. log ( this . depts) ;
} )
axios. post ( "http://127.0.0.1:8080/emp/findAllMgrs" ) .
then ( jg => {
console. log ( "上级领导信息" ) ;
console. log ( jg. data) ;
this . mgrs= jg. data;
} )
axios. post ( "http://localhost:8080/emp/findAllJobs" ) .
then ( jg => {
this . jobs= jg. data;
} )
this . addStatus= true ;
} ,
addEmp ( ) {
axios. post ( "http://127.0.0.1:8080/emp/add" , this . newEmp) .
then ( jg => {
if ( jg. data== true ) {
this . find ( ) ;
this . addStatus= false ;
this . $message ( {
type : 'success' ,
message : '新增员工成功!'
} ) ;
} else {
this . $message ( {
type : "info" ,
mesaage : "新增员工失败"
} )
}
} )
} ,
}
} )
1.2 后台核心代码
1.2.1 配置自定义查询方法findAllMgr
a 在EmpMapper.xml文件写入如下代码
< select id = " findAllMgr" resultType = " emp" >
SELECT distinct e2.ename,e2.empno
FROM Emp e1
INNER JOIN Emp e2
ON e1.mgr=e2.empno
</ select>
b 在EmpMapper接口中写入如下代码
List < Emp > findAllMgr ( ) ;
c 在IEmpService接口中写入如下代码
List < Emp > findAllMgr ( ) ;
d 在EmpServiceImpl类中写入如下代码
@Autowired
EmpMapper empMapper;
@Override
public List < Emp > findAllMgr ( ) {
return empMapper. findAllMgr ( ) ;
}
1.2.2 新增页面中数据的展示的核心代码
a 查询所有工作(职位)
@RequestMapping ( "findAllJobs" )
public List < Emp > findAllJobs ( ) {
QueryWrapper qw = new QueryWrapper < > ( ) ;
qw. select ( "distinct job" ) . ne ( "job" , "president" ) ;
return empService. list ( qw) ;
}
b 查询所有领导信息
@RequestMapping ( "findAllMgrs" )
public List < Emp > findAllMgrs ( ) {
return empService. findAllMgr ( ) ;
}
c 查询所有部门
@Autowired
IDeptService deptService;
@RequestMapping ( "find" )
public List < Dept > find ( ) {
return deptService. list ( ) ;
}
1.2.3 新增方法
@RequestMapping ( "add" )
public boolean add ( @RequestBody Emp emp) {
empService. save ( emp) ;
return true ;
}
1.2.4 完整的后台代码
a EmpController类
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) {
return empService. removeById ( empno) ;
}
@RequestMapping ( "add" )
public boolean add ( @RequestBody Emp emp) {
empService. save ( emp) ;
return true ;
}
@RequestMapping ( "findAllMgrs" )
public List < Emp > findAllMgrs ( ) {
return empService. findAllMgr ( ) ;
}
@RequestMapping ( "findAllJobs" )
public List < Emp > findAllJobs ( ) {
QueryWrapper qw = new QueryWrapper < > ( ) ;
qw. select ( "distinct job" ) . ne ( "job" , "president" ) ;
return empService. list ( qw) ;
}
}
b DeptController类
package com. zlz. controller ;
import com. zlz. entity. Dept ;
import com. zlz. service. IDeptService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. CrossOrigin ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RestController ;
import java. util. List ;
@RestController
@RequestMapping ( "/dept" )
@CrossOrigin
public class DeptController {
@Autowired
IDeptService deptService;
@RequestMapping ( "find" )
public List < Dept > find ( ) {
return deptService. list ( ) ;
}
}
1.4 测试
1.4.1 进入初始页面,点击新增员工按钮(点击完后有重置和新增两个按钮)
1.4.2 重置按钮测试
a 点击重置按钮前
b 点击重置按钮后
1.4.3 新增按钮测试
a 界面部分
a.1 新增前的界面
a.2 新增后的界面
a.3 点击到最后一页,发现刚刚增加那条数据能被正常显示
b 数据库部分
b.1 新增前
b.2 新增后