Spring+Mybatis+Servlet开发人员管理系统

三层架构

  • 持久层--Mybatis
  • 表现层--Servlet + Jsp
  • Spring--管理对象、切面处理

基于MVC模式

  • 视图--Jsp

  • 模型--JavaBean

  • 控制器--Servlet+JavaBean


    图片.png
  • 核心控制器
    1、Servlet对象由Web容器管理
    2、Service对象由IOC容器管理

图片.png
  • 持久层实现不用创建类,而是通过Mybatis配置文件实现的。
  • controller通过注解配置servlet

部门管理

步骤

  • 实体类
  • Dao接口与Mybatis配置文件
  • Service接口与其实现类
  • 控制器
  • 页面

日志模块

采用AOP切面处理

  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">



    <!--Spirng整合Mybatis-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///sm?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>

    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--配置后直接使用类型,不用写包的全路径-->
        <!--实体类可以用简称-->
        <property name="typeAliasesPackage" value="com.alan.entity"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.alan.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


    <!--声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--通知设置-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="search*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>


    <!--织入-->
    <aop:config >
        <!--切入点设置-->
        <aop:pointcut id="txPointcut" expression="execution(* com.alan.service.*.*(..))"></aop:pointcut>
        <!--切入点与通知进行关联-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor>
    </aop:config>


    <!--启动全局扫描-->
    <context:component-scan base-package="com.alan"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
  • mybatis 相关 xml
  • DepartmentDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.alan.dao.DepartmentDao">
<!--这里可以不写全路径,应为在spring中已经配置了-->
<resultMap id="resultMap" type="Department">
    <!--主键的话用id标签-->
    <id property="id" column="id" javaType="Integer"></id>
    <!--其他的用result标签-->
    <result property="name" column="name" javaType="String"></result>
    <result property="address" column="address" javaType="String"></result>
</resultMap>

<!--设置参数和自增-->
<insert id="insert" parameterType="Department" useGeneratedKeys="true">
        insert into department(name,address) values (#{name},#{address})
    </insert>

<delete id="delete" parameterType="Integer">
        delete from department where id = #{id}
    </delete>

<update id="update" parameterType="Department">
        update department set name=#{name},address=#{address} where id = #{id}
    </update>

<select id="selectById" parameterType="Integer" resultMap="resultMap">
        select * from department where id = #{id}
    </select>

<select id="selectAll" resultMap="resultMap">
        select * from department
    </select>
</mapper>
  • LogDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.alan.dao.LogDao">
<!--这里可以不写全路径,应为在spring中已经配置了-->
<resultMap id="resultMap" type="Log">
    <!--其他的用result标签-->
    <result property="oprTime" column="opr_time" javaType="java.util.Date"></result>
    <result property="type" column="type" javaType="String"></result>
    <result property="operator" column="operator" javaType="String"></result>
    <result property="moudle" column="moudle" javaType="String"></result>
    <result property="operation" column="operation" javaType="String"></result>
    <result property="result" column="result" javaType="String"></result>
</resultMap>


    <insert id="insert" parameterType="Log" >
        insert into log(opr_time,type,operator,moudle,operation,result)
        values(#{oprTime},#{type},#{operator},#{moudle},#{operation},#{result})
    </insert>

    <select id="selectByType" parameterType="String" resultMap="resultMap">
        select * from log where type=#{type} order by opr_time desc
    </select>
</mapper>
  • SelfDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.alan.dao.SelfDao">

    <select id="selectByAccount" parameterType="String" resultMap="com.alan.dao.StaffDao.resultMap">
        select * from staff where account=#{account}
    </select>

</mapper>
  • StaffDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.alan.dao.StaffDao">
<!--这里可以不写全路径,应为在spring中已经配置了-->
<resultMap id="resultMap" type="Staff">
    <!--主键的话用id标签-->
    <id property="id" column="id" javaType="Integer"></id>
    <!--其他的用result标签-->
    <result property="account" column="account" javaType="String"></result>
    <result property="password" column="password" javaType="String"></result>
    <result property="status" column="status" javaType="String"></result>
    <result property="did" column="did" javaType="Integer"></result>
    <result property="name" column="name" javaType="String"></result>
    <result property="sex" column="sex" javaType="String"></result>
    <result property="idNumber" column="id_number" javaType="String"></result>
    <result property="workTime" column="work_time" javaType="Date"></result>
    <result property="leaveTime" column="leave_time" javaType="Date"></result>
    <result property="bornDate" column="born_date" javaType="Date"></result>
    <result property="info" column="info" javaType="String"></result>
    <!--配置多对一关联,通过部门映射表中的select语句进行查询-->
    <association property="department" column="did" javaType="Department" select="com.alan.dao.DepartmentDao.selectById" ></association>

</resultMap>

    <insert id="insert" parameterType="Staff" useGeneratedKeys="true">
        insert into staff(account,password,status,did,name,sex,id_number,work_time,leave_time,born_date,info)
        values(#{account},#{password},#{status},#{did},#{name},#{sex},#{idNumber},#{workTime},#{leaveTime},#{bornDate},#{info})
    </insert>
    <delete id="delete" parameterType="Integer">
        delete from staff where id=#{id}
    </delete>
    <update id="update" parameterType="Staff">
        update staff set account=#{account},password=#{password},status=#{status},
        did=#{did},name=#{name},sex=#{sex},id_number=#{idNumber},
        work_time=#{workTime},leave_time=#{leaveTime},born_date=#{bornDate},info=#{info} where id=#{id}
    </update>
    <select id="selectById" parameterType="Integer" resultMap="resultMap">
        select * from staff where id=#{id}
    </select>
    <select id="selectAll" resultMap="resultMap">
        select * from staff
    </select>

</mapper>
  • DispatcherServlet 中央控制器
package com.alan.global;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DispatcherServlet extends GenericServlet {

    private  ApplicationContext applicationContext ;


    public void init() throws ServletException {
        applicationContext = new ClassPathXmlApplicationContext("spring.xml");
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        /**
         * staff/add.do   login.do
         * staffController
         */


        //或者请求的url路径,将最前面的/截取掉
        String path  =  request.getServletPath().substring(1);
        String beanName ;
        String methodName;
        int index = path.indexOf('/');
        //包含反斜杠
        if(index != -1){
            beanName= path.substring(0,index)+"Controller"; //staffController
            methodName = path.substring(index+1,path.indexOf(".do"));
        }else {
            beanName = "selfController"; //只有特殊的这一个
            methodName = path.substring(0,path.indexOf(".do"));
        }

        System.out.println("path:"+path+",beanName:"+beanName+",methodName"+ methodName);

        Object object = applicationContext.getBean(beanName);
        try {
            Method method = object.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(object,request,response);
        }catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.getTargetException();
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }


    }


}
  • 登陆过滤器
package com.alan.global;

import com.alan.entity.Staff;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登陆过滤器
 */
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String path =  request.getServletPath();
        if(path.toLowerCase().indexOf("login") != -1){
            //包含login的url,直接放行。不进行过滤处理
            chain.doFilter(request,response);
        }else {
            Staff staff = (Staff) request.getSession().getAttribute("USER");
            if(staff != null){
                //session中有员工信息,直接放行,不进行过滤处理
                chain.doFilter(request,response);
            }else {
                //强制跳转回登陆界面
                response.sendRedirect(request.getContextPath()+"/toLogin.do");
            }
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
  • AOP切面类
package com.alan.global;


import com.alan.entity.Log;
import com.alan.entity.Staff;
import com.alan.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 记录日志的切面类
 */
//@Component泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
@Component
//aspect切面注解
@Aspect
public class LogAdvice {

    @Autowired
    private LogService logService;

    //配置运行日志,屏蔽掉SelfController,同时屏蔽to开头的例如toAdd等
    @After("execution(* com.alan.controller.*.*(..)) &&!execution(* com.alan.controller.SelfController.*(..)) && !execution(* com.alan.controller.*.to*(..))")
    public void operationLog(JoinPoint joinPoint){
        Log log = new Log();
        //模块名称赋值为类名
        log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
        //当前操作赋值为方法名
        log.setOperation(joinPoint.getSignature().getName());
        //当前方法的第一个参数为request
        HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
        Staff staff = (Staff) request.getSession().getAttribute("USER");
        //设置操作人
        log.setOperator(staff.getAccount());
        log.setResult("成功");
        logService.addOperationLog(log);

    }

    //配置系统日志,只有出现异常后进行记录,配置throwing
    @AfterThrowing(throwing = "e" ,pointcut = "execution(* com.alan.controller.*.*(..)) && !execution(* com.alan.controller.SelfController.*(..))")
    public void systemLog(JoinPoint joinPoint ,Throwable e){

        Log log = new Log();
        //模块名称赋值为类名
        log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
        //当前操作赋值为方法名
        log.setOperation(joinPoint.getSignature().getName());
        //当前方法的第一个参数为request
        HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
        Staff staff = (Staff) request.getSession().getAttribute("USER");
        log.setOperator(staff.getAccount());
        //将异常对象的名称传入到LOG中
        log.setResult(e.getClass().getSimpleName());
        logService.addSystemLog(log);
    }


    //配置登陆日志
    @After("execution(* com.alan.controller.SelfController.login(..))")
    public void loginLog(JoinPoint joinPoint){
        log(joinPoint);
    }


    //配置退出日志,采用前置通知
    @Before("execution(* com.alan.controller.SelfController.logout(..))")
    public void logoutLog(JoinPoint joinPoint){

        log(joinPoint);
    }


    private void log(JoinPoint joinPoint){

        Log log = new Log();
        //模块名称赋值为类名
        log.setMoudle(joinPoint.getTarget().getClass().getSimpleName());
        //当前操作赋值为方法名
        log.setOperation(joinPoint.getSignature().getName());
        //当前方法的第一个参数为request
        HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];
        Staff staff = (Staff) request.getSession().getAttribute("USER");
        if(staff == null){
            log.setOperator(request.getParameter("account"));
            log.setResult("失败");
        }else {
            log.setOperator(staff.getAccount());
            log.setResult("成功");
        }
        logService.addLoginLog(log);
    }
}
  • DepartmentController
package com.alan.controller;


import com.alan.entity.Department;
import com.alan.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("departmentController")
public class DepartmentController extends HttpServlet {


    @Autowired
    private DepartmentService departmentService;

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> list = departmentService.getAll();
        //  Department department = departmentService.get(1);
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../department_list.jsp").forward(request,response);
    }


    public void toAdd(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

        request.getRequestDispatcher("../department_add.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

        //从request获取参数值
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        //将值复制给Department对象
        Department department = new Department();
        department.setName(name);
        department.setAddress(address);
        //插入表中
        departmentService.add(department);
        //返回list.do,不需要传值,使用重定向
        response.sendRedirect("list.do");

    }


    public void toEdit(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

        String id = request.getParameter("id");
        Department department  = new Department();
        //根据id进行查询
        department = departmentService.get(Integer.parseInt(id));
        //将Department对象回传到更改页面
        request.setAttribute("OBJ",department);

        request.getRequestDispatcher("../department_edit.jsp").forward(request,response);
    }


    public void edit(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

        //从request获取参数值
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        //将值复制给Department对象
        Department department = new Department();
        department.setName(name);
        department.setAddress(address);
        department.setId(Integer.parseInt(id));
        //插入表中
        departmentService.edit(department);
        //返回list.do,不需要传值,使用重定向
        response.sendRedirect("list.do");

    }


    public void remove(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

        String id = request.getParameter("id");
        departmentService.remove(Integer.parseInt(id));
        response.sendRedirect("list.do");

    }

    }
  • LogController
package com.alan.controller;


import com.alan.entity.Log;
import com.alan.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("logController")

public class LogController {

    @Autowired
    private LogService logService;

    public void operationLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Log> list = logService.getOperationLog();
        request.setAttribute("LIST",list);
        request.setAttribute("TYPE","操作");
        request.getRequestDispatcher("../log_list.jsp").forward(request,response);
    }


    public void loginLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Log> list = logService.getLoginLog();
        request.setAttribute("LIST",list);
        request.setAttribute("TYPE","登陆");
        request.getRequestDispatcher("../log_list.jsp").forward(request,response);
    }


    public void systemLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Log> list = logService.getSystemLog();
        request.setAttribute("LIST",list);
        request.setAttribute("TYPE","系统");
        request.getRequestDispatcher("../log_list.jsp").forward(request,response);
    }
}
  • SelfController
package com.alan.controller;


import com.alan.entity.Staff;
import com.alan.service.SelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

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

@Controller("selfController")
public class SelfController {


    //依赖注入对象
    @Autowired
    private SelfService selfService;


    //跳转到登陆页面  jsp里面应该是toLogin.do
    public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("login.jsp").forward(request,response);
    }

    //登陆流程 login.do
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //从页面获取用户名和密码
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        //用户校验
        Staff staff = selfService.login(account,password);
        if(staff == null){
            //密码错误,重定向到登陆页面。一般一些异常可能使用到重定向。url发生改变。
            response.sendRedirect("toLogin.do");
        }else {
            request.getSession().setAttribute("USER",staff);
            response.sendRedirect("main.do");
        }

    }

        //打开主页面 main.do
    public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("index.jsp").forward(request,response);
    }


    //退出 logout.do
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //将session中的USER置空
        request.getSession().setAttribute("USER",null);
        response.sendRedirect("toLogin.do");
    }

    // 显示个人信息 /self/info.do
    public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("../info.jsp").forward(request,response);
    }

    //打开修改密码界面 /self/toChange.do
    public void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../change_password.jsp").forward(request,response);
    }

    //修改密码 /self/changePassword.do
    public void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");

        Staff staff = (Staff) request.getSession().getAttribute("USER");
        if(!staff.getPassword().equals(password)){
            response.sendRedirect("toChangePassword.do");
        }else {
            selfService.changePassword(staff.getId(),password1);
           // response.sendRedirect("../logout.do");
            //<script type = "text/javascript">parent.location.href="../logout.do"</script>
            response.getWriter().print("<script type = \"text/javascript\">parent.location.href=\"../logout.do\"</script>");
        }


    }
}
  • StaffController
package com.alan.controller;

import com.alan.entity.Department;
import com.alan.entity.Department;
import com.alan.entity.Staff;
import com.alan.service.DepartmentService;
import com.alan.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller("staffController")
public class StaffController {
    @Autowired
    private StaffService staffService;
    @Autowired
    private DepartmentService departmentService;

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Staff> list = staffService.getAll();
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../staff_list.jsp").forward(request,response);
    }

    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> list = departmentService.getAll();
        request.setAttribute("DLIST",list);
        request.getRequestDispatcher("../staff_add.jsp").forward(request,response);
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String idNumber = request.getParameter("idNumber");
        String info =request.getParameter("info");
        Date bornDate=null;
        try {
            bornDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bornDate"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer did = Integer.parseInt(request.getParameter("did"));

        Staff staff = new Staff();
        staff.setInfo(info);
        staff.setBornDate(bornDate);
        staff.setIdNumber(idNumber);
        staff.setDid(did);
        staff.setAccount(account);
        staff.setName(name);
        staff.setSex(sex);

        staffService.add(staff);
        response.sendRedirect("list.do");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //编辑界面需要员工信息和部门信息
        Integer id = Integer.parseInt(request.getParameter("id"));
        Staff staff = staffService.get(id);
        request.setAttribute("OBJ",staff);
        List<Department> list = departmentService.getAll();
        request.setAttribute("DLIST",list);
        request.getRequestDispatcher("../staff_edit.jsp").forward(request,response);
    }
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String account = request.getParameter("account");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String idNumber = request.getParameter("idNumber");
        String info =request.getParameter("info");
        Date bornDate=null;
        try {
            bornDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bornDate"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer did = Integer.parseInt(request.getParameter("did"));

        Staff staff = staffService.get(id);
        staff.setInfo(info);
        staff.setBornDate(bornDate);
        staff.setIdNumber(idNumber);
        staff.setDid(did);
        staff.setAccount(account);
        staff.setName(name);
        staff.setSex(sex);

        staffService.edit(staff);
        response.sendRedirect("list.do");
    }
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        staffService.remove(id);
        response.sendRedirect("list.do");
    }

    public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Staff staff = staffService.get(id);
        request.setAttribute("OBJ",staff);
        request.getRequestDispatcher("../staff_detail.jsp").forward(request,response);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容