員工信息的增刪改查
關注點:
- 員工與部門的關聯關係
- 默認登錄密碼
dao與映射文件
EmployeeDao
package com.imooc.oa.dao;
import com.imooc.oa.entity.Department;
import com.imooc.oa.entity.Employee;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository(value = "employeeDao")
public interface EmployeeDao {
void insert(Employee employee);
void update(Employee employee);
void delete(String sn);
Employee select(String sn);
List<Employee> selectAll();
List<Employee> selectByDepartmentAndPost(@Param("dsn") String dsn,@Param("post") String post);
}
EmployeeDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.oa.dao.EmployeeDao">
<resultMap id="employee" type="Employee">
<id property="sn" column="sn" javaType="String"/>
<result property="password" column="password" javaType="String"/>
<result property="name" column="name" javaType="String"/>
<result property="departmentSn" column="department_sn" javaType="String"/>
<result property="post" column="post" javaType="String"/>
<association property="department" column="department_sn" javaType="Department" >
<id property="sn" column="dsn" javaType="String"/>
<result property="name" column="dname" javaType="String"/>
</association>
</resultMap>
<insert id="insert" parameterType="Employee">
insert into employee values(#{sn},#{password},#{name},#{departmentSn},#{post})
</insert>
<update id="update" parameterType="Employee">
update employee set name=#{name},password=#{password},department_sn=#{departmentSn},post=#{post} where sn=#{sn}
</update>
<delete id="delete" parameterType="String">
delete from employee where sn=#{sn}
</delete>
<select id="select" parameterType="String" resultMap="employee">
select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn
where e.sn=#{sn}
</select>
<select id="selectAll" resultMap="employee">
select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn
</select>
<select id="selectByDepartmentAndPost" resultMap="employee">
select e.*,d.sn dsn,d.name dname from employee e left join department d on d.sn=e.department_sn
where e.sn is not NULL
<if test="dsn!=null">
and e.department_sn=#{dsn}
</if>
<if test="post!=null">
and e.post=#{post}
</if>
</select>
</mapper>
biz接口與其實現類
EmployeeBiz
package com.imooc.oa.biz;
import com.imooc.oa.entity.Department;
import com.imooc.oa.entity.Employee;
import java.util.List;
public interface EmployeeBiz {
void add(Employee employee);
void edit(Employee employee);
void remove(String sn);
Employee get(String sn);
List<Employee> getAll();
}
EmployeeBizImpl
package com.imooc.oa.biz.impl;
import com.imooc.oa.biz.EmployeeBiz;
import com.imooc.oa.dao.EmployeeDao;
import com.imooc.oa.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("employeeBiz")
public class EmployeeBizImpl implements EmployeeBiz {
@Autowired
private EmployeeDao employeeDao;
public void add(Employee employee) {
employee.setPassword("000000");
employeeDao.insert(employee);
}
public void edit(Employee employee) {
employeeDao.update(employee);
}
public void remove(String sn) {
employeeDao.delete(sn);
}
public Employee get(String sn) {
return employeeDao.select(sn);
}
public List<Employee> getAll() {
return employeeDao.selectAll();
}
}
控制器
package com.imooc.oa.controller;
import com.imooc.oa.biz.DepartmentBiz;
import com.imooc.oa.biz.EmployeeBiz;
import com.imooc.oa.entity.Department;
import com.imooc.oa.entity.Employee;
import com.imooc.oa.global.Contant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller("employeeController")
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private DepartmentBiz departmentBiz;
@Autowired
private EmployeeBiz employeeBiz;
@RequestMapping("/list")
public String list(Map<String,Object> map){
map.put("list",employeeBiz.getAll());
return "employee_list";
}
@RequestMapping("/to_add")
public String toAdd(Map<String,Object> map){
map.put("employee",new Employee());
map.put("dlist",departmentBiz.getAll());
map.put("plist", Contant.getPosts());
return "employee_add";
}
@RequestMapping("/add")
public String add(Employee employee){
employeeBiz.add(employee);
return "redirect:list";
}
@RequestMapping(value = "/to_update",params = "sn")
public String toUpdate(String sn,Map<String,Object> map){
map.put("employee",employeeBiz.get(sn));
map.put("dlist",departmentBiz.getAll());
map.put("plist", Contant.getPosts());
return "employee_update";
}
@RequestMapping("/update")
public String update(Employee employee){
employeeBiz.edit(employee);
return "redirect:list";
}
@RequestMapping(value = "/remove",params = "sn")
public String remove(String sn){
employeeBiz.remove(sn);
return "redirect:list";
}
}
頁面
employee_list
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="top.jsp"/>
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 部門列表 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<div class="panel-menu">
<div class="row">
<div class="hidden-xs hidden-sm col-md-3">
<div class="btn-group">
<button type="button" class="btn btn-default light">
<i class="fa fa-refresh"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-trash"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-plus" οnclick="javascript:window.location.href='/department/to_add';"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-md-9 text-right">
<div class="btn-group">
<button type="button" class="btn btn-default light">
<i class="fa fa-chevron-left"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-chevron-right"></i>
</button>
</div>
</div>
</div>
</div>
<div class="panel-body pn">
<table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
<thead>
<tr class="">
<th class="text-center hidden-xs">Select</th>
<th class="hidden-xs">部門編號</th>
<th class="hidden-xs">部門名稱</th>
<th class="hidden-xs">地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="dept">
<tr class="message-unread">
<td class="hidden-xs">
<label class="option block mn">
<input type="checkbox" name="mobileos" value="FR">
<span class="checkbox mn"></span>
</label>
</td>
<td>${dept.sn}</td>
<td>${dept.name}</td>
<td>${dept.address}</td>
<td>
<a href="/department/to_update?sn=${dept.sn}">編輯</a>
<a href="/department/remove?sn=${dept.sn}">刪除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
<jsp:include page="bottom.jsp"/>
employee_add.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="top.jsp"/>
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 添加員工 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<form:form action="/employee/add" modelAttribute="employee" id="admin-form" name="addForm">
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="col-md-6">
<label for="sn" class="field prepend-icon">
<form:input path="sn" cssClass="gui-input" placeholder="工號..." />
<label for="sn" class="field-icon">
<i class="fa fa-user"></i>
</label>
</label>
</div>
<div class="col-md-6">
<label for="name" class="field prepend-icon">
<form:input path="name" class="gui-input" placeholder="姓名..."/>
<label for="name" class="field-icon">
<i class="fa fa-user"></i>
</label>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-6">
<label for="sn" class="field select">
<form:select path="departmentSn" items="${dlist}" itemLabel="name" itemValue="sn" cssClass="gui-input" placeholder="所屬部門..."/>
<i class="arrow double"></i>
</label>
</div>
<div class="col-md-6">
<label for="post" class="field select">
<form:select path="post" items="${plist}" cssClass="gui-input" placeholder="職務..."/>
<i class="arrow double"></i>
</label>
</div>
</div>
<div class="panel-footer text-right">
<button type="submit" class="button"> 保存 </button>
<button type="button" class="button" οnclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</form:form>
</div>
</div>
</div>
</section>
<jsp:include page="bottom.jsp"/>
employee.update.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="top.jsp"/>
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 編輯員工信息 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<form:form action="/employee/update" modelAttribute="employee" id="admin-form" name="addForm">
<form:hidden path="password"/>
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="col-md-6">
<label for="sn" class="field prepend-icon">
<form:input path="sn" cssClass="gui-input" placeholder="工號..." readonly="true" />
<label for="sn" class="field-icon">
<i class="fa fa-user"></i>
</label>
</label>
</div>
<div class="col-md-6">
<label for="name" class="field prepend-icon">
<form:input path="name" class="gui-input" placeholder="姓名..."/>
<label for="name" class="field-icon">
<i class="fa fa-user"></i>
</label>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-6">
<label for="sn" class="field select">
<form:select path="departmentSn" items="${dlist}" itemLabel="name" itemValue="sn" cssClass="gui-input" placeholder="所屬部門..."/>
<i class="arrow double"></i>
</label>
</div>
<div class="col-md-6">
<label for="post" class="field select">
<form:select path="post" items="${plist}" cssClass="gui-input" placeholder="職務..."/>
<i class="arrow double"></i>
</label>
</div>
</div>
<div class="panel-footer text-right">
<button type="submit" class="button"> 保存 </button>
<button type="button" class="button" οnclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</form:form>
</div>
</div>
</div>
</section>
<jsp:include page="bottom.jsp"/>