티스토리 뷰

1. ORM : Object Relational Mapping Framework

- 간편한 트랜잭션( 작업단위를 모아둔 것) 과 일관된 설정으로 데이터베이스의 접근이 용이하다.

- 커넥션풀의 자동 지원으로 복잡한 설정이 따로 필요하지 않다.



2. MyBatis 연동하기


먼저 MyBatis 연동을 위한 Dependency를 추가한다. 

총 6개의 Dependency 를 추가한다.




Connection Pool을 지원하는 DataSource를 생성하기 위하여 rootContext.xml을 생성하여 아래의 코드를 추가한다.


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
    <!-- Transaction 설정하기 -->
    <!-- Transaction Manager 설정 (rollback, commit 수행)-->
    <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" />        
    </bean>
    
    <!-- Transaction 대상 설정 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="tx*" rollback-for="RuntimeException" />
            
            <!-- CRUD에 관한 method -->
            <!-- insert로 시작하는 것을 실행할 때 RuntimeException이 발생하면 rollback 한다. -->
            <tx:method name="insert*" rollback-for="RuntimeException" />
            <tx:method name="write*" rollback-for="RuntimeException" />
            <tx:method name="add*" rollback-for="RuntimeException" />
            <tx:method name="create*" rollback-for="RuntimeException" />
            <tx:method name="regist*" rollback-for="RuntimeException"/>
            <tx:method name="set*" rollback-for="RuntimeException"/>
            
            <tx:method name="update*" rollback-for="RuntimeException" />
            <tx:method name="modify*" rollback-for="RuntimeException" />
            <tx:method name="edit*" rollback-for="RuntimeException" />
            <tx:method name="change*" rollback-for="RuntimeException" />
            
            <tx:method name="delete*" rollback-for="RuntimeException" />
            <tx:method name="remove*" rollback-for="RuntimeException" />
            <tx:method name="terminate*" rollback-for="RuntimeException" />
            
            <!-- insert update delete 를 못한다. -->
            <tx:method name="read*" read-only="true" />
            <tx:method name="select*" read-only="true" />
            <tx:method name="get*" read-only="true" />
        </tx:attributes>
    </tx:advice>
        
    <aop:config>
    <!-- public | 모든 리턴타입 | com.ktds.jmj의 모든패키지의 | web 패키지 | 모든 클래스 | 모든 메소드 | 안의 것 전부 다-->
        <aop:pointcut expression="execution(public * com.ktds.jmj..web.*.*(..))" id="controllerTx" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="controllerTx" />
    </aop:config>
cs



이렇게 rootContext.xml 의 작성을 끝내고 DAO와 Impl를 생성한다.


ArticleDAOImpl.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ktds.jmj.dao.impl;
 
import org.mybatis.spring.support.SqlSessionDaoSupport;
 
import com.ktds.jmj.dao.ArticleDAO;
 
public class ArticleDAOImpl extends SqlSessionDaoSupport implements ArticleDAO {
    //extends  다음 implements 순서중요
    //ArticleDAOImpl 위에 커서 놓고 Ctrl+1 단축키
    
    //결과가 한개일 경우 selectOne
    @Override
    public String getNowSystemDate() {
        return getSqlSession().selectOne("ArticleDAO.getNowSystemDate");
    }
}
cs




그리고 articleContext.xml을 따로 생성하여 sqlSessionTemplate와 연결해준다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="articleDAO"
            class="com.ktds.jmj.dao.impl.ArticleDAOImpl" >
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate" />        
    </bean>
            
    <bean id="articleBiz"
            class="com.ktds.jmj.biz.impl.ArticleBizImpl" >
        <property name="articleDAO" ref="articleDAO" />        
    </bean>
            
</beans>
cs




그리고 이전에 했던 작업과 같이 Controller 에 DAO를 추가해주고, Bean DI를 추가한다.

마지막으로 Mapper과 Configration을 생성하고 설정해야 한다.

Mapper는 query를 쓰는 곳이다. test를 위해 SYSDATE를 출력해보았다.


1
2
3
4
5
6
7
8
9
10
11
<?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="ArticleDAO">
 
    <select id="getNowSystemDate" resultType="string">
        SELECT  SYSDATE
        FROM    DUAL        
    </select>
</mapper>
cs



Configration 은 mybatis.xml 의 이름으로 파일을 생성하여 아래의 코드를 추가한다.


1
2
3
4
5
6
7
8
9
10
11
<?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>
 
    <mappers>
        <mapper resource="/com/ktds/jmj/dao/impl/sql/articleDAO.xml" />
    </mappers>
    
</configuration>
 
cs


MyBatis의 연동 끝






rootContext.xml 의  Beans Graph






rootContext.xml의 코드


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
<?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:tx="http://www.springframework.org/schema/tx"
    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-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
    <!-- Connection Pool -->
    <!-- destroy-method="close" 는 사용하지 않는 클래스?를 닫아준다. -->
    <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:@localhost:1521:XE" />
        <property name="username" value="HR" />
        <property name="password" value="hr" />            
    </bean>
    
    <!-- MyBatis 가 사용할 Database에 연결하도록 설정 -->
    <bean id="sqlSessionFactory"
            class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource" />        
        <property name="configLocation" value="classPath:/mybatis.xml" />
    </bean>
    
    <!-- MyBatis의 CRUD 템플릿을 사용 할 수 있도록 설정 -->
    <bean id="sqlSessionTemplate"
            class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory" />
    </bean>
    
    <!-- Transaction 설정하기 -->
    <!-- Transaction Manager 설정 (rollback, commit 수행)-->
    <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" />        
    </bean>
    
    <!-- Transaction 대상 설정 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="tx*" rollback-for="RuntimeException" />
            
            <!-- CRUD에 관한 method -->
            <!-- insert로 시작하는 것을 실행할 때 RuntimeException이 발생하면 rollback 한다. -->
            <tx:method name="insert*" rollback-for="RuntimeException" />
            <tx:method name="write*" rollback-for="RuntimeException" />
            <tx:method name="add*" rollback-for="RuntimeException" />
            <tx:method name="create*" rollback-for="RuntimeException" />
            <tx:method name="regist*" rollback-for="RuntimeException"/>
            <tx:method name="set*" rollback-for="RuntimeException"/>
            
            <tx:method name="update*" rollback-for="RuntimeException" />
            <tx:method name="modify*" rollback-for="RuntimeException" />
            <tx:method name="edit*" rollback-for="RuntimeException" />
            <tx:method name="change*" rollback-for="RuntimeException" />
            
            <tx:method name="delete*" rollback-for="RuntimeException" />
            <tx:method name="remove*" rollback-for="RuntimeException" />
            <tx:method name="terminate*" rollback-for="RuntimeException" />
            
            <!-- insert update delete 를 못한다. -->
            <tx:method name="read*" read-only="true" />
            <tx:method name="select*" read-only="true" />
            <tx:method name="get*" read-only="true" />
        </tx:attributes>
    </tx:advice>
        
    <aop:config>
    <!-- public | 모든 리턴타입 | com.ktds.jmj의 모든패키지의 | web 패키지 | 모든 클래스 | 모든 메소드 | 안의 것 전부 다-->
        <aop:pointcut expression="execution(public * com.ktds.jmj..web.*.*(..))" id="controllerTx" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="controllerTx" />
    </aop:config>
    
</beans>
 
cs


'BackEnd > Spring' 카테고리의 다른 글

[Spring] Dynamic SQL  (0) 2016.04.20
[Spring] Mapper XML Files ( select / insert, update, delete / parameters )  (0) 2016.04.20
[Spring] 파일 업로드  (0) 2016.04.20
[Spring] Controller의 리턴 타입  (0) 2016.04.15
[Spring] 예외처리  (1) 2016.04.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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
글 보관함