스프링 수업(myBatis1)

거너거너·2021년 10월 28일
0

spring(학원)

목록 보기
2/16

myBatis1

(스프링DB연결 전 내용으로 인해서 중요치는 않음)

index 파일 실행

deptList.do(controller클레스)로 이동

@Autowired
private DeptService ds;	// bean 생성 불필요
	@RequestMapping("deptList.do")
	public String list(Model model) {
		List<Dept> list = ds.list();	// 서비스클래스로 이동
		model.addAttribute("list", list);
		return "deptList";
	}

서비스로 이동(DeptServiceImpl)

	@Autowired
	private DeptDao dd;	// dao객체

	public List<Dept> list() {
		return dd.list();	// dao객체로 이동
	}

DAO로 이동(DeptDaoImpl)

@Repository
public class DeptDaoImpl implements DeptDao {
	private static SqlSession session;	// SQL문을 실행시키는 메소드를 제공하는 인터페이스 생성
	static {	// DAO들어오면 자동으로 실행
	    try { 
	    	Reader reader = Resources.getResourceAsReader("configuration.xml");	// mybatis환경설정을 읽어온다.
		      SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
		      session = ssf.openSession(true);	// true = auto commit수행
		      reader.close(); 
		} catch (IOException e) {
		  System.out.println("read file error : "+e.getMessage());
		}
	}
	public List<Dept> list() {
		return session.selectList("deptns.list");	// deptns맵핑 되어있는 list 불러오기(Dept.xml)
	}

Dept.xml로 이동

	<!-- Use type aliases to avoid typing the full classname every time. -->
	<resultMap id="deptResult"    type="dept">	<!-- type= configuration.xml의 alias 이름 -->
		<result property="deptno" column="deptno" />	<!-- 컬럼명과  -->
		<result property="dname"  column="dname" />
		<result property="loc"	  column="loc" />
	</resultMap>
	
	<select id="list" resultMap="deptResult">	<!-- dto객체 생성 -->
	<!-- <select id="list" resultType="dept"> -->	<!-- dto객체 생성 / resultMap을 만들필요 없다. -->
		select * from dept order by deptno
	</select>

서비스-> 컨트롤러-> deptList.jsp로 이동

header.jsp = 공통된 내용(include를 이용하여 내용불러오기)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div class="container">
		<h2 class="text-primary">부서 목록</h2>
		<table class="table table-hover">
			<tr>
				<th>부서코드</th>
				<th>부서명</th>
				<th>근무지</th>
			</tr>
			<c:if test="${not empty list }">	<!-- 컨트롤러 name값(list) -->
				<c:forEach var="dept" items="${list}">
					<tr>
						<td>${dept.deptno }</td>
						<td><a href="deptView.do?deptno=${dept.deptno}"
							   class="btn btn-info"> ${dept.dname}</a></td>
						<td>${dept.loc }</td>
					</tr>
				</c:forEach>
			</c:if>
			<c:if test="${empty list }">
				<tr>
					<th colspan="3">데이터가 없습니다</th>
				</tr>
			</c:if>
		</table>
	</div>
</body>
</html>

컨트롤러 이동

	@RequestMapping("deptView.do")
	public String deptView(int deptno, Model model) {	// 부서번호 받기
		Dept dept = ds.select(deptno);	// 서비스로 이동
		model.addAttribute("dept", dept);
		return "deptView";
	}

서비스로 이동

	public Dept select(int deptno) {
		return dd.select(deptno);	// dao객체로 이동
	}

dao로 이동

	public Dept select(int deptno) {
		return session.selectOne("deptns.select",deptno);	// (select불러오기, deptno값 불러오기)	
	}

Dept.xml로 이동

	<select id="select" parameterType="int" resultType="dept">
		select * from dept where deptno=#{deptno}
	</select>

컨트롤러 -> deptView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<!-- deptList.do 요청한 결과 페이지(deptList.jsp)의 table태그만 불러옴 -->
<script type="text/javascript">
	$(function() {
		$('#list').load("deptList.do table");
	});
</script>
</head>
<body>
	<div class="container">
		<h2>부서 상세정보</h2>
		<table class="table table-bordered">
			<tr>
				<th>부서코드</th>
				<td>${dept.deptno}</td>
			</tr>
			<tr>
				<th>부서명</th>
				<td>${dept.dname }</td>
			</tr>
			<tr>
				<th>근무지</th>
				<td>${dept.loc }</td>
			</tr>
			<tr>
				<th colspan="2"><a href="deptList.do" class="btn btn-info">목록</a>
					<a href="deptUpdateForm.do?deptno=${dept.deptno}" class="btn btn-info">수정</a> 
					<a href="deptDelete.do?deptno=${dept.deptno}" class="btn btn-info">삭제</a></th>
			</tr>
		</table>
	</div>
	<div id="list"></div>	<!-- ajax기술 j쿼리 함수를 불러온다. -->
</body>
</html>

수정(deptUpdateForm.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div class="container" align="center">
		<h2 class="text-primary">부서정보 수정</h2>
		<form action="deptUpdate.do" method="post">
			<input type="hidden" name="deptno" value="${dept.deptno}">
			<table class="table table-striped">
				<tr>
					<th>부서코드</th>
					<td>${dept.deptno}</td>
				</tr>
				<tr>
					<th>부서명</th>
					<td><input type="text" name="dname" required="required"	value="${dept.dname }"></td>
				</tr>
				<tr>
					<th>근무지</th>
					<td><input type="text" name="loc" required="required" value="${dept.loc }"></td>
				</tr>
				<tr>
					<th colspan="2"><input type="submit" value="확인"></th>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>

컨트롤러(ModelAttribute사용/생략가능)

	@RequestMapping("deptUpdate.do")
	public String deptUpdate(Dept dept, Model model) {
		int result = ds.update(dept);
		model.addAttribute("result", result);
		return "deptUpdate";
	}

서비스

	public int update(Dept dept) {
		return dd.update(dept);
	}

dao

	public int update(Dept dept) {
		return session.update("deptns.update",dept);
	}

컨트롤러

	@RequestMapping("deptUpdate.do")
	public String deptUpdate(Dept dept, Model model) {
		int result = ds.update(dept);
		model.addAttribute("result", result);
		return "deptUpdate";
	}

deptUpdate.jsp(상세페이지 수정 후)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:if test="${result > 0 }">
		<script type="text/javascript">
			alert("수정 성공");
			location.href = "deptList.do";
		</script>
	</c:if>
	<c:if test="${result <= 0 }">
		<script type="text/javascript">
			alert("수정 실패");
			history.go(-1);
		</script>
	</c:if>
</body>
</html>

deptView.jsp(삭제)-> 컨트롤러

	@RequestMapping("deptDelete.do")
	public String deptDelete(int deptno, Model model) {
		int result = ds.delete(deptno);
		model.addAttribute("result", result);
		return "deptDelete";
	}

서비스

	public int delete(int deptno) {
		return dd.delete(deptno);
	}

dao

	public int delete(int deptno) {
		return session.delete("deptns.delete",deptno);
	}

deptDelete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:if test="${result > 0 }">
		<script type="text/javascript">
			alert("삭제 성공");
			location.href = "deptList.do";
		</script>
	</c:if>
	<c:if test="${result <= 0 }">
		<script type="text/javascript">
			alert("삭제 실패");
			history.go(-1);
		</script>
	</c:if>
</body>
</html>
profile
배움이 필요한 사람

0개의 댓글