[Spring] 개발환경 + 환경설정

cateto·2021년 1월 5일
0

매일하는Spring

목록 보기
1/4

생각없이 스프링 사용한 지 그래도 4개월정도 된 것 같다! '스프링 공부를 좀 더 할걸'이라는 소리가 왜 나오는 지 이해가는 부분이 많아지고 있다! Step by Step 꼼꼼하게 설정해보자.

개발환경

  1. Window 10
  2. Eclipse IDE Enterprise ver. 2020-12
  3. Tomcat 7.x
  4. Maven 3.x
  5. Java 1.8
  6. Spring 4.x

1. web.xml 인코딩 설정하기

	<!-- encoding filter -->
	<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> 

2. servlet-context.xml에 추가

	<!-- root-context에서 제외한 컨트롤러는 서블릿 컨텍스트에 등록한다 -->
	<context:component-scan base-package="com.mboard.controller" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

3. root-context.xml에 추가

	<!-- root-context에서는 Controller를 제외한 나머지 자원을 제어할 수 있도록 범위를 지정해준다. -->
	<context:component-scan base-package="com">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	<!-- <context:annotation-config></context:annotation-config> 이미 컴포넌트 스캔에 포함되어있음 -->

4. mybatis, mysql 설정하기

pom.xml


		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</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.12</version>
			<scope>test</scope>
		</dependency>
		
		<!-- mysql / mybatis 관련 설정 -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>8.0.22</version>
		</dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>3.4.6</version>
		</dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>1.3.2</version>
		</dependency>
		        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

java-version 속성도 build path에 맞게 바꾸고,
spring-framework버전도 판단에 따라 바꿔줘야 함.

root-context.xml

	<bean id="dataSource"
	   class="org.springframework.jdbc.datasource.DriverManagerDataSource"
	   lazy-init="false">
	
	   <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
	   <property name="url" value="jdbc:mysql://[mysql주소]" />
	   <property name="username" value="[mysql계정]" />
	   <property name="password" value="[mysql비밀번호]" />
	</bean>   

"com.mysql.jdbc.Driver"은 deprecated됬다고 뜨므로, driverClassName은 위의 것으로 한다.

5. JUnit Test

package com.nboard.controller;

import java.sql.Connection;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;


@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {
		"file:src/main/webapp/WEB-INF/spring/**/*.xml"
})
public class DataSourceTest {
	
	@Inject
	private DataSource ds;
	
	@Test
	public void testConnection() throws Exception {
		try(Connection con = ds.getConnection()){
			System.out.println();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}

성공이 됬다면 root-context.xml에 다음을 추가


	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	   <property name="dataSource" ref="dataSource"></property>
	   <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
	</bean>	

그리고 type-alias 등을 관리하는 환경설정 파일을 해당 경로에 생성해준다.

mybatis-config.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>
 
</configuration>

그리고 다시 MyBatisTest

package com.nboard.controller;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {
		"file:src/main/webapp/WEB-INF/spring/**/*.xml"
})
public class MyBatisTest {
	
	@Inject
	private SqlSessionFactory factory;
	
	@Test
	public void testFactory() {
		System.out.println(factory);
	}
	
	@Test
	public void testSession() {
		try(SqlSession session = factory.openSession()) {
			System.out.println(session);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}

이제 성공했으면 개발해보자!

profile
Curious for Everything

0개의 댓글