* 모든 코드의 import 표기는 생략함
- localhost/dept/list를 요청했을 때
부서 목록 페이지가 표시되도록 컨트롤러/요청핸들러 메서드 정의
* 우선 해당 페이지로 잘 이동되는지부터 확인하기
@Controller
@RequestMapping("/dept")
public class DeptController {
@GetMapping(path = "/list")
public String list() {
return "dept/list";
}
}
- localhost/dept/list를 요청했을 때 부서 목록(데이터)이 표시되게 하기
1) com.sample.vo 패키지에 Dept.java 정의하기
@NoArgsConstructor
@Getter
@Setter
@ToString
public class Dept {
private int no;
private String name;
private String tel;
private String fax;
@Builder
public Dept(int no, String name, String tel, String fax) {
super();
this.no = no;
this.name = name;
this.tel = tel;
this.fax = fax;
}
}
2) com.sample.mapper 패키지에 DeptMapper.java 정의하기
@Mapper
public interface DeptMapper {
List<Dept> getAllDepts();
}
3) src/amin/resources/mybatis/mappers 폴더에 Depts.xml 정의하기
<mapper namespace="com.sample.mapper.DeptMapper">
<select id="getAllDepts" resultType="com.sample.vo.Dept">
select
dept_no as no,
dept_name as name,
dept_tel as tel,
dept_fax as fax
from
shop_depts
</select>
</mapper>
4) com.sample.service 패키지에 HrService.java 정의하기
@Service
public class HrService {
@Autowired
private DeptMapper deptMapper;
public List<Dept> getAllDepts() {
List<Dept> deptList = deptMapper.getAllDepts();
return deptList;
}
}
5) com.sample.controller 패키지의 DeptController 클래스 구현하기
@Controller
@RequestMapping("/dept")
public class DeptController {
@Autowired
private HrService hrService;
@GetMapping(path = "/list")
public String list(Model model) {
model.addAttribute("deptList", hrService.getAllDepts());
return "dept/list";
}
}
6) /WEB-INF/views/dept/list.jsp에서 부서목록정보 출력하기
<body>
<%@ include file="../common/navbar.jsp" %>
<div class="container">
<div class="row mb-3">
<div class="col-12">
<h1 class="fs-3">부서관리 - 부서 목록 정보</h1>
<table class="table">
<colgroup>
<col width="15%">
<col width="35%">
<col width="25%">
<col width="25%">
</colgroup>
<thead>
<tr>
<th>부서번호</th>
<th>부서이름</th>
<th>전화번호</th>
<th>팩스번호</th>
</tr>
</thead>
<tbody>
<c:choose>
<c:when test="${empty deptList }">
<tr>
<td colspan="6" class="text-center">조회결과가 없습니다.</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach var="dept" items="${deptList }">
<tr>
<td>${dept.no }</td>
<td>${dept.name }</td>
<td>${dept.tel }</td>
<td>${dept.fax }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</tbody>
</table>
</div>
</div>
</div>
</body>