ssm使用全注解实现增删改查案例——EmpController

package org.action;


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

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

/**
 * 
*    
* 项目名称:test_myabtis_zhujie   
* 类名称:EmpController   
* 类描述:   Emp表的控制器
* 创建人:Mu Xiongxiong  
* 创建时间:2018-3-16 下午4:23:20   
* 修改人:Mu Xiongxiong   
* 修改时间:2018-3-16 下午4:23:20   
* 修改备注:   
* @version    
*
 */
@Controller
public class EmpController {

    //初始化Emp表的Service
    @Autowired
    private IEmpService empService;
    //初始化Dept表的Service
    @Autowired
    private IDeptService deptService;

    /**
     * 
    * @Description: 该方法的主要作用:查询全部员工信息
    * @Title: findEmpAll
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping("findEmpAll")
    public ModelAndView findEmpAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("empList",empService.findEmpAll());
        modelAndView.setViewName("showEmp");
        return modelAndView;
    }

    /**
     * 
    * @Description: 该方法的主要作用:根据id查询信息
    * @Title: findEmpbyId
    * @param  @param id
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping("findEmpById")
    public ModelAndView findEmpbyId(int id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("emp",empService.selectByPrimaryKey(id));
        modelAndView.addObject("deptList",deptService.findDeptAll());
        modelAndView.setViewName("updateEmp");
        return modelAndView;
    }

    /**
     * 
    * @Description: 该方法的主要作用:修改员工信息
    * @Title: updateEmp
    * @param  @param emp
    * @param  @param date
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping("updateEmp")
    public ModelAndView updateEmp(Emp emp,String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
        Date date_strDate = format.parse(date); 
            emp.setEdate(date_strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        empService.updateByPrimaryKey(emp);
        return new ModelAndView("redirect:/findEmpAll.do");
    }

    /**
     * 
    * @Description: 该方法的主要作用:查询全部部门列表
    * @Title: preSaveEmp
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping("preSaveEmp")
    public ModelAndView preSaveEmp(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("deptList",deptService.findDeptAll());
        modelAndView.setViewName("saveEmp");
        return modelAndView;
    }

    /**
     * 
    * @Description: 该方法的主要作用:添加部门信息
    * @Title: saveEmp
    * @param  @param emp
    * @param  @param date
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping("saveEmps")
    public ModelAndView saveEmp(Emp emp,String date){
        int id = ((Long)(System.currentTimeMillis())).intValue();
        emp.setEid(id);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date edate;
        try {
            edate = format.parse(date);
            emp.setEdate(edate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        empService.insert(emp);
        return new ModelAndView("redirect:/findEmpAll.do");
    }

    /**
     * 
    * @Description: 该方法的主要作用:删除员工信息
    * @Title: delEmp
    * @param  @param eid
    * @param  @return 设定文件  
    * @return  返回类型:ModelAndView   
    * @throws
     */
    @RequestMapping("delEmp")
    public ModelAndView delEmp(int eid){
        empService.deleteByPrimaryKey(eid);
        return new ModelAndView("redirect:/findEmpAll.do");
    }

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