ssh(Spring+Spring mvc+hibernate)——EmpController

 /**  
* @Title: DeptController.java
* @Package org.action
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com  
* @date 2017-12-27 上午10:54:42
* @version V1.0  
*/
package org.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.entity.Dept;
import org.entity.Emp;
import org.service.IDeptService;
import org.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import sun.beans.editors.IntEditor;

import com.sun.beans.editors.DoubleEditor;
import com.sun.beans.editors.FloatEditor;
import com.sun.beans.editors.LongEditor;

 /**   
 *    
 * 项目名称:test_ssh_16qn3   
 * 类名称:DeptController   
 * 类描述:   
 * 创建人:Mu Xiongxiong  
 * 创建时间:2017-12-27 上午10:54:42   
 * 修改人:Mu Xiongxiong   
 * 修改时间:2017-12-27 上午10:54:42   
 * 修改备注:   
 * @version    
 *    
 */
@Controller
public class EmpController {

    /**
     * 自动注入Service层
     */
    @Autowired
    private IEmpService empService;

    @Autowired
    private IDeptService deptService;




    @InitBinder    
    public void initBinder(WebDataBinder binder) {    

        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); 
        binder.registerCustomEditor(int.class, new IntEditor());
        binder.registerCustomEditor(long.class, new LongEditor());  
        binder.registerCustomEditor(double.class, new DoubleEditor());  
        binder.registerCustomEditor(float.class, new FloatEditor());  
    }

    /**
     * 
    * @Description: 该方法的主要作用:显示所有的员工
    * @Title: showDept
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping(value="showEmp")
    public ModelAndView showDept(){
        ModelAndView modelAndViewContainer = new ModelAndView();
        List<Emp> empList = empService.queryAll();
        modelAndViewContainer.addObject("empList",empList);
        modelAndViewContainer.setViewName("showEmp");
        return modelAndViewContainer;
    }






    /*
    * @Description: 该方法的主要作用:跳转到添加员工页面
    * @Title: saveEntity
    * @param  @param dept
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping(value="upSave")
    public ModelAndView upSave(){
        ModelAndView modelAndView = new ModelAndView();
        List<Dept> deptList = deptService.queryAll();
        modelAndView.addObject("deptList", deptList);
        modelAndView.setViewName("saveEmp");
        return modelAndView;
    }


    /*
    * @Description: 该方法的主要作用:添加部门
    * @Title: saveEntity
    * @param  @param dept
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping(value="saveEmps")
    public ModelAndView saveEmp(Emp emp){
        emp.setEid(((Long)System.currentTimeMillis()).intValue());//插入编号
        //根据编号查询部门表
        Dept dept = deptService.queryById(emp.getDid());
        emp.setDept(dept);
        empService.saveEntity(emp);
        //跳转到控制器中的getDeptAll方法
        return new ModelAndView("redirect:/showEmp.do");
    }


    /**
     * 
    * @Description: 该方法的主要作用:根据编号查询部门信息
    * @Title: getDeptById
    * @param  @param id
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping(value="getEmpById")
    public ModelAndView getEmpById(int id){
        ModelAndView modelAndView = new ModelAndView();
        List<Dept> deptList = deptService.queryAll();
        modelAndView.addObject("emp",empService.queryById(id));
        modelAndView.addObject("deptList", deptList);
        modelAndView.setViewName("updateEmp");
        return modelAndView;
    }


    /**
     * 
    * @Description: 该方法的主要作用:修改部门信息
    * @Title: updateEntity
    * @param  @param dept
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping(value = "updateEmp")
    public ModelAndView updateEntity(Emp emp) {
        // 根据编号查询部门表
        Dept dept = deptService.queryById(emp.getDid());
        emp.setDept(dept);
        empService.updateEntity(emp);
        return new ModelAndView("redirect:/showEmp.do");
    }


    /**
     * 
    * @Description: 该方法的主要作用:删除部门
    * @Title: delEntity
    * @param  @param id
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping(value="delEmp")
    public ModelAndView delEmp(int id) {
        Emp emp = empService.queryById(id);
        empService.delEntity(emp);
        return new ModelAndView("redirect:/showEmp.do");

    }






}
原文地址:https://www.cnblogs.com/a1111/p/12816100.html