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";
}
Author And Source
이 문제에 관하여(Spring _jdbcTemplate_ insert,update,delete), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jominjun94/Spring-jdbcTemplate-insertupdatedelete저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)