考试内容及参考

样卷: 

考试内容及参考_第1张图片

开发环境:tomcat9.x.x

一、功能要求

举例:请编写一个程序,完成对员工的管理 ,实现员工列表显示、员工添加功能。

考试内容及参考_第2张图片

二、具体功能要求及推荐实现步骤

2.1.打开基础项目

解压javaweb-frame.rar,修改工程名:javaweb-frame-班级-学号(如:javaweb-frame-ali211-01) 

2.2.创建数据库employ,数据表:

员工信息表(employee)、部门信息表(department),设置主键自增,并添加不少于3条测试数据,表结构如下:

表名

employee员工信息表

列名

数据类型(精度范围)

空/非空

约束条件

注释

id

int

非空

PK

员工id

name

varchar(30)

非空

员工姓名

phone

varchar(30)

非空

手机号

department_id

Int

非空

部门id

表名

department部门信息表

列名

数据类型(精度范围)

空/非空

约束条件

注释

id

int

非空

PK

部门id

name

varchar(30)

非空

部门名称

2.3.创建实体类Employee、Department

根据业务提供需要的构造方法和setter/getter方法。

/**
 * 部门实体类
 * @author 林辛
 * 2021年12月13日
 */
public class Department {
	
	private Integer id;
	private String name;//部门名称
	
	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;
	}
	
	
}
/**
 * 员工实体类
 * @author 林辛
 * 2021年12月13日
 */
public class Employee {
	
	private Integer id;
	private String name;//员工姓名
	private String phone;//员工手机号码
	private Integer departmentId;//员工所属部门Id
	private String departmentName;//员工所属部门名称
//补充getter setter方法}

2.4.创建EmployeeDAO类

编写添加员工方法 , 获取员工列表方法

public class EmployeeDAO {
	// 添加员工方法
	public void addEmployee(Employee employee) throws SQLException {
		String sql = "insert into employee(name,phone,department_id) values(?,?,?)";
		QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
		int row = runner.update(sql, employee.getName(), employee.getPhone(), employee.getDepartmentId());
		if (row == 0) {
			throw new RuntimeException();
		}
	}

	// 获取员工列表
	public List findAllEmployees() throws SQLException {
		String sql="select e.*,d.name as departmentName "
				+ "from employee e left join department d "
				+ "on e.department_id = d.id "
				+ "order by e.id";
		QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
		//开启下划线->驼峰转换所用,使用commons-dbutils-1.7.jar时,可以转换
		//BeanProcessor bean = new GenerousBeanProcessor();
		//RowProcessor processor = new BasicRowProcessor(bean);
		//return runner.query(sql, new BeanListHandler(Employee.class,processor));
		return runner.query(sql, new BeanListHandler(Employee.class));
	}
}

考试内容及参考_第3张图片

2.5.创建DepartmentDAO

编写获取部门列表的方法。

public class DepartmentDAO {
	
	// 获取所有部门
	public List findAllDepartments() throws SQLException {
		String sql = "select * from department";
		QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
		return runner.query(sql, new BeanListHandler(Department.class));
	}
}

2.6.开发并配置相关Servlet(3个)

实现员工列表查询

/**
 * 处理"/findAllEmployees"请求
 * 通过EmployeeDAO查询出所有员工,并设置请求对象的属性,
 * 最后请求转发给employee_list.jsp
 */
@WebServlet("/findAllEmployees")
public class FindAllEmployeesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		EmployeeDAO dao = new EmployeeDAO();
		try {
			List allEmployees = dao.findAllEmployees();
			request.setAttribute("allEmployees", allEmployees);
			request.getRequestDispatcher("/employee_list.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

实现跳转至添加页面

/**
 * 处理"/toEmployeeAddJSP"请求
 * 先查询出所有的部门信息,然后设置request对象的属性,
 * 最后请求转发给employee_add.jsp
 */
@WebServlet("/toEmployeeAddJSP")
public class ToEmployeeAddJSPServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//查询出所有的部门
		DepartmentDAO dao = new DepartmentDAO();
		try {
			List allDepartments = dao.findAllDepartments();
			request.setAttribute("allDepartments", allDepartments);
			request.getRequestDispatcher("/employee_add.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

实现员工添加并跳转至列表界面

/**
 * 处理“/addEmployee”请求
 * 从request对象中获取新员工信息,
 * 通过BeanUtils工具把这些信息封装到员工对象,
 * 然后通过EmployeeDAO添加新员工到数据库,
 * 最后重定向到员工列表页面
 */
@WebServlet("/addEmployee")
public class AddEmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Employee employee = new Employee();
		try {
			BeanUtils.populate(employee, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		EmployeeDAO dao = new EmployeeDAO();
		try {
			dao.addEmployee(employee);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//request.getRequestDispatcher("/index").forward(request, response);
		response.sendRedirect(request.getContextPath() + "/findAllEmployees");
	}

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

}

2.7.开发员工列表页面employee_list.jsp

会使用到标签库,所以下面第三行引入标签库代码不要忘了

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




员工管理


	添加员工
	
			
id 姓名 电话 部门名称
${employee.id} ${employee.name } ${employee.phone} ${employee.departmentName}

2.8.员工添加页面(employee_add.jsp)

会使用到标签库,所以下面第三行引入标签库代码不要忘了

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




添加员工


添加员工

姓名:
手机号:
所属部门: ${department.name}

项目结构如下:

考试内容及参考_第4张图片

 sql:


CREATE DATABASE /*!32312 IF NOT EXISTS*/`employ` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;

USE `employ`;

/*Table structure for table `department` */

DROP TABLE IF EXISTS `department`;

CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

/*Data for the table `department` */

insert  into `department`(`id`,`name`) values (1,'人力资源部'),(2,'销售部'),(3,'生产部');

/*Table structure for table `employee` */

DROP TABLE IF EXISTS `employee`;

CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `phone` varchar(30) NOT NULL,
  `department_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

/*Data for the table `employee` */

insert  into `employee`(`id`,`name`,`phone`,`department_id`) values (1,'唐僧','135112345678',1),(2,'孙悟空','13645678932',2),(3,'猪八戒','13812345678',3);

你可能感兴趣的:(javaweb,javaweb)