기록해! 정리해!

myBatis 본문

SpringBoot

myBatis

zsuling 2022. 10. 11. 18:10

1.오라클 테이블 만들기

Create table examtbl1011 (
 sno char(5) not null primary key,
 sname nvarchar2(10),
 kor number(3),
 eng number(3),
 math number(3),
 hist number(3)
  );
  
insert into examtbl1011 values('10101','김학생', 75, 85, 90, 60);
insert into examtbl1011 values('10102','이학생', 70, 75, 70, 60);
insert into examtbl1011 values('10103','박학생', 70, 85, 80, 75);
insert into examtbl1011 values('10201','조학생', 90, 85, 100, 50);
insert into examtbl1011 values('10202','황학생', 65, 65, 60, 70);
insert into examtbl1011 values('10203','임학생', 45, 55, 50, 55);
insert into examtbl1011 values('10304','천학생', 70, 75, 85, 80);
insert into examtbl1011 values('10305','남학생', 100, 85, 90, 90);
insert into examtbl1011 values('10306','여학생', 80, 95, 90, 85);
insert into examtbl1011 values('10307','노학생', 35, 55, 70, 55);

select *  from examtbl1011;

2. MyBatisWarApplication (멀티패키지스캐닝설정)

package com.rubypaper;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages= {"com.rubypaper","com.ruby"})
public class MyBatisWarApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyBatisWarApplication.class, args);
	}

}

이때 com.ruby를 안쓸꺼지만 생성해줌

3. Board-Mapper.xml 에 CRUD 하기

<?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">
<mapper namespace="mapper.Board-Mapper">

	<insert id="insert" parameterType="com.rubypaper.board.BoardVo">
<![CDATA[
insert into examtbl1011(sno,sname,kor,eng,math,hist)
values(#{sno},#{sname},#{kor},#{eng},#{math},#{hist})
]]>
	</insert>

	<select id="select" parameterType="com.rubypaper.board.BoardVo"
	                    resultType="com.rubypaper.board.BoardVo">
<![CDATA[
select sno,sname,kor,eng,math,hist from examtbl1011
]]>
	</select>

</mapper>

4. 생성

* jsp 파일 다운받기

(view에 jsp파일 만드려니까 안다운받아서 없음 ... 이클립스마켓가서 다운받아주기...)

* pom.xml에 추가하기 (스프링부트는 jsp가 디폴트가 아니라서 따로 추가해줘야함)  --> Maven에서 확인가능 

			<!-- JSTL 의존성 추가 (1)  -->
	    <dependency>
	       <groupId>javax.servlet</groupId>
	       <artifactId>jstl</artifactId>
	    </dependency>
	    
	    <!-- JSP 컴파일을 위한 라이브러리 추가  (2) -->
	    <dependency>
	       <groupId>org.apache.tomcat.embed</groupId>
	       <artifactId>tomcat-embed-jasper</artifactId>
	    </dependency>

(전체)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.rubypaper</groupId>
	<artifactId>MyBatisWar</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>MyBatisWar</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.2.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.oracle.database.jdbc</groupId>
			<artifactId>ojdbc8</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		
		<!-- JSTL 의존성 추가 (1)  -->
	    <dependency>
	       <groupId>javax.servlet</groupId>
	       <artifactId>jstl</artifactId>
	    </dependency>
	    
	    <!-- JSP 컴파일을 위한 라이브러리 추가  (2) -->
	    <dependency>
	       <groupId>org.apache.tomcat.embed</groupId>
	       <artifactId>tomcat-embed-jasper</artifactId>
	    </dependency>
    
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

5. jsp

view에 만들어준다

- 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>
   
<br>
<div align="center">
<h2> 학생성적 저장하기   </h2>

<form action="formOk">
<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>

- list.jsp

JSP 사용할 수 있게 추가해줘야함

<%@ 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>
   
<br>
<div align="center">
<h2> 학생성적    </h2>

<table border=1 width=700>
<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> ${m.sname} </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>

6. 스프링부트에서는 BoardDaoImpl 가 필요없다. 삭제

 

7. Board-Mapper  namespace를 BoardDao로 수정

   이름과 인터페이스이름이 같아야한다.

8.

 

9. BoardController

return "form"; //form.jsp로 갈건데 설정에서 .jsp를 붙혀주기로 했기떄문에 form만 작성해도됨

package com.rubypaper.board;

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

@Controller
public class BoardController {
	
	@Autowired //인터페이스를 받아 올 수 없음
    BoardServiceImpl service;
	
	public BoardController(){
		System.out.println("===>BoardController실행");
	}
	
	@RequestMapping("/form.do")
	public String form() {
		System.out.println("===>/form.do");
		
		return "form"; //form.jsp로 갈건데 설정에서 .jsp를 붙혀주기로 했기떄문에 form만 작성해도됨
	}
	
	@RequestMapping("/formOk")
	public String formOk(BoardVo vo) {
		System.out.println("===>/formOk");
		service.insert(vo);
		return "redirect:list.do"; 
		
	}
	
	@RequestMapping("/list.do")
	public  String  list(Model model) {
		System.out.println("===> /list.do ");
		 model.addAttribute("li", service.select(null));
		return "list";		
	}
}

10. index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h2> Mybatis 프로젝트 </h2>

<h3> 게시판 만들기 </h3>

 <a href="list.do" > 목록보기 </a>

 <a href="form.do" > 저장하기 </a>
 
</div>
</body>
</html>

 

 

결과

'SpringBoot' 카테고리의 다른 글

myBatis2 (2)- 상세보기  (0) 2022.10.13
myBatis2 (1)  (0) 2022.10.13
다중 mapper 설정하기  (0) 2022.10.11
MyBatis 사용하기  (0) 2022.10.11
eclipse web developer tools 3.27 + MyBatis 다운  (0) 2022.10.11
Comments