一、需求说明
1.实体类:Department
2.数据库访问层:DepartmentDao/DepartmentDaoImpl
3.业务层:DepartmentService/DepartmentServiceImpl
4.Junit测试:
5.控制层:DepartmentServlet extends BaseServlet
6.视图层:system/deptadd.jsp
将HTML修改为jsp
二、数据库设计
创建员工表
##新建员工表
create table dept (
DEPTNO int(10) not null auto_increment,
DEPTNAME varchar(100) not null,
LOCATION varchar(100),
primary key(DEPTNO)
)
三、实体类设计
要继承Serializable,Comparable两个类
package com.facai.entity;
import java.io.Serializable;
public class Department implements Serializable,Comparable<Department>{
private int deptno;//部门编号
private String deptname;//部门名称
private String location;//所在地点
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Department [deptno=" + deptno + ", deptname=" + deptname
+ ", location=" + location + "]";
}
public Department(int deptno, String deptname, String location) {
super();
this.deptno = deptno;
this.deptname = deptname;
this.location = location;
}
public Department() {
super();
}
@Override
public int compareTo(Department other) {
// TODO Auto-generated method stub
return this.deptno-other.deptno;
}
}
四、数据库访问层
DepartmentDaoImpl
package com.facai.dao.impl;
import com.facai.dao.DepartmentDao;
import com.facai.entity.Department;
import com.facai.util.DBUtil;
public class DepartmentDaoImpl implements DepartmentDao {
//添加部门
@Override
public int save(Department dept) {
String sql="insert into dept values(?,?,?)";
Object[] param={dept.getDeptno(),dept.getDeptname(),dept.getLocation()};
return DBUtil.executeDML(sql, param);
}
}
其他类以此类推
五、新技能点
测试类使用注解测试:
加入@test标签后可右键点击类名选择测试
package com.facai.test;
import org.junit.Test;
import com.facai.entity.Department;
import com.facai.service.DepartmentService;
import com.facai.service.impl.DepartmentServiceImpl;
public class TestDepartService {
@Test
public void testAdd(){
DepartmentService deptService=new DepartmentServiceImpl();
Department dept =new Department(1,"总裁办", "502");
int n=deptService.add(dept);
System.out.println(n);
}
}
六、控制层
package com.facai.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.facai.entity.Department;
import com.facai.service.DepartmentService;
import com.facai.service.impl.DepartmentServiceImpl;
public class DepartmentServlet extends BaseServlet {
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()+"/deptList.html");
}else{
request.setAttribute("error", "添加失败");
//此时必须使用转发,因为要复用保存在request中的数据
request.getRequestDispatcher("/system/deptAdd.jsp").forward(request, response);
}
}
}
七、视图层
<%@ 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-size:16px;font-weight:bold;">${error}</span>
</body>
</html>