서비스 패키지 생성
src/main/java > dao > DeptDAO java class 생성
// DeptDAO class 생성
public class DeptDAO {
private SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
src/main/java > service > DeptService java class 생성
// DeptService class 생성
public class DeptService {
private DeptDAO deptDao;
public DeptService(DeptDAO deptDao) {
this.deptDao = deptDao;
}
}
src/main/resources > config.spring.context > context-3-dao.xml
// bean생성
<bean id="deptDAO" class="dao.DeptDAO">
<property name="sqlSession" ref="sqlSession" />
</bean>
<!-- ref : sqlSession이라는 id를 가진 객체를 DAO에서 참조(사용)하겠다는 말 -->
src/main/resources > config.spring.context > context-4-service.xml
//bean 생성
<bean id="deptService" class="service.DeptService">
<constructor-arg ref="deptDAO" />
</bean>
<!-- deptDAO의 id를 가진 객체를 사용하겠다
*
setter인젝션이 아닌 생성자 인젝션을 사용한 것
setter 인젝션과 생성자 인젝션 중 편한 쪽으로 사용하면 된다
-->
파일 구동을 위한 세팅
src/main/java > vo > DeptVO.java
vo 패키지 생성 및 DeptVO 자바 클래스 생성
int deptno;
String dname;
String loc;
public DeptVO( ) {
}
public DeptVO(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
src/main/resources > config.mybatis.mapper > dept.xml
: sql문 작성 및 파일 관리 객체
mapper의 namespace 부분에 고유 이름을 붙여주어야 해서
공란이면 오류남
<select id="selectList" resultType="vo.DeptVO">
select * from dept
</select>
<!-- 방법의 차이인 것 list vs map 방식으로 select하겠다
result타입은 결과값이기 때문에 1) return값이 될 수도 있고 2) <> 자료형이 될 수도 있다
적용순서
dept.xml에서 sql문 작성
DAO에서 dept.xml에 작성되어 있는 sql문 적용
물음표 값을 #으로 받아온다
service 파일에서 DAO에 작성되어 있는 메소드를 끌어와서 return
-->
<select id="selectOne" resultType="java.util.HashMap" parameterType="int">
select * from dept where deptno=#{deptno}
</select>
<insert id="insert" parameterType="vo.DeptVO">
insert into dept values(#{deptno},#{dname},#{loc})
</insert>
<select id="maxNo" resultType="int">
select max(deptno) from dept
</select>
<update id="update" parameterType="vo.DeptVO">
update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
</update>
<delete id="delete" parameterType="int">
delete dept where deptno=#{deptno}
</delete>
src/main/java > dao > DeptDAO에 객체 생성
resultType에 따라서 메소드 반환형을 정해준 것
List / Map 반환하는 클래스를 만들었다
src/main/java > service > DeptService.java
// DeptService 작성
하나의 테이블마다 dao를 만드는 게 아니라
여러 개의 dao를 하나의 서비스로 묶어서 여러 개의 dao메소드를
한꺼번에 service 클래스에서 사용하기 위한 목적
-> DAO에 작성되어 있는 메소드 끌어와서 return
src/main/resources > config.mybatis > mybatis-config.xml
// sql문을 작성한 파일을 불러오는 코드
<mappers>
<mapper resource="config/mybatis/mapper/dept.xml"/>
<!-- <mapper resource="config/mybatis/mapper/board_select.xml" />
<mapper resource="config/mybatis/mapper/board_insert.xml" />
<mapper resource="config/mybatis/mapper/board_update.xml" />
<mapper resource="config/mybatis/mapper/board_delete.xml" /> -->
</mappers>
Controller로 구동 클래스 생성
src/main/java > common > ViewPath.java
: 경로 관리를 위한 자바 인터페이스 ViewPath 작성
public static final String DEPT = "/WEB-INF/views/dept/";
경로 변수 생성
src > main > webapp > WEB-INF > spring > appServlet > servlet-content.xml
ViewPath에서 경로 변수를 사용하므로
servlet-context.xml에서 이중으로 사용하면 안 된다
<!--
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
ViewPath에서 경로를 설정해 주어서
servlet-context.xml 에서 필요없어짐 (중복)
-> 필요없는 코드 삭제
-->
<beans:bean class="controller.DeptController">
<beans:constructor-arg ref="deptService"/>
</beans:bean>
<context:component-scan base-package="com.increpas.db" />
src/main/java > controller > DeptController.java
// 컨트롤러 클래스 작성
@Controller
public class DeptController {
private DeptService deptService;
public DeptController(DeptService deptService) {
this.deptService = deptService;
}
@RequestMapping({"/","/list"})
public String list (Model model) {
List<DeptVO> list = deptService.selectList();
model.addAttribute("list", list);
// 모델은 addattribute를 사용해 가져온다 request영역에 setting
// request와 model은 모두 parameter 영역
return ViewPath.DEPT + "list.jsp";
}
@RequestMapping("/insertForm")
public String insertForm() {
return ViewPath.DEPT + "insert.jsp";
}
// WEB-INF/views/dept/insert.jsp
@RequestMapping("/insert")
public String insert(DeptVO vo) {
boolean check = deptService.insert(vo);
if(check) {
return "redirect:/list";
}else {
return "redirect:/insertForm";
}
}
@RequestMapping("/updateForm")
public String update(Model model,int deptno) {
Map<String, Object> vo = deptService.selectOne(deptno);
model.addAttribute("vo", vo);
return ViewPath.DEPT + "update.jsp";
}
@RequestMapping("/update")
public String update(DeptVO vo) {
if(deptService.update(vo) != 0) {
return "redirect:/list";
}else {
return "redirect:/updateForm?deptno=" + vo.getDepno();
}
}
@RequestMapping("/delete")
public String delete(@RequestParam("deptno") int deptno) {
deptService.delete(deptno);
return "redirect:/list";
}
// @RequestMapping("/delete/{deptno}")
// public String delete(@PathVariable("deptno") int deptno) {
// deptService.delete(deptno);
//
// return "redirect:/list";
// }
// deptSerivce에서 만들어 놓은 dao 메소드들을 controller에서 사용하겠다
// controller에서 경로와 로직을 모두 작성해 넘긴다
// -> 로직페이지가 필요 없어짐
}
view page 생성
// insert.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<form action="/db/insert" method="post">
<table border="1">
<tr>
<th>Name</th>
<td>
<input type="text" name="dname">
</td>
</tr>
<tr>
<th>Loc</th>
<td>
<input type="text" name="loc">
</td>
</tr>
<tr>
<th align="right" colspan="2">
<input type="submit" value="추가">
<input type="button" value="리스트" onclick="location.href='/db/'">
</th>
</tr>
</table>
</form>
</div>
</body>
</html>
// update.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<form action="/db/update" method="post">
<input type="hidden" value="${vo.DEPTNO }" name="deptno">
<table border="1">
<tr>
<th>Name</th>
<td>
<input type="text" name="dname" value="${vo.DNAME }">
</td>
</tr>
<tr>
<th>Loc</th>
<td>
<input type="text" name="loc" value="${vo.LOC }">
</td>
</tr>
<tr>
<th align="right" colspan="2">
<input type="submit" value="수정">
<input type="button" value="리스트" onclick="location.href='/db/'">
</th>
</tr>
</table>
</form>
</div>
</body>
</html>
// list.jsp 생성
<%@ 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>
<script type="text/javascript">
function del(deptno) {
if(!confirm("삭제하시겠습니까?")){
return;
}
// location.href='/db/delete/' + deptno;
location.href='/db/delete?deptno=' + deptno;
}
</script>
</head>
<body>
<table border="1" align="center">
<tr>
<th>부서번호</th>
<th>부서명</th>
<th>위치</th>
<th align="center">
<button onclick="location.href='/db/insertForm'">입력</button>
</th>
</tr>
<c:forEach var="vo" items="${ list }">
<tr>
<td>${ vo.deptno }</td>
<td>${ vo.dname }</td>
<td>${ vo.loc }</td>
<td>
<button onclick="location.href='/db/updateForm?deptno=${vo.deptno}'">수정</button>
<button onclick="del(${vo.deptno})">삭제</button>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
'Backend > Spring' 카테고리의 다른 글
Spring Mybatis Example : Visit 샘플 예제 풀이 (0) | 2023.05.28 |
---|---|
Spring Mybatis 기본 세팅 1 DB, Driver 세팅 (0) | 2023.05.23 |
Spring 서버 구동 방식 (0) | 2023.05.23 |
Spring 한글 필터 설정 방법 (0) | 2023.05.23 |
Spring 기본 routine : logic Parameter 구동 방식 (0) | 2023.05.18 |