这是基于Struts2框架写的一个员工管理系统,可以进行员工的增删改查操作,还有登录功能。
话不多说,先来几个运行界面,下面上代码:
用户登录:
注册,也就是新增员工信息,这里写的直接新增员工信息后,员工可以直接登录,你可以改成单独分一个管理人员登录表:
登录成功之后,员工信息列表:
然后进行修改操作,修改页面和注册页面一样,但是会根据你要修改的id自动填充信息,删除就是直接删除。
上代码:
1、目录结构:
2、LoginAction.java
package com.hnpi.action;
import java.util.List;
import java.util.Map;
import com.hnpi.bean.User;
import com.hnpi.service.UserService;
import com.hnpi.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String login() {
UserService userService = new UserServiceImpl();
List users = userService.selectAllUser();
if (username != null && !"".equals(username) && password != null && !"".equals(password)) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getUsername().equals(username) && users.get(i).getPassword().equals(password)) {
// 登录成功 保留登录信息
Map session = ActionContext.getContext().getSession();
session.put("user", username);
return "success";
}
}
return "fail";
} else {
return "fail";
}
}
public String register() {
User user = new User(0, username, password, realname, sex, birthday, nation, education, post, phone, address,
email);
UserService userService = new UserServiceImpl();
if (userService.addUser(user)) {
return "success";
} else
return "fail";
}
private int id;
private String username;
private String password;
private String realname;
private String sex;
private String birthday;
private String nation;
private String education;
private String post;
private String phone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3、UserAction.java
package com.hnpi.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.hnpi.bean.User;
import com.hnpi.service.UserService;
import com.hnpi.service.impl.UserServiceImpl;
public class UserAction {
/**
* 员工信息列表
*
* @return
*/
public String userList() {
// 将获取的数据传递至userlist.jsp页面
UserService userService = new UserServiceImpl();
List users = userService.selectAllUser();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("users", users);
return "success";
}
/**
* 根据id查询图书 直接跳转到准备更新图书页面
*
* @return
*/
public String selectUserById() {
UserService userService = new UserServiceImpl();
User user = userService.selectUserById(id);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("userById", user);
return "success";
}
/**
* 更新员工信息
*
* @return
*/
public String updateUser() {
User user = new User(id, username, password, realname, sex, birthday, nation, education, post, phone, address,
email);
UserService userService = new UserServiceImpl();
if (userService.updateUser(user)) {
return "success";
} else
return "fail";
}
/**
* 删除员工信息
*
* @param id
* @return
*/
public String deleteById() {
UserService userService = new UserServiceImpl();
if (userService.delUser(id)) {
return "success";
} else
return "fail";
}
private int id;
private String username;
private String password;
private String realname;
private String sex;
private String birthday;
private String nation;
private String education;
private String post;
private String phone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
4、User.java
package com.hnpi.bean;
public class User {
private int id;
private String username;
private String password;
private String realname;
private String sex;
private String birthday;
private String nation;
private String education;
private String post;
private String phone;
private String address;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", realname=" + realname
+ ", sex=" + sex + ", birthday=" + birthday + ", nation=" + nation + ", education=" + education
+ ", post=" + post + ", phone=" + phone + ", address=" + address + ", email=" + email + "]";
}
public User(int id, String username, String password, String realname, String sex, String birthday, String nation,
String education, String post, String phone, String address, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.realname = realname;
this.sex = sex;
this.birthday = birthday;
this.nation = nation;
this.education = education;
this.post = post;
this.phone = phone;
this.address = address;
this.email = email;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
5、UserDaoImpl.java
package com.hnpi.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hnpi.bean.User;
import com.hnpi.dao.UserDao;
import com.hnpi.util.DBUtil;
public class UserDaoImpl implements UserDao {
/**
* 查找用户列表
*/
public List selectAllUser() {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user";
List users = new ArrayList();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setRealname(rs.getString(4));
user.setSex(rs.getString(5));
user.setBirthday(rs.getString(6));
user.setNation(rs.getString(7));
user.setEducation(rs.getString(8));
user.setPost(rs.getString(9));
user.setPhone(rs.getString(10));
user.setAddress(rs.getString(11));
user.setEmail(rs.getString(12));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn, ps, null);
}
return users;
}
/**
* 新增用户
*/
public boolean addUser(User user) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
int result = 0;
String sql = "insert into user (username,password,realname,sex,birthday,nation,education,post,phone,address,email) values (?,?,?,?,?,?,?,?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getRealname());
ps.setString(4, user.getSex());
ps.setString(5, user.getBirthday());
ps.setString(6, user.getNation());
ps.setString(7, user.getEducation());
ps.setString(8, user.getPost());
ps.setString(9, user.getPhone());
ps.setString(10, user.getAddress());
ps.setString(11, user.getEmail());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn, ps, null);
}
if (result > 0) {
return true;
} else {
return false;
}
}
/**
* 根据ID查询员工信息
*/
public User selectUserById(int id) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user where id = ?";
User user = new User();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setRealname(rs.getString(4));
user.setSex(rs.getString(5));
user.setBirthday(rs.getString(6));
user.setNation(rs.getString(7));
user.setEducation(rs.getString(8));
user.setPost(rs.getString(9));
user.setPhone(rs.getString(10));
user.setAddress(rs.getString(11));
user.setEmail(rs.getString(12));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn, ps, null);
}
return user;
}
/**
* 修改员工信息
*/
public boolean updateUser(User user) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
int result = 0;
String sql = "update user set username = ?,password = ?,realname = ?,sex = ?,birthday = ?,nation = ?,education = ?,post = ?,phone = ?,address = ?,email = ? where id =?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getRealname());
ps.setString(4, user.getSex());
ps.setString(5, user.getBirthday());
ps.setString(6, user.getNation());
ps.setString(7, user.getEducation());
ps.setString(8, user.getPost());
ps.setString(9, user.getPhone());
ps.setString(10, user.getAddress());
ps.setString(11, user.getEmail());
ps.setInt(12, user.getId());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn, ps, null);
}
if (result > 0) {
return true;
} else {
return false;
}
}
/**
* 删除员工信息
*/
public boolean delUser(int id) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
int result = 0;
String sql = "delete from user where id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
if(result > 0){
return true;
}else{
return false;
}
}
}
6、UserDao.java
package com.hnpi.dao;
import java.util.List;
import com.hnpi.bean.User;
public interface UserDao {
/**
* 查询用户列表
*
* @return
*/
public List selectAllUser();
/**
* 新增用户
*
* @param user
* @return
*/
boolean addUser(User user);
/**
* 根据id查询员工信息
*
* @param id
* @return
*/
public User selectUserById(int id);
/**
* 更新员工信息
*
* @return
*/
public boolean updateUser(User user);
/**
* 删除员工信息
*
* @param id
* @return
*/
boolean delUser(int id);
}
7、UserInterceptor.java 这个是拦截器。这里可以不用也可以用,用的话需要在struts.xml文件里面配置拦截器。
package com.hnpi.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class UserInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String user=(String) ActionContext.getContext().getSession().get("user");
if (user==null||"".equals(user)) {
return "fail";
}
return invocation.invoke();
}
}
8、UserServiceImpl.java
package com.hnpi.service.impl;
import java.util.List;
import com.hnpi.bean.User;
import com.hnpi.dao.UserDao;
import com.hnpi.dao.impl.UserDaoImpl;
import com.hnpi.service.UserService;
public class UserServiceImpl implements UserService {
/**
* 调用Dao层实现查询
*/
public List selectAllUser() {
UserDao userDao = new UserDaoImpl();
List users = userDao.selectAllUser();
return users;
}
/**
* 调用Dao层实现新增
*/
public boolean addUser(User user) {
UserDao userDao = new UserDaoImpl();
return userDao.addUser(user);
}
/**
* 调用Dao层实现根据id查询
*/
public User selectUserById(int id) {
UserDao userDao = new UserDaoImpl();
return userDao.selectUserById(id);
}
/**
* 调用Dao层实现更新
*/
public boolean updateUser(User user) {
UserDao userDao = new UserDaoImpl();
return userDao.updateUser(user);
}
/**
* 调用Dao层实现删除
*/
public boolean delUser(int id) {
UserDao userDao = new UserDaoImpl();
return userDao.delUser(id);
}
}
9、UserService.java
package com.hnpi.service;
import java.util.List;
import com.hnpi.bean.User;
public interface UserService {
/**
* 查找用户列表
* @return
*/
public List selectAllUser();
/**
* 新增用户
*
* @param user
* @return
*/
boolean addUser(User user);
/**
* 根据id查询员工信息
* @param id
* @return
*/
public User selectUserById(int id);
/**
* 更新员工信息
*
* @return
*/
public boolean updateUser(User user);
/**
* 删除员工信息
*
* @param id
* @return
*/
boolean delUser(int id);
}
10、DBUtil.java 这是链接数据库的 要配置成你自己的数据库
package com.hnpi.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
/**
* 连接数据库
* @return
*/
public static Connection getConn(){
String url = "jdbc:mysql://localhost:3308/staff?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC";
String user = "root";
String pwd = "root";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库
* @param conn
* @param ps
* @param rs
*/
public static void closeConn(Connection conn,PreparedStatement ps,ResultSet rs){
try{
if(conn!=null){
conn.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
登录页面
用户登录
13、register.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 + "/";
%>
用户注册
注册
14、updateUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
修改员工信息页面
修改员工信息
15、struts.xml
userList
/user
userList
/user/login.jsp
/user/login.jsp
/user/register.jsp
/user/userList.jsp
/user/updateUser.jsp
userList
userList
16、这里用到了layui样式,直接搜索layui下载即可:
这就是员工管理系统的实现
可以到这里下载源码:https://download.csdn.net/download/weixin_42322648/12514517
代做java毕业设计,有意向可以加微信:18739766888