728x90
개요
국비지원 교육을 받으며 배웠던 프로젝트 전 스프링 셋팅 최종
◆ 프로젝트 생성
1. Dynamic Web Projcet 생성
- Apache Tomcat v8.5
- Dynamic web doule version 3.1 (4 이상 선택시 톰캣 add 불가)
- Build path 추가
- src\main\java
- src\main\resources
- Content directory 이름 수정
- webapp
- Generate web.xml deployment descriptor 체크
- Finish
2. 프로젝트 확인
- JRE System Library [JavaSe-1.8 확인]
- 버전이 다를 경우
- Properties → Execution environment JavaSe - 1.8로 수정
- Markers에서 quick fix로 한 번 더 수정
- 버전이 다를 경우
◆ xml 설정
1. 메이븐 프로젝트로 변경
- 프로젝트 우클릭 → configure → convert to maven project
2. pom.xml 설정
- Spring Library 의존성 추가
- jstl 라이브러리 추가
- jdbc 라이브러리 추가
- Data Source 라이브러리 추가
<packaging> war </packaging>윗 부분은 프로젝트마다 변경되므로 <packaging>과 <build> 사이에 해당 코드를 추가한다.
<properties>
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
</properties>
<dependencies>
<!-- spring container(core) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Oracle JDBC -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
</dependencies>
* My Batis Setting
아래 코드를 추가한다.
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
* File Upload Setting
아래 코드를 추가한다.
<!-- common fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
* Ajax Setting
아래의 코드를 추가한다.
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
3. /webapp/WEB-INF/web.xml 설정
- DispatcherServlet 정의
- 한글처리
- Context Listener 등록
</welcome-file-list>이후에 해당 코드 추가
<!-- DispatChServlet Mapping : Front Controller 등록 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 한글처리 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Context Listener 등록 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
4. /webapp/WEB-INF/spring-servlet.xml 생성 후 설정
- Annotation, controller 설정
- html, css, js, img 파일 접근 처리
- view resolver 설정 (jstl 설치 후 설정해야 에러가 뜨지 않음.)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc">
<!-- annotation 설정을 하겠다. -->
<context:annotation-config />
<!-- com.javaex.controller 패키지 밑에 있는 클래스 중에 @Controller를 달고 있는 클래스의 객체를
생성 하겠다. (new 하겠다) -->
<context:component-scan
base-package="com.javaex.controller" />
<!-- 서블릿 컨테이너의 디폴트 서블릿 위임 핸들러 -->
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<!-- 기본 뷰 리졸버 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
</beans>
* File Upload Setting
아래 코드를 추가한다.
- Url 매핑은 경로에 맞춰 수정해주어야 한다.
<!-- 멀티파트 리졸버 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 최대업로드 가능한 바이트크기 -->
<property name="maxUploadSize" value="52428800" />
<!-- 디스크에 임시 파일을 생성하기 전에 메모리에 보관할수있는 최대 바이트 크기 -->
<property name="maxInMemorySize" value="52428800" />
<!-- defaultEncoding -->
<property name="defaultEncoding" value="utf-8" />
</bean>
<!-- url매핑 -->
<!-- mapping과 location은 경로에 맞춰 수정해주어야 한다. -->
<mvc:resources mapping="/upload/**" location="file:C:/javaStudy/upload/" />
* Ajax Setting
<!-- jackson -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
5. /webapp/WEB-INF/applicationContext.xml 생성 후 설정
- application Context 설정
- <context:component-scan base-package="com.javaex"> 패키지 위치 확인
- Oracle Data Source bean 등록
- user id, password 확인.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.javaex">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>
<!-- oracle datasource -->
<bean id="oracleDatasource"
class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL"
value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="user" value="webdb" />
<property name="password" value="webdb" />
<property name="connectionCachingEnabled" value="true" />
<qualifier value="main-db" />
</bean>
</beans>
* My Batis Setting
아래 코드를 추가한다.
<!-- MyBatis SqlSessionFactoryBean 설정-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oracleDatasource" />
<property name="configLocation" value="classpath:mybatis/configuration.xml" />
</bean>
<!-- MyBatis SqlSessionTemplate 설정-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
◆ My Batis
1. /Projcet/src/main/resources/mybatis/configuration.xml 생성
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
<mappers>
<mapper resource="mybatis/mappers/매퍼파일명.xml" />
</mappers>
</configuration>
2. /Project/src/main/resources/mybatis/mappers/매퍼파일명.xml 생성
<?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>
◆ 간단 붙여넣기
pom.xml (spring, mybatis, upload, json)
<packaging> war </packaging>윗 부분은 프로젝트마다 변경되므로 <packaging>과 <build> 사이에 해당 코드를 추가한다.
<properties>
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
</properties>
<dependencies>
<!-- spring container(core) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Oracle JDBC -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- common fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
web.xml
</welcome-file-list>, </web-app> 사이에 해당 코드 추가
<!-- DispatChServlet Mapping : Front Controller 등록 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 한글처리 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Context Listener 등록 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
spring-servlet.xml (upload, json)
/webapp/WEB-INF/spring-servlet.xml 생성
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc">
<!-- annotation 설정을 하겠다. -->
<context:annotation-config />
<!-- com.javaex.controller 패키지 밑에 있는 클래스 중에 @Controller를 달고 있는 클래스의 객체를
생성 하겠다. (new 하겠다) -->
<context:component-scan
base-package="com.javaex.controller" />
<context:component-scan
base-package="com.javaex.api.controller" />
<!-- 기본 뷰 리졸버 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<!-- 서블릿 컨테이너의 디폴트 서블릿 위임 핸들러 -->
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<!-- 멀티파트 리졸버 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 최대업로드 가능한 바이트크기 -->
<property name="maxUploadSize" value="52428800" />
<!-- 디스크에 임시 파일을 생성하기 전에 메모리에 보관할수있는 최대 바이트 크기 -->
<property name="maxInMemorySize" value="52428800" />
<!-- defaultEncoding -->
<property name="defaultEncoding" value="utf-8" />
</bean>
<!-- url매핑 -->
<mvc:resources mapping="/upload/**"
location="file:C:/javaStudy/upload/" />
<!-- jackson -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json; charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
applicationContext.xml(mybatis)
/webapp/WEB-INF/applicationContext.xml 생성
- application Context 설정
- <context:component-scan base-package="com.javaex"> 패키지 위치 확인
- Oracle Data Source bean 등록
- user id, password 확인.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.javaex">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>
<!-- oracle datasource -->
<bean id="oracleDatasource"
class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL"
value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="user" value="webdb" />
<property name="password" value="webdb" />
<property name="connectionCachingEnabled" value="true" />
<qualifier value="main-db" />
</bean>
<!-- MyBatis SqlSessionFactoryBean 설정 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oracleDatasource" />
<property name="configLocation"
value="classpath:mybatis/configuration.xml" />
</bean>
<!-- MyBatis SqlSessionTemplate 설정 -->
<bean id="sqlSession"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans>
MyBatis
1. /Projcet/src/main/resources/mybatis/configuration.xml 생성
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
<mappers>
<mapper resource="mybatis/mappers/매퍼파일명.xml" />
</mappers>
</configuration>
2. /Project/src/main/resources/mybatis/mappers/매퍼파일명.xml 생성
<?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>
반응형
'Server > Spring&Spring Boot' 카테고리의 다른 글
[토비의 스프링] 스프링이란? (0) | 2022.10.23 |
---|---|
[Spring] @PathVariable로 불러온 값을 JSP에서 사용하기 (0) | 2022.07.07 |
[Spring] 파라미터가 없을 때(null) @RequestParam 사용하는 법 (0) | 2022.06.22 |
[Spring] Path 표현식 (CSS 경로 등) (0) | 2022.06.21 |
[Spring] Mapper, Dao, Service, Controller Sequence (0) | 2022.06.20 |