公司人才管理系统源码简易版(进阶中1.0)
Java 后端页面
controller控制层
AdminController 管理者层面
import com.gl.ams.pojo.vo.Admin;
import com.gl.ams.service.AdminService;
import com.gl.ams.service.impl.AdminServiceImpl;
import com.gl.ams.servlet.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
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 java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@WebServlet(urlPatterns = "/dologin")
public class AdminController extends HttpServlet {
private AdminService adminService=new AdminServiceImpl();
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
Map<String,String[]> map= request.getParameterMap();
Admin admin=new Admin();
try {
BeanUtils.populate(admin,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Admin realadmin= adminService.login(admin);
if (realadmin==null){
response.getWriter().print("<script>alert('login no ok');history.go(-1);</script>");
}else{
request.getSession().setAttribute("loginadmin",realadmin);
response.sendRedirect(request.getContextPath()+"/admin/index.jsp");
}
}
}
DeptController 部门层面
import com.gl.ams.pojo.vo.Dept;
import com.gl.ams.service.DeptService;
import com.gl.ams.service.impl.DeptServiceImpl;
import com.gl.ams.servlet.BaseServlet;
import com.gl.ams.utils.DateUtil;
import com.gl.ams.utils.JsonResult;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = {
"/admin/dept"})
public class DeptController extends BaseServlet {
private DeptService deptService=new DeptServiceImpl();
public void doadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
Dept dept=new Dept();
dept.setDeptname(request.getParameter("deptname"));
dept.setDeptdesc(request.getParameter("deptdesc"));
String datestr=request.getParameter("createdate");
Date createdate= DateUtil.getDateByString(datestr,"yyyy-MM-dd");
dept.setCreatedate(createdate);
int result= deptService.addDept(dept);
JsonResult jsonResult = new JsonResult();
jsonResult.setData(result);
if (result==-1){
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("部门名称重复,添加失败");
}
if (result==0){
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("未知异常,添加失败");
}
Gson gson = new Gson();
response.getWriter().print(gson.toJson(jsonResult));
}
public void doedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
Dept dept=new Dept();
dept.setDeptname(request.getParameter("deptname"));
dept.setDeptdesc(request.getParameter("deptdesc"));
String datestr=request.getParameter("createdate");
Integer deptid=Integer.parseInt(request.getParameter("deptid"));
dept.setDeptid(deptid);
Date createdate= DateUtil.getDateByString(datestr,"yyyy-MM-dd");
dept.setCreatedate(createdate);
int result= deptService.editDept(dept);
JsonResult jsonResult=new JsonResult();
jsonResult.setData(result);
if (result==-1){
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("部门名称重复,修改失败");
}
if(result==0){
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("未知异常,修改失败");
}
Gson gson=new Gson();
response.getWriter().print(gson.toJson(jsonResult));
}
public void remove(HttpServletRequest request,HttpServletResponse response) throws IOException {
Integer deptid=Integer.parseInt(request.getParameter("id"));
int result= deptService.removeDeptById(deptid);
JsonResult jsonResult=new JsonResult();
jsonResult.setData(result);
if (result==-1){
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("该部门还有员工,删除失败");
}
if(result==0){
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("未知异常,删除失败");
}
Gson gson=new Gson();
response.getWriter().print(gson.toJson(jsonResult));
}
public void getdeptbyid(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
Integer deptid = Integer.parseInt(request.getParameter("id"));
Dept dept = deptService.getDeptById(deptid);
JsonResult jsonResult = new JsonResult();
jsonResult.setData(dept);
Gson gson = new Gson();
response.getWriter().print(gson.toJson(jsonResult));
}
public void checkname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
String deptname = request.getParameter("deptname");
Dept dept = deptService.getDeptByName(deptname);
if (dept == null) {
response.getWriter().print("true");
} else {
response.getWriter().print("false");
}
}
public void dolist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
List<Dept> depts= deptService.getAll();
Gson gson=new Gson();
String depts_json=gson.toJson(depts);
response.getWriter().print(depts_json);
}
}
EmpController 员工层面
import com.gl.ams.pojo.vo.Emp;
import com.gl.ams.service.EmpService;
import com.gl.ams.service.impl.EmpServiceImpl;
import com.gl.ams.servlet.BaseServlet;
import com.gl.ams.utils.JsonResult;
import com.gl.ams.utils.PageResult;
import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = "/admin/emp")
public class EmpController extends BaseServlet {
private EmpService empService =new EmpServiceImpl();
public void doadd(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
Map<String, String[]> map = request.getParameterMap();
Emp emp = new Emp();
BeanUtils.populate(emp,map);
int result = empService.addEmp(emp);
JsonResult jsonResult = new JsonResult();
jsonResult.setData(result);
if (result==0){
jsonResult.setErrorMessage("添加失败!");
jsonResult.setResponseCode(500);
}
if (result==-1){
jsonResult.setErrorMessage("员工编号重复,添加失败!");
jsonResult.setResponseCode(500);
}
response.getWriter().print(new Gson().toJson(jsonResult));
}
public void checkempid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
String empid=request.getParameter("empid");
Emp emp= empService.getEmpById(empid);
JsonResult jsonResult=new JsonResult();
if (emp==null){
jsonResult.setData(true);
}else{
jsonResult.setData(false);
jsonResult.setResponseCode(500);
jsonResult.setErrorMessage("员工编号重复,添加失败!");
}
response.getWriter().print(new Gson().toJson(jsonResult));
}
public void doedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
Map<String,String[]> map= request.getParameterMap();
Emp emp=new Emp();
BeanUtils.populate(emp,map);
int result= empService.editEmp(emp);
JsonResult jsonResult=new JsonResult();
jsonResult.setData(result);
if (result==0){
jsonResult.setErrorMessage("修改失败!");
jsonResult.setResponseCode(500);
}
response.getWriter().print(new Gson().toJson(jsonResult));
}
public void listpage(HttpServletRequest request,HttpServletResponse response) throws IOException {
int pageindex=1;
String pageindex_str=request.getParameter("pageindex");
if (pageindex_str!=null && !"".equals(pageindex_str)){
pageindex=Integer.parseInt(pageindex_str);
}
int pagesize=2;
String pagesize_str=request.getParameter("pagesize");
if (pagesize_str!=null && !"".equals(pagesize_str)){
pagesize=Integer.parseInt(pagesize_str);
}
List<Emp> emps = empService.getEmpByPage(pageindex, pagesize);
long pagecount=empService.getPageCount(pagesize);
PageResult pageResult=new PageResult();
pageResult.setPagecount(pagecount);
pageResult.setPageindex(pageindex);
pageResult.setPagesize(pagesize);
pageResult.setDatas(emps);
JsonResult jsonResult = new JsonResult();
jsonResult.setData(pageResult);
response.getWriter().print(new Gson().toJson(jsonResult));
}
public void getempbyid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
String empid=request.getParameter("id");
Emp emp= empService.getEmpById(empid);
JsonResult jsonResult=new JsonResult();
jsonResult.setData(emp);
response.getWriter().print(new Gson().toJson(jsonResult));
}
}
mapper 数据库持久层
interface 接口
AdminMapper 管理者接口数据库
import com.gl.ams.pojo.vo.Admin;
import java.sql.Connection;
import java.sql.SQLException;
public interface AdminMapper {
public Admin selectByUsername(Connection conn, String username)throws SQLException;
}
DeptMapper 部门接口数据库
import com.gl.ams.pojo.vo.Dept;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public interface DeptMapper {
public int insertDept(Connection conn, Dept dept)throws SQLException;
public int updateDept(Connection conn, Dept dept)throws SQLException;
public int deleteDeptById(Connection conn, Integer deptid)throws SQLException;
public Dept selectById(Connection conn, Integer deptid)throws SQLException;
public Dept selectByName(Connection conn, String deptname)throws SQLException;
public List<Dept> selectAll(Connection conn)throws SQLException;
}
EmpMapper 员工接口数据库
import com.gl.ams.pojo.vo.Emp;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public interface EmpMapper {
public long selectEmpCountByDeptId(Connection conn, Integer deptid) throws SQLException;
public int insertEmp(Connection conn, Emp emp)throws SQLException;
public int updateEmp(Connection conn,Emp emp)throws SQLException;
public int deleteEmpById(Connection conn,String empid)throws SQLException;
public Emp selectEmpById(Connection conn,String empid)throws SQLException;
public List<Emp> selectEmpByPage(Connection conn,int pageindex,int pagesize)throws SQLException;
public long selectEmpCount(Connection conn)throws SQLException;
}
impl 存放接口实现类
AdminMapperImpl 管理者操作数据库
import com.gl.ams.mapper.AdminMapper;
import com.gl.ams.pojo.vo.Admin;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.Connection;
import java.sql.SQLException;
public class AdminMapperImpl implements AdminMapper {
@Override
public Admin selectByUsername(Connection conn, String username) throws SQLException {
String sql="select * from admin where username=?";
QueryRunner queryRunner=new QueryRunner();
Admin admin= queryRunner.query(conn,sql,new BeanHandler<Admin>(Admin.class),username);
return admin;
}
}
DeptMapperImpl 部门操作数据库
import com.gl.ams.mapper.DeptMapper;
import com.gl.ams.pojo.vo.Dept;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class