一、项目功能
1.什么是OA?
OA系统是一个企业用来办公管理的软件。是一个用来管理日常的办公事务的一个系统。
2.项目需求:
(1)部门管理;
(2)员工管理;
(3)考勤管理;
(4)报销管理;
(5)收支管理;
用例图
3.项目界面:
- 登录界面: 登录页面
- 主界面: 主界面
4.导入数据库:
数据库导入5.搭建项目的需求:
(1)创建项目sxtoa(workspace编码统一修改为utf-8);
(2)创建包和文件夹
(3) 加入jar(ojdbc6.jar gson-2.2.4.jar junit.jar log4j.jar);
(4) 加入工具类:DBUtil.java和BaseServlet;
(5) 加入过滤器;
(6)加入界面原型 ;
(7) 部署项目并测试( Junit测试);
二、部门管理功能
1.添加部门:
- 需求说明:
(1)创建实体类 :Department;
(2) 创建数据库访问层 Department Dao和Department DaoImpl;
(3) 创建业务层:Department Service和Department ServiceImpl;
(4) 控制层: DepartmentServlet extends BaseServlet;
(5) 视图层:system/deptAdd.jsp;
(6)Junit测试;
- 时序图: 时序图
- 代码示例:
Dao层:
/**
* 添加部门
*/
@Override
public int save(Department dept) {
String sql = "insert into dept values(?,?,?)";
Object [] params = {dept.getDeptno(),dept.getDeptName(),dept.getLocation()};
return DBUtil.executeUpdate(sql, params);
}
控制层:
/**
* 添加部门
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收视图层的表单数据
int deptno = Integer.parseInt(request.getParameter("deptno"));
String deptName = request.getParameter("deptName");
String location = request.getParameter("location");
//调用业务层完成添加操作
DepartmentService deptService = new DepartmentServiceImpl();
Department dept = new Department(deptno, deptName, location);
int n = deptService.add(dept);
//根据结果跳转到不同的页面
if(n>0){
//如果是表单的提交,成功之后建议使用重定向,避免表单的重复提交
//request.getRequestDispatcher("/deptList.html").forward(request, response);
response.sendRedirect(request.getContextPath()+"/servlet/DepartmentServlet?method=selDept");
}else{
request.setAttribute("error", "添加失败");
//此时必须使用转发,因为要复用保存在request中的数据
request.getRequestDispatcher("/system/deptAdd.jsp").forward(request, response);
}
}
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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" />
</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="servlet/DepartmentServlet?method=add" method="post">
<ul class="forminfo">
<li><label>部门编号</label><input name="deptno" type="text" class="dfinput" /> </li>
<li><label>部门名称</label><input name="deptName" type="text" class="dfinput" /> </li>
<li><label>办公地点</label><input name="location" type="text" class="dfinput" /></li>
<li><label> </label><input name="" type="submit" class="btn" value="确认保存"/></li>
</ul>
</form>
</div>
<span style="color: red;font-weight: bold;font-size: 16px;">${error}</span>
</body>
</html>
功能实现:
2.删除部门:
- 需求说明:
(1)开发视图层,实现删除部门超链接;
(2)开发控制层,实现删除部门功能;
(3)开发业务层,实现删除部门功能;
(4)开发数据访问层,实现删除部门功能;
- 代码示例:
Dao层:
/**
* 删除指定部门
*/
@Override
public int delDeptDao(int deptno) {
String sql = "delete from dept where deptno=?";
Object [] params = {deptno};
return DBUtil.executeUpdate(sql, params);
}
Servlet控制层:
/**
* 删除指定部门
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void delDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//获取请求系信息
int deptno = Integer.parseInt(request.getParameter("deptno")) ;
//调用业务层处理结果
DepartmentService service = new DepartmentServiceImpl();
service.delDept(deptno);
//跳转到指定页面
request.getRequestDispatcher("/servlet/DepartmentServlet?method=selDept").forward(request, response);
}
Jsp:
<%@ 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%>" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></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 deleteDept(deptno) {
var flag = window.confirm("您确定删除吗?");
if (flag) {
location.href = "servlet/DepartmentServlet?method=delDept&deptno=" + deptno;
}
}
</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">
<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>
</tr>
</thead>
<tbody>
<c:forEach items="${select }" var="dept">
<tr>
<td><input name="" type="checkbox" value="" /></td>
<td>${dept.deptno}</td>
<td>${dept.deptName}</td>
<td>${dept.location }</td>
<td><a
href="servlet/DepartmentServlet?method=findById&deptno=${dept.deptno }"
class="tablelink">修改</a> <a
href="javascript:deleteDept(${dept.deptno })" class="tablelink ">
删除</a></td>
</tr>
</c:forEach>
</table>
<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>
-
实现效果:
结果
3.修改部门信息:
- 需求说明:
(1)通过部门id查询部门是否存在;
(2)查询到部门信息后跳转到修改页面;
(3)对数据进行提交保存;
(4)如果修改成功跳转到部门查询页面,失败跳转回当前页面,并提示修改失败。
-
时序图:
更新部门时序图.png
Dao层:
/**
* 查询指定id部门
*/
@Override
public Department findByIdDao(int deptno) {
String sql = "select * from dept where deptno = ?";
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
//声明变量
Department dept = null;
try {
conn = DBUtil.getConnection();
pstm = conn.prepareStatement(sql);
pstm.setInt(1, deptno);
rs = pstm.executeQuery();
while(rs.next()){
dept = new Department();
dept.setDeptno(rs.getInt("deptno"));
dept.setDeptName(rs.getString("deptName"));
dept.setLocation(rs.getString("location"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dept;
}
/**
* 修改指定部门信息
*/
@Override
public int updateDao(Department dept) {
String sql = "update dept set deptname=?,location=? where deptno= ?";
Object [] params = {dept.getDeptName(),dept.getLocation(),dept.getDeptno()};
return DBUtil.executeUpdate(sql, params);
}
Servlet控制层:
/**
* 修改部门信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收视图层的表单数据
int deptno = Integer.parseInt(request.getParameter("deptno"));
String deptName = request.getParameter("deptName");
String location = request.getParameter("location");
//调用业务层完成添加操作
DepartmentService deptService = new DepartmentServiceImpl();
Department dept = new Department(deptno, deptName, location);
int n = deptService.updateService(dept);
//根据结果跳转到不同的页面
if(n>0){
//如果是表单的提交,成功之后建议使用重定向,避免表单的重复提交
//request.getRequestDispatcher("/deptList.html").forward(request, response);
response.sendRedirect(request.getContextPath()+"/servlet/DepartmentServlet?method=selDept");
}else{
request.setAttribute("error", "修改失败!");
//此时必须使用转发,因为要复用保存在request中的数据
request.getRequestDispatcher("/system/deptUpdate.jsp").forward(request, response);
}
}
/**
* 查询指定id的部门
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//获取请求信息
int deptno = Integer.parseInt(request.getParameter("deptno")) ;
//调用业务层处理结果
DepartmentService service = new DepartmentServiceImpl();
Department dept = service.findByIdService(deptno);
//跳转到指定页面
request.setAttribute("dept", dept);
request.getRequestDispatcher("/system/deptUpdate.jsp").forward(request, response);
}
Jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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" />
</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="servlet/DepartmentServlet?method=update" method="post">
<ul class="forminfo">
<li><label>部门编号</label><input name="deptno" type="text"
class="dfinput" value="${dept.deptno }" readonly="readonly" /></li>
<li><label>部门名称</label><input name="deptName" type="text"
class="dfinput" value="${dept.deptName }" /></li>
<li><label>办公地点</label><input name="location" type="text"
class="dfinput" value="${dept.location }" /></li>
<li><label> </label><input name="" type="submit"
class="btn" value="确认保存" /></li>
</ul>
</form>
</div>
<span style="color: red;font-size: 16px" ; font-weight: bold>${error }</span>
</body>
</html>
-
效果图:
image.png image.png
三、员工管理功能:
1.添加员工:
- 实体类创建:
private String empId;//员工编号
private String password;//密码
private String realName;//真实姓名
private String sex;//性别
private Date brithDate;//出生日期
private Date hireDate;//入职日期
private Date 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 Department dept;//员工所属部门 不仅包含部门的编号,还包含其他信息
private Position position;
private Employee mgr;//上级领导的信息
private List<Employee>empList = new ArrayList<Employee>();//下级的信息,可能多个
- 数据访问层:
@Override
public int save(Employee emp) {
String sql = "insert into employee values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
java.sql.Date leaveDate2 = null;
Date leaveDate = emp.getLeaveDate();
if(leaveDate!=null){
leaveDate2 = new java.sql.Date(leaveDate.getTime());
}
Object [] params = {
emp.getEmpId(),
emp.getPassword(),
emp.getDept().getDeptno(),
emp.getPosition().getPosId(),//?
emp.getMgr().getEmpId(),
emp.getRealName(),
emp.getSex(),
new java.sql.Date(emp.getBrithDate().getTime()),
new java.sql.Date(emp.getHireDate().getTime()),
leaveDate2, //???
emp.getOnDuty(),
emp.getEmpType(),
emp.getPhone(),
emp.getQq(),
emp.getEmerContactPerson(),
emp.getIdCard()
};
return DBUtil.executeUpdate(sql, params);
}
@Override
public List<Employee> findByType(int type) {
String sql= "select * from employee where emptype=?";
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
List<Employee> list = new ArrayList<Employee>();
try {
//建立数据库链接
conn = DBUtil.getConnection();
pstm = conn.prepareStatement(sql);
//4.使用SQL命令发送器发送SQL命令给数据库,并得到返回的结果(子弹)
pstm.setInt(1, type);
rs = pstm.executeQuery();
//处理结果
while(rs.next()){
Employee emp = new Employee();
emp.setEmpId(rs.getString("empId"));
emp.setRealName(rs.getString("realName"));
emp.setSex(rs.getString("sex"));
emp.setPhone(rs.getString("phone"));
list.add(emp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeAll(rs, pstm, conn);
}
return list;
}
- Servlet控制层:
package com.bjsxt.servlet;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bjsxt.entity.Department;
import com.bjsxt.entity.Employee;
import com.bjsxt.entity.Position;
import com.bjsxt.service.DepartmentService;
import com.bjsxt.service.EmployeeService;
import com.bjsxt.service.impl.DepartmentServiceImpl;
import com.bjsxt.service.impl.EmployeeServiceImpl;
public class EmployeeServlet extends BaseServlet{
public void empAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
//获取所有部门信息
DepartmentService deptService = new DepartmentServiceImpl();
List<Department> deptList = deptService.selDept();
request.setAttribute("deptList", deptList);
//获取所有的岗位信息
//获取上级员工
EmployeeService empService = new EmployeeServiceImpl();
List<Employee> mgrList = empService.findByType(2);//1 基层员工 2 各级管理人员
request.setAttribute("mgrList", mgrList);
//跳转
request.getRequestDispatcher("system/empAdd.jsp").forward(request, response);
}
public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
//获取员工的信息
String empId = request.getParameter("empId");
String password = "123456";
String realName = request.getParameter("realName");
String sex = request.getParameter("sex");
//日期类型的处理
String sbirthDate = request.getParameter("brithDate");
String shireDate = request.getParameter("hireDate");
String sleaveDate = request.getParameter("leaveDate");
Date birthDate= null,hireDate = null,leaveDate = null;
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
birthDate = sdf.parse(sbirthDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
hireDate = sdf.parse(shireDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
leaveDate = sdf.parse(sleaveDate);
} catch (ParseException e) {
e.printStackTrace();
}
//整数的处理
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");
//对象的处理
int deptno = Integer.parseInt(request.getParameter("deptno"));
Department dept = new Department();
dept.setDeptno(deptno);
int posId = Integer.parseInt(request.getParameter("posId"));
Position position = new Position();
position.setPosId(posId);
String mgrId = request.getParameter("mgrId");
Employee mgr = new Employee();
mgr.setEmpId(mgrId);//!!!
//调用业务层完成添加操作
Employee emp = new Employee(empId, password, realName, sex, birthDate, hireDate, leaveDate, onDuty, empType, phone, qq, emerContactPerson, idCard, dept, position, mgr, null);
EmployeeService empService = new EmployeeServiceImpl();
int n = empService.addEmp(emp);
//根据结果进行页面跳转
if(n>0){
response.sendRedirect(request.getContextPath()+"/empList.html");
}else{
request.setAttribute("error", "添加员工失败");
request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
}
}
}
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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:"500px",height:"300px",skinType:"tinymce"});
</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="servlet/EmployeeServlet?method=add" 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',isShowWeek:true})" /><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" id="ecp" cols="" rows="" 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>${error }</span>
</body>
</html>
-
实现效果:
修改页面 成功跳转页面
四、MVC架构:
1.MVC:
MVC它主要分模型、视图、控制器三层。
实现了显示模块与功能模块的分离
2.优点:
• 降低耦合性
• 分工协作
• 组件重用
3.缺点:
结构图增加了系统结构和实现的复杂性。
4. PowerDesigner面向对象建模:
- 用例图:(User Case Diagram) 通常用来定义系统的高层次草图,它从用户角度描述了应用的系统功能,指出了各个功能的外部操作者。包括用例、执行者、关联三个建模要素。
- 序列图:(Sequence Diagram):描述系统如何实际完成在User Case图中定义的功能。可以画出对象(类的实例)之间交互时产生的时序关系。主要包括执行者、对象、生命线、激活、消息、返回消息等建模要素。
- 类图(Class Diagram):描述类与类之间的静态关系。其中包括一系列的包,类,接口和它们之间的关系。类图是定义其他图的基础。
网友评论