'datasource'에 해당되는 글 1건

  1. 2017.02.21 :: 스프링 프로젝트 - 공지사항 Request 처리를 위한 환경설정
프로그래밍 2017. 2. 21. 00:59


Request 정보 처리를 위한 설정


사용자가 공지사항 등록화면에서 입력한 정보와 첨부파일 정보를 저장할 테이블을 생성하고 DB연동을 위한 Datasource 설정, Mybatis연동 설정, Transaction설정, 파일처리를 위한 multipartResolver 설정을 차례대로 설명하겠습니다.


1. Database 테이블 생성


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
34
35
36
37
38
39
40
41
42
43
44
/*
    공지사항 정보 저장
*/
CREATE TABLE SCOTT.TB_NOTICE(
NOTICE_ID       NUMBER(10),
NOTICE_TITLE    VARCHAR2(200),
NOTICE_CONTENT  VARCHAR2(4000),
DEL_YN          CHAR(1)
)
TABLESPACE USERS
STORAGE
(
    INITIAL 64K
    NEXT 1M
)
NOCOMPRESS;
 
/*
    공지사항 첨부파일 정보 저장
*/
CREATE TABLE SCOTT.TB_ATT_FILE(
NOTICE_ID           NUMBER(10),
FILE_ID             NUMBER(3),
ATT_FILE_SAVE_NM    VARCHAR2(200),
ATT_FILE_NM         VARCHAR2(4000),
ATT_FILE_SIZE       NUMBER(10),
DEL_YN              CHAR(1)
)
TABLESPACE USERS
STORAGE
(
    INITIAL 64K
    NEXT 1M
)
NOCOMPRESS;
 
ALTER TABLE SCOTT.TB_NOTICE
ADD CONSTRAINT PK_TB_NOTICE PRIMARY KEY (NOTICE_ID);
 
ALTER TABLE SCOTT.TB_ATT_FILE
ADD CONSTRAINT PK_TB_ATT_FILE PRIMARY KEY (NOTICE_ID, FILE_ID);
 
ALTER TABLE SCOTT.TB_ATT_FILE
ADD CONSTRAINT FK_TB_ATT_FILE FOREIGN KEY (NOTICE_ID) REFERENCES SCOTT.TB_NOTICE (NOTICE_ID);
cs



2. Datasource 및 Transaction 설정

 - root-context.xml 파일을 참조하여 네임스페이스와 설정 정보를 추가합니다.

 - oracle Driver를 찾지 못하는 error가 발생하면, 오라클 설치 폴더에서 ojdbc를 찾아 JAVA를 설치한 곳으로 복사하면 error가 해결됩니다.

=> 오라클홈 -> jdbc -> lib -> ojdbc6.jar 파일을

=> 자바설치폴더 -> jre -> lib -> ext 폴더로 복사


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    <!--  
        @Service, @Repository 스트레오타입 어노테이션이 선언된 클래스를 빈으로 등록한다.
    -->
     <context:component-scan base-package="com.devdic.board">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <context:annotation-config/>
    
    <!-- 
    datasource 설정에 필요한 라이브러리 추가를 위해 아래의 내용을 pom.xml에 추가필요
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.3</version>
    </dependency>
    -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin@127.0.0.1:1521:orcl" />
        <property name="username" value="SCOTT"/>
        <property name="password" value="SCOTT"/>
    </bean>
    
    <!-- Transaction 설정 -->
    <!-- 
    Transaction 설정에 필요한 라이브러리 추가를 위해 아래의 내용을 pom.xml에 추가
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
    </dependency>  
     -->
     <!-- dataSource를 활용하는 트랜잭션 매니저를 등록 -->
     <!-- dataSource 빈을 DataSourceTransactionManager 트랜잭션 매니저의 dataSource 속성에 지정  -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- AOP advisor 를 활용하여 트랜잭션을 처리함
        아래의 dependency를 pom.xml에 추가 
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
    -->
     <tx:advice id="txAdvice" transaction-manager="txManager">
       <tx:attributes>
       <!--
            모든 메소드에 대해서 트랜잭션을 REQUIRED 으로 지정하고
            Exception 발생 시 롤백 지정
        -->
       <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
       </tx:attributes>
    </tx:advice>
    <!-- 
    com.devdic.board 아래의 클래스 명이 ~~~ServiceImpl로 끝나는
    클래스 내의 모든 메소드는 트랜재션 처리가 된다.
     -->
    <aop:config><!-- execution(* *..service.impl.*ServiceImpl.*(..)) -->
        <aop:pointcut id="requiredTx" expression="execution(* com.devdic.board..*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" />
    </aop:config>
cs


3. myBatis 연동

 3.1  root-context.xml에 아래의 myBatis 연동 설정 정보 추가


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
<!--  Mybatis 연동
    pom.xml 추가
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.1</version>
    </dependency>
    
    마이바티스 스프링 연동 모듈
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.0</version>
    </dependency>
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- mybatis config파일 경로 지정 -->
        <property name="configLocation" value="/WEB-INF/config/mybatis-config.xml"/>
        
        <!-- SQL(mapper) 경로 지정 -->
        <property name="mapperLocations" value="classpath:sqlMap/**/*.xml"/>
    </bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
          <constructor-arg ref="sqlSessionFactory" />
    </bean>
cs


3.2 WEB-INF -> config(폴더생성) -> mybatis-config.xml(파일 추가)

 - mybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.2//EN" "HTTP://mybatis.org/dtd/mybatis-3-config.dtd">
 
<configuration>
    <settings>
        <setting name="cacheEnabled" value="false" />
        <setting name="useGeneratedKeys" value="true" />
        <setting name="defaultExecutorType" value="BATCH" />
        <setting name="jdbcTypeForNull" value="NULL" />
        <setting name="callSettersOnNulls" value="true" />
    </settings>
 
    <typeAliases>
        <typeAlias type="java.util.Map" alias="map"/>
    </typeAliases>
 
    <mappers>
    </mappers>
</configuration>
cs



4. 첨부파일 처리를 위한 multipartResolver 설정
  - root-context.xml 파일에 아래와 같이 추가합니다.
  - dependency 추가를 위한 정보는 주석부분을 참조하세요!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <!-- MultipartResolver -->
    <!-- 
        <dependency>
            <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
     -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   
<property name="maxUploadSize" value="50000000"></property>
    </bean>
 
cs


posted by 생각퍼즐
: