본문 바로가기
Tech/Web

[Spring] Spring 기본세팅 & Setting Test

by 소라소라잉 2019. 11. 7.

1. Spring Framework version 변경 -> 5.1.7

 

 

2. JDK version 변경 -> 1.8

 

 

3. Spring webmvc dependency 두개 복사 후 spring-test, spring-jdbc로 변경

 

 

4. JUnit version 변경 -> 4.12

 

 

5. lombok 라이브러리 추가(lombok maven)

 

 

6. Log4J Exclusion,Scope삭제, version 변경->1.2.17 & Maven UPDATE 

 

 

 

 

7. Loading Test 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.sora.loading;
 
import static org.junit.Assert.assertNotNull;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import lombok.extern.log4j.Log4j;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class LoadingTest {
    
    @Autowired
    private ApplicationContext ctx;    
    @Test
    public void testCtx() {
    log.info(ctx);
    assertNotNull(ctx);
    }    
}
 
cs

 

 

8. root-context.xml 수정 및 DB connection pool & myBatis Test 

(pom.xml에 mysql connector, mybatis, mybatis-spring, hikari cp dependency 추가) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName"
            value="com.mysql.cj.jdbc.Driver"></property>
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/bitdb?serverTimezone=Asia/Seoul&amp;useSSL=false"></property>
        <property name="username" value="bit04"></property>
        <property name="password" value="1234"></property>
    </bean>
 
 
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
        destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>
 
 
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.sora.loading;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
import org.junit.Test;
 
import lombok.extern.log4j.Log4j;
 
@Log4j
public class MySQLConnectTests {
 
    @Test
    public void testConnect() throws Exception {
        // Class.forName의 return type은 class
        // 어떤 class인줄 모르니 <?>
        // Class<?> clz = Class.forName("com.mysql.jdbc.Driver");
        // com.mysql.jdbc.Driver <= Old version.(deprecated임) 아래가 new type
        Class<?> clz = Class.forName("com.mysql.cj.jdbc.Driver");
        log.info(clz);
 
        // 위까지 오류없이 완료됐으면 JDBC Driver연결 테스트
        // 아래 코드는 에러 남.(구버전)
        // Connection con =
        // DriverManager.getConnection("jdbc:mysql://localhost:3306/bitdb","bit04","1234");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/bitdb?serverTimezone=Asia/Seoul&useSSL=false""bit04""1234");
        log.info(con);
        con.close();
 
    }    
}
 
cs

 

mybatis Test - Time Test (어노테이션 테스트, xml 테스트)

: root-context에 mapper bean 생성을 위한 코드 작성 

1
2
    <mybatis-spring:scan
        base-package="org.sora.mapper" />
cs

 

 

src/main/resources에 org package 생성 및 하위 폴더 생성(sora>mapper) 후

TimeMapper.xml 파일 생성

1
2
3
4
5
6
7
8
9
10
11
12
<?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="org.sora.mapper.TimeMapper">
 
<select id="getTime2" resultType="String">
select now()
</select>
</mapper>
 
cs

 

TimeMapper interface 

1
2
3
4
5
6
7
8
9
10
11
12
13
package org.sora.mapper;
 
import org.apache.ibatis.annotations.Select;
 
public interface TimeMapper {    
    // 1. for 어노테이션 테스트
    @Select("select now()")
    public String getTime();    
    
    // 2. for xml 테스트 
    public String getTime2();    
}
 
cs

 

 

Test Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package org.sora.mapper;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import lombok.extern.log4j.Log4j;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class TimeMapperTests {
    
    @Autowired
    private TimeMapper mapper;
    
    @Test
    public void testTime() {        
        log.info(mapper.getTime());
        log.info(mapper.getTime2());        
    }    
 
}
cs

 

끄적_spring_setting.txt
0.00MB
intelliJ_Spring_setting.txt
0.00MB

 

프로젝트1107.txt
0.00MB

'Tech > Web' 카테고리의 다른 글

MyBatis Mapper XML파일(ResultMap,Collection)  (0) 2019.11.18
Spring-Web-Security  (0) 2019.11.15
[Spring] Spring 구성요소와 동작원리  (0) 2019.11.07
[Spring] 끄적거리며 알아가는 Spring  (0) 2019.11.07
[Servlet] File upload  (0) 2019.10.24

댓글