기록해! 정리해!

myBatis2 (2)- 상세보기 본문

SpringBoot

myBatis2 (2)- 상세보기

zsuling 2022. 10. 13. 11:18

1.ExamVO

package com.rubypaper.exam;

import lombok.Data;

@Data
public class ExamVO {

	private String sno;
	private String sname;
	private int kor;
	private int eng;
	private int math;
	private int hist;
}

2. ExamDao

package com.rubypaper.exam;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ExamDao {
	   void  insert (ExamVO vo);
	   void  update (ExamVO vo);
	   void  delete (ExamVO vo);
	   ExamVO getExam(ExamVO vo);
	   List<ExamVO> getExamList(ExamVO vo);
}

3. ExamService

package com.rubypaper.exam;

import java.util.List;

public interface ExamService {
	void  insert (ExamVO vo);
	   void  update (ExamVO vo);
	   void  delete (ExamVO vo);
	   ExamVO getExam(ExamVO vo);
	   List<ExamVO> getExamList(ExamVO vo);
	   
}

4.ExamServiceImpl

package com.rubypaper.exam;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExamServiceImpl implements ExamService {

	@Autowired
	private ExamDao  dao;
	
	@Override
	public void insert(ExamVO vo) {
		dao.insert(vo);
		
	}

	@Override
	public void update(ExamVO vo) {
		dao.update(vo);
		
	}

	@Override
	public void delete(ExamVO vo) {
		dao.delete(vo);
		
	}

	@Override
	public ExamVO getExam(ExamVO vo) {
		
		return dao.getExam(vo);
	}

	@Override
	public List<ExamVO> getExamList(ExamVO vo) {		
		return dao.getExamList(vo) ;
	}

}

5. ExamController

package com.rubypaper.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.rubypaper.exam.ExamServiceImpl;
import com.rubypaper.exam.ExamVO;

@Controller
public class ExamController {
	
	@Autowired   // 중요 - 클래스를 사용해야 한다.
 	private ExamServiceImpl  service;
    
	@RequestMapping("/form.do")
	String form(){	
		System.out.println("==> /form.do 확인");
		return "form";		
	}
	
	@RequestMapping("/formOk.do")
	String formOk(ExamVO vo){	
		service.insert(vo);
		return "redirect:getExamList.do";		
	}
	
	@RequestMapping("/getExamList.do")
	String getExamList(ExamVO vo , Model model ){	

		model.addAttribute("li", service.getExamList(vo));
		
		return "getExamList";		
	}
	
	@RequestMapping("/getExam.do")
	String getExam(ExamVO vo , Model model ){	
		model.addAttribute("m", service.getExam(vo));
		
		return "getExam";		
	}
}

6. form.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">
<h2> 성적 저장하기 </h2>

<form action="formOk.do">
<table border=1 >
<tr> <td > 번호 </td><td > <input type=text name="sno" id="sno" >  </td> </tr>
<tr> <td> 이름 </td> <td > <input type=text name=sname  id="sname"></td> </tr>
<tr> <td> 국어 </td><td > <input type=text  name=kor  id="kor">  </td> </tr>
<tr> <td> 영어 </td> <td > <input type=text name=eng  id="eng">  </td> </tr>
<tr> <td> 수학 </td><td > <input type=text  name=math  id="math"> </td> </tr>
<tr> <td> 역사 </td><td > <input type=text  name=hist  id="hist"> </td> </tr>
<tr> <td colspan=2  align="center">
     <input type=submit value="저장하기" >
     </td> 
</tr>

</table>
</form>

</div>
</body>
</html>

7. getExamList

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jstl/core_rt" 
          prefix="c" %>   

<%@ taglib prefix="fmt" 
     uri="http://java.sun.com/jsp/jstl/fmt"%>
     
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h2> 성적 목록 보기 ( <a href=index.html>처음으로</a>) </h2>
<table border=1 >
<tr align="center"> 
 <th > 순번 </th> <th > 번호 </th> <th> 이름 </th> <th> 국어 </th>
 <th> 영어 </th> <th> 수학 </th> <th> 역사 </th>
 <th> 합계 </th><th> 평균 </th><th> 평균1 </th>

</tr>
<c:forEach  items="${li}"  var="m"  varStatus="status" >

<c:set var="sum"  value="${m.kor + m.eng + m.math + m.hist}" />
<c:set var="avg"  value="${sum / 4}" />
<c:set var="ksum"  value="${ksum + m.kor}" />
<c:set var="esum"  value="${esum + m.eng}" />
<c:set var="msum"  value="${msum + m.math}" />
<c:set var="hsum"  value="${hsum + m.hist}" />
<c:set var="ssum"  value="${ssum + sum}" />
<c:set var="asum"  value="${asum + avg}" />

<tr align="center"> 
 <td> ${status.count} </td>
 <td> ${m.sno} </td>
 <td><a href=getExam.do?sno=${m.sno}> ${m.sname}</a> </td>
 <td> ${m.kor} </td>
 <td> ${m.eng} </td>
 <td> ${m.math} </td>
 <td> ${m.hist} </td>
 <td> ${sum} </td>
 <td> ${avg} </td>
 <td> <fmt:formatNumber value="${avg}" pattern=".0" />  </td>

 </tr>
</c:forEach>

<tr align="center"> 
 <td colspan=3 > 누적합 </td>

 <td> ${ksum} </td>
 <td> ${esum}  </td>
 <td> ${msum}  </td>
 <td> ${hsum}  </td>
 <td> ${ssum} </td>
 <td> ${asum} </td>
 <td> ${asum} </td>

 </tr>

</table>


</div>
</body>
</html>

8. getExam

<%@ 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">
<h2> 성적 상세보기 </h2>
<form action="updateOk.do">
<table border=1 >
<tr> <td > 번호 </td><td > <input type=text name="sno" id="sno"  value="${m.sno}" >  </td> </tr>
<tr> <td> 이름 </td> <td > <input type=text name=sname  id="sname" value="${m.sname}" ></td> </tr>
<tr> <td> 국어 </td><td > <input type=text  name=kor  id="kor" value="${m.kor}" >  </td> </tr>
<tr> <td> 영어 </td> <td > <input type=text name=eng  id="eng" value="${m.eng}" >  </td> </tr>
<tr> <td> 수학 </td><td > <input type=text  name=math  id="math" value="${m.math}" > </td> </tr>
<tr> <td> 역사 </td><td > <input type=text  name=hist  id="hist" value="${m.hist}" > </td> </tr>
<tr> <td> 합계 </td><td > ${m.kor + m.eng + m.math + m.hist} </td> </tr>
<tr> <td> 평균 </td><td > ${(m.kor + m.eng + m.math + m.hist) /4} </td> </tr>

<tr> <td colspan=2  align="center">
     <input type=button value="목록보기" onClick="listOk()" >
     <input type=submit value="수정하기" >
     <input type=button value="삭제하기"  onClick="delOk(${m.sno})" >
     </td> 
</tr>

</table>
</form>


</div>
</body>
</html>

9. Mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- 인터페이스 DAO 이름을 가지고 namespace 이름으로 지정한다.  --> 
<mapper namespace="com.rubypaper.exam.ExamDao">

<!-- id의 값은 ExamDao 인터페이스의 메소드 이름과 매핑된다.   -->
<insert id="insert"  parameterType="com.rubypaper.exam.ExamVO">
 <![CDATA[
   insert  into examtbl1013 (sno, sname, kor, eng, math, hist)
   values( #{sno}, #{sname}, #{kor}, #{eng}, #{math}, #{hist})
 ]]>
</insert>

<update id="update"  parameterType="com.rubypaper.exam.ExamVO">
 <![CDATA[
   update examtbl1013 
   set sname=#{sname}, kor=#{kor}, eng=#{eng}, math=#{math}, hist=#{hist} 
   where sno=#{sno}
 ]]>
</update>

<delete id="delete"  parameterType="com.rubypaper.exam.ExamVO">
 <![CDATA[
   delete  from examtbl1013 where sno=#{sno}
 ]]>
</delete>

<select id="getExam"  parameterType="com.rubypaper.exam.ExamVO"
                      resultType="com.rubypaper.exam.ExamVO" >
 <![CDATA[
   select  *  from examtbl1013 where sno=#{sno}
 ]]>
</select>


<select id="getExamList"  parameterType="com.rubypaper.exam.ExamVO"
                          resultType="com.rubypaper.exam.ExamVO" >
 <![CDATA[
   select  *  from examtbl1013
 ]]>
</select>

</mapper>

'SpringBoot' 카테고리의 다른 글

myBatis2 (4)- 자료실(사진)  (0) 2022.10.13
myBatis2 (3)- 검색창  (0) 2022.10.13
myBatis2 (1)  (0) 2022.10.13
myBatis  (0) 2022.10.11
다중 mapper 설정하기  (0) 2022.10.11
Comments