Spring _jdbcTemplate_ insert,update,delete

view 코드, service 코드는 따로 넣지 않았습니다.
dao - controller의 관계 확인 용도 - 의존성 주입

insert dao

@Override
public void insertEmp(EemployeeVO emp) {
	// TODO Auto-generated method stub
	String sql = "insert into employees(employee_id,"+ 
			"first_name, last_name,email,phone_number,"+
			"hire_date, job_id , salary , commission_pct,"+
			"manager_id, department_id)"+
			"vlaues(?,?,?,?,?,sysdate,?,?,?,?,?)";
	jdbcTemplate.update(sql,
			emp.getEmployeeId(),
			emp.getFirstName(),
			emp.getLastName(),
			emp.getEmail(),
			emp.getPhoneNumber(),
			emp.getJobId(),
			emp.getSalary(),
			emp.getCommissionPct(),
			emp.getMangerId(),
			emp.getDepartmentId());
			
}

insert controller

@RequestMapping(value = "hr/insert", method = RequestMethod.GET)
	public String insertEmp(@PathVariable int employeeId,Model model) {
		return "hr/insertform";
	}
@RequestMapping(value = "hr/insert", method = RequestMethod.POST)
	public String insertEmp(EemployeeVO emp,Model model) {
		empDao.insertEmp(emp);
		return "redirect:list";
	}

update dao

@Override
public void updateEmp(EemployeeVO emp) {
	// TODO Auto-generated method stub
	String sql = "update employees"
			+"set first_name=?, last_name=?, email=?,"
			+"phone_number=? , hire_date=?, job_id=?"
			+"salary=?, commission_pct=?, manager_id=?"
			+"department_id=? where employee_id=?";
	jdbcTemplate.update(sql,
			emp.getFirstName(),
			emp.getLastName(),
			emp.getEmail(),
			emp.getPhoneNumber(),
			emp.getJobId(),
			emp.getSalary(),
			emp.getCommissionPct(),
			emp.getMangerId(),
			emp.getDepartmentId());
}

update controller

@RequestMapping(value = "hr/update", method = RequestMethod.GET)
	public String updateEmp(int empId, Model model) {
		EemployeeVO emp = empDao.getEmplist(empId);
		model.addAttribute(emp);
		return "hr/updateForm";
	}
@RequestMapping(value = "hr/update", method = RequestMethod.POST)
	public String updateEmp(EemployeeVO emp , Model model) {
		empService.updateEmp(emp);
		return "redirect:list";
	}	
	    
    

delete dao

@Override
public void deleteEmp(int empId, String email) {
	String sql = "delete from employees where employee_id =? and email = ?";
	jdbcTemplate.update(sql,empId,email);
	//아이디랑 이메일을 같이 하는경우는 혹시몰라서 email 도 같이 받아줌 
	
}

delete controller

@RequestMapping(value = "hr/deleteForm")
	public void deleteEmp(int empId , Model model) {
		
        model.addAttribute("emp",empDao.getEmplist(empId));
	
  	  }
	
	
	
@RequestMapping(value = "hr/deleteForm", method = RequestMethod.POST)
	public String deleteEmp2(int empId, String email) {
		
	empDao.deleteEmp(empId, email);
	return "redirect:list";
		
	}

좋은 웹페이지 즐겨찾기