내용 |
기능 추가하기
- 4장의 예제에 추가하세요.
- 부서테이블(Departments)의 모든 정보를 출력하세요.
---------------- front end code ----------------------------
- 요청 URL은 /hr/dept/list 여야 합니다.
5- 컨트롤러 메서드 이름은 getAllDeptList 입니다.<-- EmpController에 추가하세요.
@Autowired
IDeptService deptService;
@RequestMapping("/hr/dept/list")
public String getAllDeptList(Model model) {
model.addAttribute("deptList", deptService.getAllDeptList());
return "hr/deptlist";
}
6- 뷰 이름은 hr/deptlist 입니다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>부서 정보</h1>
<table border=1>
<c:forEach var="dept" items="${deptList}">
<tr>
<td>${dept.departmentId}</td>
<td>${dept.departmentName}</td>
<td>${dept.managerId}</td>
<td>${dept.locationId}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
------------------backend code-------------------
1- Value Object 클래스는 DeptVO입니다.
변수 이름은 테이블의 열 이름을 참고하세요.
테이블의 열 이름은 department_id(숫자), department_name(문자), manager_id(숫자), location_id(숫자) 입니다.
package com.coderby.myapp.hr.model;
public class DeptVO {
private int departmentId;
private String departmentName;
private int managerId;
private int locationId;
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public int getManagerId() {
return managerId;
}
public void setManagerId(int managerId) {
this.managerId = managerId;
}
public int getLocationId() {
return locationId;
}
public void setLocationId(int locationId) {
this.locationId = locationId;
}
@Override
public String toString() {
return "DeptVO [departmentId=" + departmentId + ", departmentName=" + departmentName + ", managerId="
+ managerId + ", locationId=" + locationId + "]";
}
}
2- IDeptRepository 인터페이스와 DeptRepository 클래스를 구현하세요.
구현할 메서드 이름은 public List<DeptVO> getAllDeptList()입니다.
sql 구문은 select * from departments 입니다.
package com.coderby.myapp.hr.dao;
import java.util.List;
import com.coderby.myapp.hr.model.DeptVO;
public interface IDeptRepository {
List<DeptVO> getAllDeptList();
}
package com.coderby.myapp.hr.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.coderby.myapp.hr.model.DeptVO;
public class DeptRepository implements IDeptRepository {
JdbcTemplate jdbcTemplate;
@Override
public List<DeptVO> getAllDeptList() {
String sql = "select * from departments";
return jdbcTemplate.query(sql, new DeptMapper());
}
private class DeptMapper implements RowMapper<DeptVO> {
@Override
public DeptVO mapRow(ResultSet rs, int rowNum) throws SQLException {
DeptVO dept = new DeptVO();
dept.setDepartmentId(rs.getInt("department_id"));
dept.setDepartmentName(rs.getString("department_name"));
dept.setManagerId(rs.getInt("manager_id"));
dept.setLocationId(rs.getInt("location_id"));
return dept;
}
}// end DeptMapper
}
3- IDeptService 인터페이스와 DeptService 클래스를 구현하세요.
구현할 메서드 이름은 public List<DeptVO> getAllDeptList()입니다.
4- Repository와 Service 빈 생성 및 의존성 주입을 설정하세요.
- 컨트롤러와 서비스 의존성 주입 설정
<!-- DeptRepository 클래스에 setter 추가 -->
<bean id="deptRepository" class="com.coderby.myapp.hr.dao.DeptRepository">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- DeptService 클래스에 setter 추가 -->
<bean id="deptService" class="com.coderby.myapp.hr.service.DeptService">
<property name="deptRepository" ref="deptRepository"/>
</bean> |