Spring 프로젝트 환경 설정
1. Spring framework 의존성 설정
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
2. POM의 프로퍼티 설정
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.2.5.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.sl4j-version>1.6.6</org.sl4j-version>
</properties>
3. Mysql Connect J 설정
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
4. try-with 구문 활용
try (Autoacloseable 인터페이스를 구현한 타입의 변수) {
… 작업하기.
} catch (Exception) {
… 예외처리하기.
}
5. JUnit 설정
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
6. Mybatis / Mybatis-spring / spring-jdbc / spring-test 설정하기.
<!— Mybatis —>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<!— Mybatis spring —>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.4</version>
</dependency>
<!— spring-jdbc —>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version> <!— 스프링의 버젼과 동일하게 맞춘다. —>
</dependency>
<!— spring-test —>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.5.RELEASE</version> <!— 스프링의 버젼과 동일하게 맞춘다. —>
</dependency>
7. 기본적인 root-context의 네임스페이스 설정하기
- aop
- beans
- context
- jdbc
- mybatis-spring
8. DataSource 설정하기.
<bean id=“dataSource” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=“driverClassName” value=“com.mysql.jdbc.Driver”/>
<property name=“url” value=“jdbc:mysql://127.0.0.1:3306/book_ex”/>
<property name=“username” value=“kido”/>
<property name=“password” value=“kids”/>
</bean>
9. Junit 테스트 (스프링 올려서 테스트하기)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations={“file:src/main/webapp/WEB-INF/spring/**/*.xml”})
10. Mybatis 연동10.1 root-context.xml 에 SqlSessionFactory 추가하기
< bean id=“sqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”>
<property name=“dataSource” ref=“dataSource”/>
<property name=“configLocation” value=“classpath:/mybatis-config.xml”/>
<property name=“mapperLocations” value=“class path:/mappers/**/*Mapper.xml”/>
</bean>
<!— SqlSession Template과 SqlSessionFactory 연결하기 —>
<bean id=“sqlSession” class=“org.mybatis.spring.SqlSessionTemplate” destroy-method=“clearCache” >
<constructor-arg name=“sqlSessonFactory” ref=“sqlSessionFactory”/>
</bean>
10.2 resources/mybatis-config.xml 추가하기
<?xml version=“1.0” encoding=“utf-8”?>
<!DOCTYPE configuration
<configuration>
</configuration>
11. Jackson Bind 연동
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
상기내용 추가후 : @ResponseBody 만 해주면 된다.
12. 자바 서블릿 API 추가. (서블릿 테스트를 하기 위해서 자동생성 버젼에서 3.1로 올려준다. )
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
13. Mybatis-log4j 설정하기.
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
<!— root context에 dataSource 수정하기 —>
<bean id=“dataSource” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=“driverClassName” value=“net.sf.log4jdbc.sql.jdbcapi.DriverSpy”/>
<property name=“url” value=“jdbc:log4jdbc:mysql://127.0.0.1:3306/book_ex”/>
<property name=“username” value=“kido”/>
<property name=“password” value=“kido”/>
</bean>
<!— log4jdbc.log4j2.properties 생성 —>
log4jdbc.spylogdelegator.name=“net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
<!— logback.xml 생성하기 —>
<?xml version=“1.0” encoding=“utf-8” ?>
<configuration>
<include resource=“org/springframework/boot/logging/logback/base.xml”/>
<logger name=“jdbc.sqlonly” level=“DEBUG”/>
<logger name=“jdbc.sqltiming” level=“INFO”/>
<logger name=“jdbc.audit” level=“WARN”/>
<logger name=“jdbc.resultset level=“ERROR”/>
<logger name=“jdbc.resultsettable” level=“ERROR”/>
<logger name=“jdbc.connection” level=“INFO”/>
</configuration>