Java Web项目 慧心人力资源管理系统[通俗易懂]

2022-09-02 10:06:14 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

题目:慧心人力资源管理系统 文档下载:https://download.csdn.net/download/weixin_44893902/16336711 完整项目下载:https://download.csdn.net/download/weixin_44893902/16393518

目录

一、语言和环境

二、实现功能

三、数据库设计

四、具体要求及推荐实现步骤

五、评分标准

六、实现代码


一、语言和环境

  1. 实现语言:JAVA语言。
  2. 环境要求:MyEclipse/Eclipse Tomcat MySql。
  3. 使用技术:Jsp Servlet JavaBean
  4. 数据库技术:JDBC

二、实现功能

人力资源公司为了提高管理效率,需开发一套B/S结构人力资源管理系统,对员工信息进行管理,主要功能如下:

1、首页默认显示现有的所有员工信息

图1 首页显示所有员工信息

2、鼠标悬停某行数据,该行样式发生变化

图2 鼠标悬停样式效果

3、表格最后一行为统计行,显示当前一共有多少条数据

图3 统计行效果

4、 点击右下“新增”按钮进入新增页面,输入相关信息后点击添加,完成新增员工信息功能,并且返回到首页显示最新数据,如图4、5所示

图4 添加员工信息

图5 添加成功后首页刷新数据

5、点击每行最后删除功能时弹出提示框,用户确认后删除该行数据并刷新最新员工数据,如图6所示。

图6 删除确认提示窗口

图7 删除后刷新数据

6、点击每行更新链接后跳转到更新页面,更新界面显示出要修改的员工信息,更新成功后跳转到首页显示最新数据,如图8、9所示:

图8 更新员工数据

图9 更新后刷新数据

三、数据库设计

  1. 创建数据库(HRDB)。
  2. 创建员工信息表(TB_EMP),结构如下。

字段名

说明

字段类型

长度

备注

id

员工编号

int

主键,自增,增量为1

name

员工姓名

varchar

50

不能为空

gender

员工性别

varchar

50

不能为空

birthday

员工生日

varchar

50

不能为空

phone

手机号码

varchar

11

不能为空

四、具体要求及推荐实现步骤

  1. 创建数据库,创建员工信息表,添加测试数据(至少添加4条测试数据)。
  2. 创建web工程并创建各个包,导入工程所需的jar文件。
  3. 创建Employee实体类。
  4. 创建BaseDAO工具类使用JDBC完成数据表数据的增删改查。创建Servlet相关类完成功能对应的用户请求处理。
  5. 编写JSP页面进行数据展示,以及新增、修改的界面。

五、评分标准

题目:慧心人力资源管理系统

5

数据库(提交数据库脚本)

5

创建数据库及添加测试数据

5

正确创建和编写实体类,包含所有属性及方法

2

正确创建BaseDao类,包含获取连接及关闭资源方法

35

正确创建EmployeeDAO类或者EmployeeMapper接口,使用jdbc

10

正确创建查询数据返回集合的方法

5

正确创建删除数据的方法

10

正确创建更新数据的方法

10

正确创建新增数据的方法

30

Servlet相关处理类

10

正确创建处理全部数据查询查询请求Servlet类,web.xml中请求映射配置或注解正确

5

正确创建处理新增数据请求Servlet类,web.xml中请求映射配置或注解正确

5

正确创建处理删除数据请求Servlet类,web.xml中请求映射配置或注解正确

5

正确创建处理更新数据时单个数据查询请求Servlet类,web.xml中请求映射配置或注解正确

5

正确创建处理数据更新请求Servlet类,web.xml中请求映射配置或注解正确

18

JSP相关页面

10

展示所有数据页面 7分 显示统计信息3分

3

修改员工信息页面

5

新增员工信息页面

5

总体编程技术

2

编码命名规范

3

程序正常运行,无异常

总分

100分

六、实现代码

1、目录结构:

2、使用到的包:

3、Java代码:

util

BaseDao.java

代码语言:javascript复制
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class BaseDao {
	
	private static String url="jdbc:mysql://localhost:3306/hrdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
	private static String userName="root";
	private static String password="root";
	private static String driver="com.mysql.jdbc.Driver";
	private static Connection connection;
	private static Properties properties = new Properties();
	
	static{
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection(){
		try {
			if(connection==null||connection.isClosed()){
				connection = DriverManager.getConnection(url,userName,password);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	public static void close(Connection connection) throws Exception {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
	}
	public static void main(String[] args) {
		System.out.println(getConnection());
	}
}

entity

TbEmp.java

代码语言:javascript复制
package entity;

import java.io.Serializable;

public class TbEmp implements Serializable{

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String gender;
	private String birthday;
	private String phone;
	public TbEmp() {
		super();
	}
	@Override
	public String toString() {
		return "TbEmp [id="   id   ", name="   name   ", gender="   gender   ", birthday="   birthday   ", phone="
				  phone   "]";
	}
	public TbEmp(Integer id, String name, String gender, String birthday, String phone) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
		this.phone = phone;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
}

mapper

EmpMapper.java

代码语言:javascript复制
package mapper;

import java.util.List;

import entity.TbEmp;

public interface EmpMapper {
	//查询所有
	List<TbEmp> queryAll();
	//添加
	int addEmp(TbEmp tbEmp);
	//更新
	int updateEmp(TbEmp tbEmp);
	//删除
	int del(int id);
}

service

EmpService.java

代码语言:javascript复制
package service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;

import entity.TbEmp;
import mapper.EmpMapper;
import util.BaseDao;

public class EmpService implements EmpMapper {

	@Override
	public List<TbEmp> queryAll() {
		Connection conn = null;
		String sql = null;
		try {
			sql = "select * from tb_emp";
			conn = (Connection) BaseDao.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet resultSet = ps.executeQuery();
			ArrayList<TbEmp> arrayList = new ArrayList<TbEmp>();
			while (resultSet.next()) {
				TbEmp tbEmp = new TbEmp();
				tbEmp.setId(resultSet.getInt("id"));
				tbEmp.setName(resultSet.getString("name"));
				tbEmp.setGender(resultSet.getString("gender"));
				tbEmp.setBirthday(resultSet.getString("birthday"));
				tbEmp.setPhone(resultSet.getString("phone"));
				arrayList.add(tbEmp);
			}
			if (arrayList.size() > 0) {
				return arrayList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public int addEmp(TbEmp tbEmp) {
		Connection connection = null;
		try {
			String sql = "insert into tb_emp(name,gender,birthday,phone) values(?,?,?,?)";
			connection = (Connection) BaseDao.getConnection();
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, tbEmp.getName());
			ps.setString(2, tbEmp.getGender());
			ps.setString(3, tbEmp.getBirthday());
			ps.setString(4, tbEmp.getPhone());
			int add = ps.executeUpdate();
			if (add > 0) {
				return add;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int updateEmp(TbEmp tbEmp) {
		Connection connection = null;
		try {
			String sql = "update tb_emp set name='"   tbEmp.getName()   "',gender='"   tbEmp.getGender()
					  "',birthday='"   tbEmp.getBirthday()   "',phone='"   tbEmp.getPhone()   "' where id='"
					  tbEmp.getId()   "'";
			System.out.println(sql);
			connection = (Connection) BaseDao.getConnection();
			PreparedStatement ps = connection.prepareStatement(sql);
			int update = ps.executeUpdate();
			if (update > 0) {
				return update;
			}
		} catch (Exception e) {
		}
		return 0;
	}

	@Override
	public int del(int id) {
		Connection connection = null;
		String sql = null;
		try {
			sql = "delete from tb_emp where id="   id;
			connection = (Connection) BaseDao.getConnection();
			PreparedStatement ps = connection.prepareStatement(sql);
			int delUpdate = ps.executeUpdate();
			if (delUpdate > 0) {
				return delUpdate;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

}

controller

EmpController.java

代码语言:javascript复制
package controller;

import java.util.List;

import entity.TbEmp;
import mapper.EmpMapper;
import service.EmpService;

public class EmpController {
	EmpMapper empMapper=new EmpService();
	public List<TbEmp> allInfoList(){
		List<TbEmp> queryAll = empMapper.queryAll();
		return queryAll;
	}
	//添加
	public int addEmp(TbEmp tbEmp){
		return empMapper.addEmp(tbEmp);
	}
	//更新
	public int updataEmp(TbEmp tbEmp){
		return empMapper.updateEmp(tbEmp);
	}
	//删除
	public int delInfo(int id){
		return empMapper.del(id);
	}
	
}

servlet

EmpShowServlet.java

代码语言:javascript复制
package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import controller.EmpController;
import entity.TbEmp;

@WebServlet("/EmpShowServlet")
public class EmpShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public EmpShowServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		EmpController controller=new EmpController();
		List<TbEmp> tbemp = controller.allInfoList();	
		HttpSession session = request.getSession();
		session.setAttribute("empsList", tbemp);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}

}

AddEmpServlet.java

代码语言:javascript复制
package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controller.EmpController;
import entity.TbEmp;

@WebServlet("/AddEmpServlet")
public class AddEmpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AddEmpServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		EmpController controller = new EmpController();
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String birthday = request.getParameter("birthday");
		String phone = request.getParameter("phone");
		TbEmp emp = new TbEmp(0, name, gender, birthday, phone);
		int addEmp = controller.addEmp(emp);
		if (addEmp > 0) {
			response.sendRedirect("EmpShowServlet");
		}
	}

}

DelServlet.java

代码语言:javascript复制
package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controller.EmpController;

@WebServlet("/DelServlet")
public class DelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public DelServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EmpController controller=new EmpController();
		String id=request.getParameter("id");
		int id1=Integer.parseInt(id);
		int del = controller.delInfo(id1);
		if (del>0) {
			response.sendRedirect("EmpShowServlet");
		}
	}

}

UpdatEmpServlet.java

代码语言:javascript复制
package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controller.EmpController;
import entity.TbEmp;

@WebServlet("/UpdatEmpServlet")
public class UpdataEmpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UpdataEmpServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		EmpController controller=new EmpController();
		Integer id= Integer.parseInt(request.getParameter("id").toString());
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String birthday = request.getParameter("birthday");
		String phone = request.getParameter("phone");
		TbEmp emp=new  TbEmp(id,name,gender,birthday,phone);
		System.out.println(emp.toString());
		int updatEmp = controller.updataEmp(emp);
		if (updatEmp>0) {
			response.sendRedirect("EmpShowServlet");
		}
	}

}

4、HTML代码:

main.jsp

代码语言:javascript复制
<%@page import="entity.TbEmp"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>慧心人力资源管理系统</title>
<style type="text/css">
body {
	width: 100%;
}

.wrap_table {
	width: 60%;
	margin: 0 auto;
	text-align: center;
}

table {
	text-align: center;
	margin: 0 auto;
}

table tr:nth-last-child(1) {
	padding-right: 20px;
}

table th {
	padding: 10px;
}

table:nth-last-child(2) tr:hover, table tr:hover a {
	color: white;
	background-color: black;
}

table:nth-last-child(1) tr {align ="left";
	
}

table:nth-last-child(1) td {
	padding: 10px;
}

table:nth-last-child(2) td {
	padding: 5px;
}

#end {
	text-align: right;
}

#name {
	margin-bottom: 10px;
}

#price {
	margin-bottom: 10px;
}

#num {
	margin-bottom: 10px;
}

#type {
	margin-bottom: 10px;
}
</style>
</head>
<body >
	<div class="wrap_table">
		<table border="1" cellspacing="0" cellpadding="0" height="300px">
			<caption align="top"
				style="font-size: 1.6rem; height: 50px; font-weight: bold;">人力资源管理</caption>
			<tr>
				<th width="80">员工编号</th>
				<th width="120">员工姓名</th>
				<th width="80">员工性别</th>
				<th width="140">员工生日</th>
				<th width="140">员工手机</th>
				<th width="120">操作</th>
			</tr>
			<%
				List<TbEmp> empsList = (List<TbEmp>) session.getAttribute("empsList");
				int i = 0;
			%>
			<c:forEach items="${empsList}" var="emp">
				<tr>
					<td>${emp.id }</td>
					<td>${emp.name }</td>
					<td>${emp.gender }</td>
					<td>${emp.birthday }</td>
					<td>${emp.phone }</td>
					<td><input type="button" onclick="update(this)" class="updata"
						value="更新" /> <input type="button" onclick="del(${emp.id })"
						value="删除" /></td>
				</tr>

			</c:forEach>
			<tr>
				<td colspan="6" id="end"><span>共计<%=empsList.size()   1%>条数据
				</span> <a href="addEmp.jsp">新增</a></td>
			</tr>

		</table>
		<div
			style="width: 500px; height: 400px; border: 1px solid #000; background-color: white; display: none; position: absolute; left: 40%; top: 10%;"
			id="UpdataDiv">
			<form action="UpdatEmpServlet" method="post" id="UpdataForm">
				<br /> <br />
				<table border="1" cellspacing="0" cellpadding="0"
					style="text-align: left;" width="400" height="200">
					<caption align="top"
						style="font-size: 1.6rem; height: 50px; font-weight: bold;">更新员工信息</caption>
					<tr style="display: none">
						<td><input type="text" name="id" id="id" value=""></td>
					</tr>
					<tr>
						<td>员工姓名:<input type="text" name="name" id="name" value=""></td>
					</tr>
					<tr>
						<td>员工性别:<input id="man" type="radio" name="gender" value="男" />男
							<input id="woman" type="radio" name="gender" value="女">女
						</td>
					</tr>
					<tr>
						<td>员工生日:<input id="birthday" name="birthday" type="date"
							value="" /></td>
					</tr>
					<tr>
						<td>员工号码:<input type="text" name="phone" id="phone" value=""></td>
					</tr>
					<tr align="center">
						<td><input type="submit" value="提交"> <input
							type="button" value="取消" class="updata2"></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
	<script type="text/javascript">
		$(".updata").click(function(){
			$("#UpdataDiv").css("display","block");
			
			});
		$(".updata2").click(function(){
			$("#UpdataDiv").css("display","none");
			});
			
		</script>
	<script type="text/javascript">
		function update(obj) {
			   var id=$(obj).parents("tr").children("td").eq(0).text();
			   var name=$(obj).parents("tr").children("td").eq(1).text();
			   var gender=$(obj).parents("tr").children("td").eq(2).text();
			   var birthday=$(obj).parents("tr").children("td").eq(3).text();
			   var phone=$(obj).parents("tr").children("td").eq(4).text();
			   document.getElementById("id").value = id;
			   document.getElementById("name").value = name;
			   if (gender=="男") {
					document.getElementById("man").checked = true
			   }
			   if (gender=="女") {
			   		document.getElementById("woman").checked = true
			   }
			   document.getElementById("birthday").value = birthday;
			   document.getElementById("phone").value = phone;  
			}
		function  del(id) {
			 if (confirm("确定删除该数据?")) {
				window.location.href="DelServlet?id=" id;
	         }
		}
		function a(){
			$.ajax({  
	    		url:"EmpShowServlet",//servlet文件的名称
	    		type:"POST",
	    		success:function(e){
	    			alert("servlet调用成功!");
	    		}
	    	});
			
		}
		</script>
</body>
</html>

addEmp.jsp

代码语言:javascript复制
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>慧心人力资源管理系统-添加员工信息</title>
<style type="text/css">
body {
	width: 100%;
}

.wrap_table {
	width: 40%;
	margin: 0 auto;
	text-align: center;
}

table {
	text-align: left;
	margin: 0 auto;
	height: 200px;
	width: 400px;
}

table td {
	padding: 10px;
}

.button_class input {
	padding: 3px 10px;
}
</style>
</head>
<body>
	<div class="wrap_table">
		<form action="AddEmpServlet" method="post">
			<table border="1" cellspacing="0" cellpadding="0">
				<caption align="top"
					style="font-size: 1.6rem; height: 50px; font-weight: bold;">添加员工信息</caption>
				<tr>
					<td>员工姓名:</td>
					<td><input type="text" name="name" id="name" value="" /></td>
				</tr>
				<tr>
					<td>性别:</td>
					<td><input id="man" type="radio" checked="checked"
						name="gender" value="男" />男 <input id="woman" type="radio"
						name="gender" / value="女">女</td>
				</tr>
				<tr>
					<td>员工生日:</td>
					<td><input id="birthday" name="birthday" type="date" value="" />
				</tr>
				<tr>
					<td>员工手机:</td>
					<td><input type="text" name="phone" id="" value="" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center" class="button_class"><input
						type="submit" name="" id="" value="添加" /> <input type="reset"
						name="" id="" value="重置" /></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140007.html原文链接:https://javaforall.cn

0 人点赞