반응형
* 모든 코드의 import 표기는 생략함
a. 부서 목록 페이지 요청하기
0. Controller.java
- localhost/dept/list를 요청했을 때
부서 목록 페이지가 표시되도록 컨트롤러/요청핸들러 메서드 정의* 우선 해당 페이지로 잘 이동되는지부터 확인하기
@Controller
@RequestMapping("/dept")
public class DeptController {
@GetMapping(path = "/list")
public String list() {
return "dept/list";
}
}
b. 부서 목록 정보 표시하기
- localhost/dept/list를 요청했을 때 부서 목록(데이터)이 표시되게 하기
1. vo class
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. Mapper interface
2) com.sample.mapper 패키지에 DeptMapper.java 정의하기
- DeptMapper.java에 모든 부서정보를 반환하는 추상메서드 정의하기
@Mapper
public interface DeptMapper {
List<Dept> getAllDepts();
}
3. XML
3) src/amin/resources/mybatis/mappers 폴더에 Depts.xml 정의하기
- Depts.xml에 매퍼인터페이스에 정의한 추상메서드와 매핑되는 SQL 구문 작성하기
<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. Service.java
4) com.sample.service 패키지에 HrService.java 정의하기
- HrService.java 클래스에 DeptMapper 인터페이스를 구현한 객체 의존성 주입받기
- HrService.java 클래스에 모든 부서정보를 반환하는 서비스 메서드 구현하기
@Service
public class HrService {
@Autowired
private DeptMapper deptMapper;
public List<Dept> getAllDepts() {
List<Dept> deptList = deptMapper.getAllDepts();
return deptList;
}
}
5. Controller.java
5) com.sample.controller 패키지의 DeptController 클래스 구현하기
- DeptController 클래스에 HrService 객체 의존성 주입받기
- localhost/dept/list 요청을 처리하는 요청핸들러 메서드에서 모든 부서정보를 조회해서 Model에 저장하기
@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. list.jsp
6) /WEB-INF/views/dept/list.jsp에서 부서목록정보 출력하기
- list.jsp에서 EL, JSTL을 이용해서 모든 부서목록정보 출력하기
<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>
< 해당 글은 velog에서 이전하며 옮겨온 글로, 가독성이 좋지 않을 수 있는 점 양해 부탁드립니다. >
🔗 velog 버전 보기 : https://velog.io/@ryuneng2/Spring-모든-데이터-조회-기본-순서
'BackEnd > Spring' 카테고리의 다른 글
[Spring] YAML 이란? (Feat. JSON, XML) (0) | 2025.01.17 |
---|---|
[Spring] 게시판 등록 폼 기본 순서 정리(Feat. Builder 메소드 체이닝) (0) | 2025.01.17 |
[Spring] mybatis 쿼리 캐싱 (<cache> 태그) (0) | 2025.01.17 |
[Spring] mybatis의 동적 쿼리 (0) | 2025.01.17 |
[Spring] Spring MVC의 주요 컴포넌트(Model, View 전달 과정) (0) | 2025.01.17 |