20240214 스프링2일차(어제복습)

2024. 2. 14. 12:292023.11.21-2024.05.31

1교시
new>스프링레거시프로젝트>스프링mvc프로젝트>도메인 주소 입력 org.mask.controller


기본세팅 : 버전바꾸기>자바, pom.xml

*pom.xml

<properties>
		<java-version>11</java-version>
		<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>

 

RELEASE 뭔지 찾아보기

 

 


*자바버전 바꾸기1*

프로젝트 클릭>Properties 들어가기>Project Facets> Java :11로 변경

 

 

 

*자바버전 바꾸기2*

Properties>Java Build Path> Libraries>Modulepath>Edit> JavaSE-11 변경

 

 

*필요한 것 넣어주기 (pdf 참고)

 

 

저장소 추가 pom.xml >자바 버전 바꾼 <properties>아래  <repositories></repositories> 넣어주기

	<repositories>
		<repository>
			<id>mvn2</id>
			<url>http://repo1.maven.org/maven2</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>egovframe</id>
			<url>http://www.egovframe.go.kr/maven</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

 

넣어주기.

 

 

 

 

mybatis - spring, mairadb,------.jar 추가 (메이븐 방식)

추가하고, pom.xml 오른쪽> 메이븐> 업데이트 프로젝트으로 업데이트 시켜주기

	<dependencies>
		<!-- spring jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<!-- 전자정부 페이징 -->
		<dependency>
			<groupId>egovframework.rte</groupId>
			<artifactId>egovframework.rte.ptl.mvc</artifactId>
			<version>3.8.0</version>
		</dependency>
		<!-- 파일업로드 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>
		<!-- mariadb-java-client -->
		<dependency>
			<groupId>org.mariadb.jdbc</groupId>
			<artifactId>mariadb-java-client</artifactId>
			<version>2.7.3</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.6</version>
		</dependency>
		<!-- mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
		<dependency>
			<groupId>com.zaxxer</groupId>
			<artifactId>HikariCP</artifactId>
			<version>5.1.0</version>
		</dependency>

 

 

pom 추가한 것까지 최종

<?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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.mask</groupId>
	<artifactId>controller</artifactId>
	<name>fed14</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>11</java-version>
		<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
	</properties>
	<repositories>
		<repository>
			<id>mvn2</id>
			<url>http://repo1.maven.org/maven2</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>egovframe</id>
			<url>http://www.egovframe.go.kr/maven</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	
	<dependencies>

		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.16.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.30</version>
			<scope>provided</scope>
		</dependency>

		<!-- spring jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<!-- 전자정부 페이징 -->
		<dependency>
			<groupId>egovframework.rte</groupId>
			<artifactId>egovframework.rte.ptl.mvc</artifactId>
			<version>3.8.0</version>
		</dependency>
		<!-- 파일업로드 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>
		<!-- mariadb-java-client -->
		<dependency>
			<groupId>org.mariadb.jdbc</groupId>
			<artifactId>mariadb-java-client</artifactId>
			<version>2.7.3</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.6</version>
		</dependency>
		<!-- mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
		<dependency>
			<groupId>com.zaxxer</groupId>
			<artifactId>HikariCP</artifactId>
			<version>5.1.0</version>
		</dependency>

		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>

		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>

		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<additionalProjectnatures>
						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
					</additionalProjectnatures>
					<additionalBuildcommands>
						<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
					</additionalBuildcommands>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>11</source>
					<target>11</target>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.2.1</version>
				<configuration>
					<mainClass>org.test.int1.Main</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 

 

 

 

web.xml > 기본설정 한번씩 보기

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>

 

 

/를 들어오는 모든걸 잡아요

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

 

 

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<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>
	
	<context:component-scan base-package="org.mask.*" />

 

<context:component-scan base-package="org.mask.controller" />

*로 변경

<context:component-scan base-package="org.mask.*" />

 

 

<context:component-scan base-package="org.mask.*" /> 이 줄 아래에

 

추가하고 Spring 패키지에 db-Context.xml 만들어주러가기
<beans:import resource="classpath:/spring/db-Context.xml"/>

 

*src/main/resources>spring 패키지 만들기

spring 패키지> New Spring Bean Definition file> db-context.xml 파일 만들기

 

아래 파일을 만들면 된다.

 

db-Context.xml 안에 DB객체 명, sqlSessionFactory명, sqlSession명 적어주기

(따로 설명은 안한다. pdf 파일 참고하면 된다. )

P타입 value

R타입.

 

*src/main/resources>mybatis 패키지 만들기>config, mappers 패키지 만들기

(mybatis.config, mybatis.mappers 이름으로 만들면 mybatis 아래 생성된다. )

 

 

https://mybatis.org/mybatis-3/ko/getting-started.html

 

mybatis – 마이바티스 3 | 시작하기

 

mybatis.org

 

mappers 패키지>board-Mapper.xml 만들고 가져오기

 

아래 불러오는 내용 적어주기

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
<mapper namespace="board">
	<select id="boardList" resultType="HashMap">
		SELECT * FROM boardview LIMIT 0,10
	</select>

</mapper>

 

mybatis-Config.xml 만들고 가져오기

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">

 

 

HomeController 삭제>BoardController 클래스 만들기

 

 

views>board.jsp 만들기

 

 

 

src/main/java 묶어서 만들어주기

 

 

 

이름에 맞게 어노테이션 넣어주기

service>@Service

package org.mask.service;

import org.springframework.stereotype.Service;

@Service
public class BoardService {

}

 

rpository>@ Rpository

package org.mask.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BoardDAO {

}

 

 

board에 필요한 것 만들어주기

 

BoardController.java

//데이터 흐름
//controller -> service -> repository -> mybatis ->DB

package org.mask.controller;

import java.util.List;
import java.util.Map;

import org.mask.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/*
 * 스프링에서 객체 생성하는 방법은
 * @Controller
 * @Service
 * @Repository
 * @Component
 * */

@Controller
public class BoardController {
	//데이터 흐름
	//controller -> service -> repository -> mybatis ->DB

	@Autowired
	   private BoardService boardService;

	   @GetMapping({ "/board", "/" }) // 요청 들어와도 board 로 간다
	   public String board(Model model) {
	      List<Map<String, Object>> list = boardService.boardList();
	      model.addAttribute("list", list);
	      return "board"; // /WEB-INF/views/board.jsp 열어줘
	   }

	}

 

 

BoardService.java

package org.mask.service;

import java.util.List;
import java.util.Map;

import org.mask.dao.BoardDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BoardService {
   
   @Autowired
   private BoardDAO boardDAO;
   
   public List<Map<String, Object>> boardList(){
      return boardDAO.boardList2();
   }

}

 

 

 

BoardDAO

package org.mask.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BoardDAO {

	@Autowired
	private SqlSession sqlSession;
	
	  public List<Map<String, Object>> boardList() {
	      return sqlSession.selectList("board.boardList"); // namespace, id 가져옴
	   }


	
	  public List<Map<String, Object>> boardList2() {
	      List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	      
	      Map<String, Object> map = new HashMap<String, Object>();
	      map.put("board_no", 1);
	      map.put("board_title", "첫번째 글");
	      map.put("board_write", "홍길동");
	      map.put("board_date", "24.02.01");
	      map.put("board_count", 1);
	      map.put("comment", 0);
	      list.add(map);

	      map = new HashMap<String, Object>();
	      map.put("board_no", 2);
	      map.put("board_title", "두번째 글");
	      map.put("board_write", "이길동");
	      map.put("board_date", "24.02.02");
	      map.put("board_count", 2);
	      map.put("comment", 1);
	      list.add(map);

	      map = new HashMap<String, Object>();
	      map.put("board_no", 3);
	      map.put("board_title", "세번째 글");
	      map.put("board_write", "정길동");
	      map.put("board_date", "24.02.03");
	      map.put("board_count", 3);
	      map.put("comment", 2);
	      list.add(map);

	      map = new HashMap<String, Object>();
	      map.put("board_no", 4);
	      map.put("board_title", "네번째 글");
	      map.put("board_write", "신길동");
	      map.put("board_date", "24.02.04");
	      map.put("board_count", 4);
	      map.put("comment", 3);
	      list.add(map);

	      map = new HashMap<String, Object>();
	      map.put("board_no", 5);
	      map.put("board_title", "다섯번째 글");
	      map.put("board_write", "김길동");
	      map.put("board_date", "24.02.05");
	      map.put("board_count", 5);
	      map.put("comment", 4);
	      list.add(map);

	      return list;
	   }
	}

 

 

 

 

 

board.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>
</head>
<body>
	<h1>게시판입니다. </h1>
	${list }
</body>
</html>

 

 

 

<%@ 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>
</head>
<body>
	<h1>게시판입니다. </h1>
	<table>
		<thead>
			<tr>
				<th>번호</th>
				<th>제목</th>
				<th>글쓴이</th>
				<th>날짜</th>
				<th>읽음</th>
			</tr>	
		</thead>
		<tbody>
			<c:forEach items="${list }" var="row">
			<tr>
				<td>${row.board_no }</td>
				<td>${row.board_title }</td>
				<td>${row.board_write }</td>
				<td>${row.board_date }</td>
				<td>${row.board_count }</td>
			</tr>
			</c:forEach>
</body>
</html>

 

 

 

 

BoardController.java

package org.mask.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.mask.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/*
 * 스프링에서 객체 생성하는 방법은
 * @Controller
 * @Service
 * @Repository
 * @Component
 * */

@Controller
public class BoardController {
	//데이터 흐름
	//controller -> service -> repository -> mybatis ->DB

	@Autowired
	   private BoardService boardService;

	   @GetMapping({ "/board", "/" }) // 요청 들어와도 board 로 간다
	   public String board(Model model) {
	      List<Map<String, Object>> list = boardService.boardList();
	      model.addAttribute("list", list);
	      return "board"; // /WEB-INF/views/board.jsp 열어줘
	   }
	   
	   @GetMapping("/detail")
	   public String detail(HttpServletRequest request, Model model) {
		   String no= request.getParameter("no");
		   System.out.println(no);
		   Map<String, Object> detail = boardService.detail(no);//지금은 스트링으로 다 쓰지만 변경할거다
		   model.addAttribute("detail", detail);
		   
		   return "detail";
	   }

	}