一、添加员工
1.封装实体类:
生成get和set方法,有参构造和无参构造。
private String empId;//员工编号
private String passWord;//密码
private int deptNo;//部门ID
private int posId;//岗位ID
private String mgrId;//上级编号
private String realName;//真实姓名
private String sex;//性别
private String birthDate;//出生日期
private String hireDate;//入职日期
private String leaveDate;//离职日期
private int onDuty;//是否在职 0-离职 1-在职
private int empType; //员工类型1.普通员工 2.管理人员 含经理、总监、总裁等 3.管理员
private String phone;//联系方式
private String qq;
private String emerContactPerson;//紧急联系人信息
private String idCard;//身份证号码
private String deptName;//部门
private String pname;//岗位
2.数据访问层:
在Dao层添加“添加员工”的方法,
SQL语句:insert into employee values (?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),to_date(?,'yyyy-mm-dd'),to_date(?,'yyyy-mm-dd'),?,?,?,?,?,?)。
- 代码示例:
/**
* 添加数据
*/
@Override
public int save(Employee emp) {
String sql = "insert into employee values (?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),to_date(?,'yyyy-mm-dd'),to_date(?,'yyyy-mm-dd'),?,?,?,?,?,?)";
Object[] obj = {emp.getEmpId(),emp.getPassWord(),emp.getDeptNo(),emp.getPosId(),emp.getMgrId(),
emp.getRealName(),emp.getSex(),emp.getBirthDate(),emp.getHireDate(),emp.getLeaveDate(),
emp.getOnDuty(),emp.getEmpType(),emp.getPhone(),emp.getQq(),emp.getEmerContactPerson(),
emp.getIdCard()};
return DBUtil.executeUpdate(sql, obj);
}
3.业务层:
增加添加员工的方法:
@Override
public int addEmp(Employee emp) {
// TODO Auto-generated method stub
return ed.save(emp);
}
4.控制层:
将获取的表单数据添加到Employee对象中,调用业务层中的添加方法,实现员工的添加;添加成功页面跳转的员工查询页面,添加失败回到该页面并提示。
/**
* 添加员工
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
public void addEmp(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
//获取页面信息
String empId = request.getParameter("empId");
String password = "123456";
int deptNo = Integer.parseInt(request.getParameter("deptno"));
int posId = Integer.parseInt(request.getParameter("posId"));
String mgrId =request.getParameter("mgrId");
String realName = request.getParameter("realName");
String sex = request.getParameter("sex");
String birthDate = request.getParameter("birthDate");
String hireDate = request.getParameter("hireDate");
String leaveDate = request.getParameter("leaveDate");
int onDuty =Integer.parseInt(request.getParameter("onDuty"));
int empType = Integer.parseInt(request.getParameter("empType"));
String phone = request.getParameter("phone");
String qq = request.getParameter("qq");
String emerContactPerson = request.getParameter("emerContactPerson");
String idCard = request.getParameter("idCard");
//调用业务层完成添加操作
Employee emp = new Employee(empId, password, deptNo, posId, mgrId, realName, sex, birthDate, hireDate, leaveDate, onDuty, empType, phone, qq, emerContactPerson, idCard);
EmployeeService empService = new EmployeeServiceImpl();
int n = empService.addEmp(emp);
if(n>0){
response.sendRedirect("employeeServlet?method=findAll");
}else{
request.setAttribute("error", "添加失败!");
request.getRequestDispatcher("system/empAdd.jsp").forward(request, response);
}
}
/**
* 查询指定类型的员工
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//获取部门信息
DepartmentService deptService = new DepartmentServiceImpl();
List<Department> deptList = deptService.selDept();
request.setAttribute("deptList", deptList);
//获取所有岗位信息
PositionService pos = new PositionServiceImpl();
List<Position> posList = pos.findAll();
request.setAttribute("posList", posList);
//获取上级员工
EmployeeService emp = new EmployeeServiceImpl();
List<Employee> mgrList = emp.findByType(2);
request.setAttribute("mgrList",mgrList);
//跳转到指定页面
request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
}
5.视图层:
添加页面的展示。
实现显示岗位、部门、上级等下拉列表的动态数据 :
方法1:先跳到一个Servlet,查询岗位、部门、上级列表等信息,在跳转到JSP页面进行显示 。
方法2:先跳到JSP页面,借助onload事件和Ajax获取岗位、部门、上级列表等信息并显示 。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/select.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.idTabs.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="editor/kindeditor.js"></script>
<script type="text/javascript" src="My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$(".select1").uedSelect({
width : 345
});
});
</script>
<script type="text/javascript">
KE.show({id:"ecp",width:"800px",height:"300px"});
</script>
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="#">人事管理</a></li>
<li><a href="#">添加员工</a></li>
</ul>
</div>
<form action="employeeServlet?method=addEmp" method="post">
<div class="formbody">
<div class="formtitle"><span>基本信息</span></div>
<ul class="forminfo">
<li>
<label>用户名</label>
<input name="empId" type="text" class="dfinput" /></li>
<li>
<li>
<label>真实姓名</label>
<input name="realName" type="text" class="dfinput" /><i></i></li>
<li>
<label>性别</label><cite>
<input name="sex" type="radio" value="男" checked="checked" />男
<input name="sex" type="radio" value="女" />女<i>也可以根据身份证号自动获取</i></cite>
</li>
<li>
<label>出生日期</label>
<input name="birthDate" type="text" class="dfinput" onfocus="WdatePicker({skin:'whyGreen',lang:'en'})" /><i>也可以根据身份证号自动获取</i></li>
<li>
<li>
<label>入职时间</label>
<input name="hireDate" type="text" class="dfinput" onfocus="WdatePicker()"/><i></i></li>
<li>
<label>离职时间</label>
<input name="leaveDate" type="text" class="dfinput" onfocus="WdatePicker()"/><i></i></li>
<li>
<label>是否在职</label><cite>
<input name="onDuty" type="radio" value="1" checked="checked" />是
<input name="onDuty" type="radio" value="0" />否</cite>
</li>
<li>
<label>员工类型</label><cite>
<input name="empType" type="radio" value="1" checked="checked" />基层员工
<input name="empType" type="radio" value="2" />各级管理人员</cite>
</li>
<li>
<label>所属部门<b>*</b></label>
<div class="vocation">
<select class="select1" name="deptno">
<c:forEach items="${deptList }" var="dept">
<option value="${dept.deptno }">${dept.deptName }</option>
</c:forEach>
</select>
</div>
</li>
<li>
<label>从事岗位<b>*</b></label>
<div class="vocation">
<select class="select1" name="posId">
<option value="1">总裁</option>
<option value="2">教学经理</option>
<option value="3">咨询经理</option>
<option value="4">咨询师</option>
<option value="5">讲师</option>
</select>
</div>
</li>
<li>
<label>直接上级<b>*</b></label>
<div class="vocation">
<select class="select1" name="mgrId">
<option value="">没有上级</option>
<c:forEach items="${mgrList }" var="mgr">
<option value="${mgr.empId }">${mgr.realName }</option>
</c:forEach>
</select>
</div>
<input name="" type="text" class="dfinput" placeholder="也可以在此输入首字母帮助显示"/></li>
</li>
<li>
<label>联系方式</label>
<input name="phone" type="text" class="dfinput" />
</li>
<li>
<label>QQ号</label>
<input name="qq" type="text" class="dfinput" />
</li>
<li>
<label>紧急联系人信息</label>
<textarea name="emerContactPerson" cols="" rows="" id="ecp" class="textinput"></textarea>
</li>
<li>
<label>身份证号</label>
<input name="idCard" type="text" class="dfinput" />
</li>
<li>
<label> </label>
<input name="" type="submit" class="btn" value="确认保存" />
</li>
</ul>
</div>
</form>
<span style="color: red;font-size: 16px">${error }</span>
</body>
</html>
6.使用日历插件和富文本编辑器:
使用日历插件My97DatePicker:
(1)复制My97DatePicker到WebRoot下;
(2)导入外部My97DatePicker:<scripttype="text/javascript"src="My97DatePicker/WdatePicker.js"></script>;
(3)使用My97DatePicker:<input name="leaveDate" type="text" class="dfinput" onfocus="WdatePicker()"/>。
使用富文本编辑器:
(1)复制kindEditor文件夹到项目;
(2)导入外部kindEditor:
<script type="text/javascript" src="editor/kindeditor.js"></script>;
(3)使用My97DatePicker:<script type="text/javascript“>KE.show({id : 'contents'});</script>。
- 实现效果: image.png
二、查询所有员工
1.数据访问层:
实现查询所有员工包括部门信息岗位信息的操作;
SQL语句:select e.*,d.deptname,p.pname from employee e inner join dept d on(e.deptno=d.deptno) inner join position p on(e.posid=p.posid);
/**
* 查询所有员工信息
* @return
*/
@Override
public List<Employee> findAll(){
String sql = "select e.*,d.deptname,p.pname from employee e inner join dept d on(e.deptno=d.deptno) inner join position p on(e.posid=p.posid)";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Employee> list = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
list = new ArrayList<Employee>();
while(rs.next()){
Employee emp = new Employee();
emp.setEmpId(rs.getString("empId"));
emp.setPassWord(rs.getString("passWord"));
emp.setDeptNo(rs.getInt("deptNo"));
emp.setPosId(rs.getInt("posId"));
emp.setMgrId(rs.getString("mgrId"));
emp.setRealName(rs.getString("realName"));
emp.setSex(rs.getString("sex"));
emp.setBirthDate(rs.getString("birthDate"));
emp.setHireDate(rs.getString("hireDate"));
emp.setLeaveDate(rs.getString("leaveDate"));
emp.setOnDuty(rs.getInt("onDuty"));
emp.setEmpType(rs.getInt("empType"));
emp.setPhone(rs.getString("phone"));
emp.setQq(rs.getString("qq"));
emp.setEmerContactPerson(rs.getString("emerContactPerson"));
emp.setIdCard(rs.getString("idCard"));
emp.setDeptName(rs.getString("deptName"));
emp.setPname(rs.getString("pname"));
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeAll(rs, ps, conn);
}
return list;
}
2.业务层:
添加Service方法实现所有员工的查询;
@Override
public List<Employee> findAll() {
return ed.findAll();
}
3.控制层:
调用业务层中实现所有员工的查询,并将数据传送到视图层。
/**
* 查询所有员工
* @throws IOException
* @throws ServletException
*/
public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//调用业务层查询所有员工
EmployeeService empService = new EmployeeServiceImpl();
List<Employee> empList = empService.findAll();
request.setAttribute("empList", empList);
request.getRequestDispatcher("/system/empSel.jsp").forward(request, response);
}
4.视图层:
将获取的数据遍历出来展现到页面。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/select.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.idTabs.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="editor/kindeditor.js"></script>
<script type="text/javascript" src="My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$(".select1").uedSelect({
width : 200
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".click").click(function(){
$(".tip").fadeIn(200);
});
$(".tiptop a").click(function(){
$(".tip").fadeOut(200);
});
$(".sure").click(function(){
$(".tip").fadeOut(100);
});
$(".cancel").click(function(){
$(".tip").fadeOut(100);
});
});
function deleteEmp(empId){
var flag = window.confirm("确定删除吗?");
if(flag){
location.href = "employeeServlet?method=deleteEmp&empId2="+empId;
}
}
</script>
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="#">人事管理</a></li>
<li><a href="#">员工管理</a></li>
</ul>
</div>
<div class="rightinfo">
<form action="employeeServlet?method=findEmp" method="post">
<ul class="prosearch">
<li>
<label>查询:</label><i>用户名</i>
<a>
<input name="empId" type="text" class="scinput" value="${empId }" />
</a><i>所属部门</i>
<a>
<select class="select1" name="deptno">
<option value="0">--全部--</option>
<c:forEach items="${deptList }" var="dept">
<c:if test="${dept.deptno==deptno }">
<option value="${dept.deptno }" selected="selected">${dept.deptName }</option>
</c:if>
<c:if test="${dept.deptno!=deptno }">
<option value="${dept.deptno }" >${dept.deptName }</option>
</c:if>
</c:forEach>
</select>
</a>
</li>
<li>
<label>是否在职:</label>
<c:if test="${onDuty==1 }">
<input name="onDuty" type="radio" value="1" checked="checked" /> 是
<input name="onDuty" type="radio" value="0" /> 否
</c:if>
<c:if test="${onDuty==0 }">
<input name="onDuty" type="radio" value="1" /> 是
<input name="onDuty" type="radio" value="0" checked="checked"/> 否
</c:if>
<c:if test="${empty onDuty }">
<input name="onDuty" type="radio" value="1" checked="checked" /> 是
<input name="onDuty" type="radio" value="0" /> 否
</c:if>
</li>
<li>
<label>入职时间:</label>
<a>
<input name="hireDate" type="text" class="scinput" onfocus="WdatePicker()" value="${hireDate }"/>
</a>
</li>
<a>
<input name="" type="submit" class="sure" value="查询" />
</a>
</ul>
</form>
<div class="formtitle1"><span>员工列表</span></div>
<table class="tablelist">
<thead>
<tr>
<th>
<input name="" type="checkbox" value="" checked="checked" />
</th>
<th>用户名<i class="sort"><img src="images/px.gif" /></i></th>
<th>真实姓名</th>
<th>所属部门</th>
<th>所属岗位</th>
<th>入职时间</th>
<th>联系方式</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${empList }" var="emp">
<tr>
<td>
<input name="" type="checkbox" value="" />
</td>
<td>${emp.empId }</td>
<td>${emp.realName }</td>
<td>${emp.deptName}</td>
<td>${emp.pname }</td>
<td>${emp.hireDate }</td>
<td>${emp.phone }</td>
<td>
<a href="empInfo.html" class="tablelink">查看</a>
<a href="employeeServlet?method=toUpdate&empId=${emp.empId }" class="tablelink">修改</a>
<a href="javascript:deleteEmp(${emp.empId})" class="tablelink "> 删除</a>
<a href="employeeServlet?method=resetpwd&empId=${emp.empId }" class="tablelink"> 重置密码</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<div class="pagin">
<div class="message">共<i class="blue">1256</i>条记录,当前显示第 <i class="blue">2 </i>页</div>
<ul class="paginList">
<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>
<li class="paginItem"><a href="javascript:;">1</a></li>
<li class="paginItem current"><a href="javascript:;">2</a></li>
<li class="paginItem"><a href="javascript:;">3</a></li>
<li class="paginItem"><a href="javascript:;">4</a></li>
<li class="paginItem"><a href="javascript:;">5</a></li>
<li class="paginItem more"><a href="javascript:;">...</a></li>
<li class="paginItem"><a href="javascript:;">10</a></li>
<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>
</ul>
</div>
<div class="tip">
<div class="tiptop"><span>提示信息</span>
<a></a>
</div>
<div class="tipinfo">
<span><img src="images/ticon.png" /></span>
<div class="tipright">
<p>是否确认对信息的修改 ?</p>
<cite>如果是请点击确定按钮 ,否则请点取消。</cite>
</div>
</div>
<div class="tipbtn">
<input name="" type="button" class="sure" value="确定" />
<input name="" type="button" class="cancel" value="取消" />
</div>
</div>
</div>
<script type="text/javascript">
$('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
-
实现效果:
image.png
三、多条件查询员工
1.数据访问层:
实现对员工查询时通过员工的不同和相同的条件查询某一类的员工。
SQL语句:select e.empid,e.realname,e.hiredate, e.phone,d.deptname,p.pname from employee e join dept d on e.deptno = d.deptno
join position p on e.posid=p.posid
where 1=1 and e.empid like '% %'
and e.deptno=3 and e.onDuty= 1
and to_char(e.hiredate,'YYYY-MM-DD')>='1992-09-20'
/**
* 查询指定条件的员工
* @param empId2
* @param deptno2
* @param onDuty
* @return
*/
@Override
public List<Employee> find(String empId2,int deptno2,int onDuty,String hireDate2){
Connection conn = null;
PreparedStatement ps= null;
ResultSet rs = null;
List<Employee> list = new ArrayList<Employee>();
StringBuilder sql = new StringBuilder("select e.empid,e.realname,e.hiredate, e.phone,d.deptname,p.pname "
+ "from employee e join dept d on e.deptno = d.deptno "
+ "join position p on e.posid=p.posid "
+ "where 1=1");
if(empId2!=null&&!"".equals(empId2)){
sql.append(" and e.empid like '%"+empId2+"%'");
}
if(deptno2!=0){
sql.append(" and e.deptno="+deptno2);
}
sql.append("and e.onDuty="+onDuty);
if(hireDate2!=null){
sql.append("and to_char(e.hiredate,'YYYY-MM-DD')>='"+hireDate2+"'");
}
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql.toString());
rs = ps.executeQuery();
while(rs.next()){
Employee emp = new Employee();
emp.setEmpId(rs.getString("empId"));
emp.setRealName(rs.getString("realName"));
emp.setHireDate(rs.getString("hireDate"));
emp.setPhone(rs.getString("phone"));
emp.setDeptName(rs.getString("deptName"));
emp.setPname(rs.getString("pname"));
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeAll(rs, ps, conn);
}
return list;
}
2.业务层:
添加多条件查询员工的方法;
@Override
public List<Employee> findEmp(String empId2, int deptno2, int onDuty, String hireDate2) {
return ed.find(empId2, deptno2, onDuty, hireDate2);
}
3.控制层:
接收视图层的并发送接收的数据实现记忆条件;
调用业务层的多条件查询方法,将获取的数据存放到request中。
/**
* 指定多条件查询
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
public void findEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//获取页面信息
String empId = request.getParameter("empId");
int deptno =0;
String deptno2 = request.getParameter("deptno");
try{
deptno = Integer.parseInt(deptno2);
}catch(NumberFormatException e){
e.printStackTrace();
}
int onDuty = 1;
String onDuty2 = request.getParameter("onDuty");//null
try{
onDuty = Integer.parseInt(onDuty2);
}catch(NumberFormatException e){
e.printStackTrace();
}
String hireDate =request.getParameter("hireDate");
//获取部门信息
DepartmentService deptService = new DepartmentServiceImpl();
List<Department> deptList = deptService.selDept();
request.setAttribute("deptList", deptList);
//调用业务层查询指定条件的员工
EmployeeService empService = new EmployeeServiceImpl();
List<Employee> emp = empService.findEmp(empId, deptno, onDuty, hireDate);
request.setAttribute("empList", emp);
request.setAttribute("empId", empId);
request.setAttribute("deptno",deptno);
request.setAttribute("onDuty",onDuty);
request.setAttribute("hireDate",hireDate);
request.getRequestDispatcher("/system/empSel.jsp").forward(request, response);
}
4.视图层:
获取并遍历数据并展现到页面
<form action="employeeServlet?method=findEmp" method="post">
<ul class="prosearch">
<li>
<label>查询:</label><i>用户名</i>
<a>
<input name="empId" type="text" class="scinput" value="${empId }" />
</a><i>所属部门</i>
<a>
<select class="select1" name="deptno">
<option value="0">--全部--</option>
<c:forEach items="${deptList }" var="dept">
<c:if test="${dept.deptno==deptno }">
<option value="${dept.deptno }" selected="selected">${dept.deptName }</option>
</c:if>
<c:if test="${dept.deptno!=deptno }">
<option value="${dept.deptno }" >${dept.deptName }</option>
</c:if>
</c:forEach>
</select>
</a>
</li>
<li>
<label>是否在职:</label>
<c:if test="${onDuty==1 }">
<input name="onDuty" type="radio" value="1" checked="checked" /> 是
<input name="onDuty" type="radio" value="0" /> 否
</c:if>
<c:if test="${onDuty==0 }">
<input name="onDuty" type="radio" value="1" /> 是
<input name="onDuty" type="radio" value="0" checked="checked"/> 否
</c:if>
<c:if test="${empty onDuty }">
<input name="onDuty" type="radio" value="1" checked="checked" /> 是
<input name="onDuty" type="radio" value="0" /> 否
</c:if>
</li>
<li>
<label>入职时间:</label>
<a>
<input name="hireDate" type="text" class="scinput" onfocus="WdatePicker()" value="${hireDate }"/>
</a>
</li>
<a>
<input name="" type="submit" class="sure" value="查询" />
</a>
</ul>
</form>
-
效果图:
效果图
四、删除员工
1.数据访问层:
添加删除数据的方法;根据id删除员工;
SQL语句:delete from employee where empid = ' ';
/**
* 删除
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void deleteEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String empId = request.getParameter("empId2");
//调用业务层完成删除操作
EmployeeService empService = new EmployeeServiceImpl();
empService.delete(empId);
request.getRequestDispatcher("/employeeServlet?method=findAll").forward(request, response);
}
2.业务层:
添加删除方法;
@Override
public void delete(String empId) {
this.ed.delete(empId);
}
3.控制层:
获取视图层页面信息的用户ID;
调用业务层删除方法。
/**
* 删除
* @throws ServletException
* @throws IOException
*/
public void deleteEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String empId = request.getParameter("empId2");
//调用业务层完成删除操作
EmployeeService empService = new EmployeeServiceImpl();
empService.delete(empId);
request.getRequestDispatcher("/employeeServlet?method=findAll").forward(request, response);
}
4.视图层:
添加一个弹窗,删除时进行确认是否删除;
//添加弹窗
function deleteEmp(empId){
var flag = window.confirm("确定删除吗?");
if(flag){
location.href = "employeeServlet?method=deleteEmp&empId2="+empId;
}
}
//删除按钮
<a href="javascript:deleteEmp('${emp.empId}')" class="tablelink "> 删除</a>
-
效果图:
image.png
五、更新员工
1.数据访问层:
(1)查询指定ID的员工信息
(2)对员工信息进行修改操作;
SQL语句:update employee set password=?,deptno=?,posid=?,mgrid=?,realname=?,
sex=?,birthdate=to_date(?,'yyyy-mm-dd'),hiredate=to_date(?,'yyyy-mm-dd'),
leavedate=to_date(?,'yyyy-mm-dd'),onDuty=?,empType=?,phone=?,qq=?,
emerContactPerson=?,idCard=?
where empId=?
/**
* 查询指定ID的员工信息
*/
@Override
public Employee findById(String empId) {
String sql = "select * from employee where empId=?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Employee emp = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, empId);
rs = ps.executeQuery();
while(rs.next()){
emp = new Employee();
emp.setEmpId(rs.getString("empId"));
emp.setPassWord(rs.getString("passWord"));
emp.setDeptNo(rs.getInt("deptNo"));
emp.setPosId(rs.getInt("posId"));
emp.setMgrId(rs.getString("mgrId"));
emp.setRealName(rs.getString("realName"));
emp.setSex(rs.getString("sex"));
emp.setBirthDate(rs.getString("birthDate"));
emp.setHireDate(rs.getString("hireDate"));
emp.setLeaveDate(rs.getString("leaveDate"));
emp.setOnDuty(rs.getInt("onDuty"));
emp.setEmpType(rs.getInt("empType"));
emp.setPhone(rs.getString("phone"));
emp.setQq(rs.getString("qq"));
emp.setEmerContactPerson(rs.getString("emerContactPerson"));
emp.setIdCard(rs.getString("idCard"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeAll(rs, ps, conn);
}
return emp;
}
/**
* 修改操作
*/
@Override
public int update(Employee emp) {
String sql = "update employee set password=?,deptno=?,posid=?,mgrid=?,realname=?,"
+ "sex=?,birthdate=to_date(?,'yyyy-mm-dd'),hiredate=to_date(?,'yyyy-mm-dd'),"
+ "leavedate=to_date(?,'yyyy-mm-dd'),onDuty=?,empType=?,phone=?,qq=?,"
+ "emerContactPerson=?,idCard=? where empId=?";
Object[] obj = {emp.getPassWord(),emp.getDeptNo(),emp.getPosId(),emp.getMgrId(),
emp.getRealName(),emp.getSex(),emp.getBirthDate(),emp.getHireDate(),emp.getLeaveDate(),
emp.getOnDuty(),emp.getEmpType(),emp.getPhone(),emp.getQq(),emp.getEmerContactPerson(),
emp.getIdCard(),emp.getEmpId()};
return DBUtil.executeUpdate(sql, obj);
}
2.业务层:
(1)添加查询指定ID员工信息的方法;
(2)添加修改员工信息的方法。
@Override
public int update(Employee emp) {
return ed.update(emp);
}
3.控制层:
(1)先查询指定ID的员工信息,进行回显到修改页面;
(2)再调用用业务层修改员工信息,判断是否修改成功,成功后跳转到查询全部员工的页面;失败后回到该页面并提示修改失败。
/**
* 修改前准备
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String empId = request.getParameter("empId");
//调用业务层获取员工信息
EmployeeService empService = new EmployeeServiceImpl();
Employee emp = empService.findById(empId);
request.setAttribute("emp", emp);
//获取所有部门信息
DepartmentService deptService = new DepartmentServiceImpl();
List<Department> deptList = deptService.selDept();
request.setAttribute("deptList", deptList);
//获取所有岗位信息
PositionService pos = new PositionServiceImpl();
List<Position> posList = pos.findAll();
request.setAttribute("posList", posList);
//获取上级员工
List<Employee> mgrList = empService.findByType(2);
request.setAttribute("mgrList", mgrList);
//页面跳转
request.getRequestDispatcher("/system/empUpdate.jsp").forward(request, response);
}
/**
* 修改
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
public void update (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
//获取页面信息
String empId = request.getParameter("empId");
String password = "123456";
int deptNo = Integer.parseInt(request.getParameter("deptno"));
int posId = Integer.parseInt(request.getParameter("posId"));
String mgrId =request.getParameter("mgrId");
String realName = request.getParameter("realName");
String sex = request.getParameter("sex");
String birthDate = request.getParameter("birthDate");
String hireDate = request.getParameter("hireDate");
String leaveDate = request.getParameter("leaveDate");
int onDuty =Integer.parseInt(request.getParameter("onDuty"));
int empType = Integer.parseInt(request.getParameter("empType"));
String phone = request.getParameter("phone");
String qq = request.getParameter("qq");
String emerContactPerson = request.getParameter("emerContactPerson");
String idCard = request.getParameter("idCard");
//调用业务层完成添加
Employee emp = new Employee(empId, password, deptNo, posId, mgrId, realName, sex, birthDate, hireDate, leaveDate, onDuty, empType, phone, qq, emerContactPerson, idCard);
EmployeeService empService = new EmployeeServiceImpl();
int n = empService.update(emp);
if(n>0){
response.sendRedirect("/employeeServlet?method=findAll");
}else{
request.setAttribute("error", "修改失败!");
request.getRequestDispatcher("/system/empUpdate.jsp").forward(request, response);
}
}
public void resetpwd(HttpServletRequest request, HttpServletResponse response){
}
4.视图层:
获取控制层查询到的信息在页面进行显示。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/select.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.idTabs.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="editor/kindeditor.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$(".select1").uedSelect({
width : 345
});
});
</script>
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="#">人事管理</a></li>
<li><a href="#">修改员工信息</a></li>
</ul>
</div>
<div class="formbody">
<div class="formtitle"><span>基本信息</span></div>
<form action="employeeServlet?method=update" method="post">
<ul class="forminfo">
<li>
<label>用户名</label>
<input name="empId" type="text" class="dfinput" value="${emp.empId }" readonly="readonly"/><i>必须唯一,也可以根据真实姓名自动生成</i></li>
<li>
<li>
<label>真实姓名</label>
<input name="realName" type="text" class="dfinput" value="${emp.realName }"/><i></i></li>
<li>
<label>性别</label><cite>
<c:if test="${emp.sex=='男' }">
<input name="sex" type="radio" value="" checked="checked" />男
<input name="sex" type="radio" value="" />女<i>也可以根据身份证号自动获取</i></cite>
</c:if>
<c:if test="${emp.sex=='女' }">
<input name="sex" type="radio" value="男"/>男
<input name="sex" type="radio" value="女" checked="checked" />女<i>也可以根据身份证号自动获取</i></cite>
</c:if>
</li>
<li>
<label>出生日期</label>
<input name="birthDate" type="text" class="dfinput" value="${emp.birthDate }" /><i>也可以根据身份证号自动获取</i></li>
<li>
<li>
<label>入职时间</label>
<input name="hireDate" type="text" class="dfinput" value="${emp.hireDate }"/><i></i></li>
<li>
<label>离职时间</label>
<input name="leaveDate" type="text" class="dfinput" value="${emp.leaveDate }"/><i></i></li>
<li>
<label>是否在职</label><cite>
<input name="onDuty" type="radio" value="1" checked="checked" />是
<input name="onDuty" type="radio" value="0" />否</cite>
</li>
<li>
<label>员工类型</label><cite>
<input name="empType" type="radio" value="1" checked="checked" />基层员工
<input name="empType" type="radio" value="2" />各级管理人员</cite>
</li>
<li>
<label>所属部门<b>*</b></label>
<div class="vocation">
<select class="select1" name="deptno">
<c:forEach items="${deptList }" var="dept">
<option value="${dept.deptno }">${dept.deptName }</option>
</c:forEach>
</select>
</div>
</li>
<li>
<label>从事岗位<b>*</b></label>
<div class="vocation">
<select class="select1" name="posId">
<option value="1">总裁</option>
<option value="2">教学经理</option>
<option value="3">咨询经理</option>
<option value="4">咨询师</option>
<option value="5">讲师</option>
</select>
</div>
</li>
<li>
<label>直接上级<b>*</b></label>
<div class="vocation">
<select class="select1" name="mgrId">
<option value="">没有上级</option>
<c:forEach items="${mgrList }" var="mgr">
<option value="${mgr.empId }">${mgr.realName }</option>
</c:forEach>
</select>
</div>
<input name="" type="text" class="dfinput" placeholder="也可以在此输入首字母帮助显示"/></li>
</li>
<li>
<label>联系方式</label>
<input name="phone" type="text" class="dfinput" value="${emp.phone }"/>
</li>
<li>
<label>QQ号</label>
<input name="qq" type="text" class="dfinput" value="${emp.qq }"/>
</li>
<li>
<label>紧急联系人信息</label>
<textarea name="emerContactPerson" cols="" rows="" class="textinput">${emp.emerContactPerson }</textarea>
</li>
<li>
<label>身份证号</label>
<input name="idCard" type="text" class="dfinput" value="${emp.idCard }"/>
</li>
<li>
<label> </label>
<input name="" type="submit" class="btn" value="确认保存" />
</li>
</ul>
</form>
</div>
</body>
</html>
-
效果图:
修改界面
网友评论