mybatis-context.xml
servlet-context.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>
<!-- db.properties -->
<properties resource="oracle.properties"/>
<!-- TypeAliases -->
<typeAliases>
<typeAlias alias="userVO" type="com.lec08.dao.BoardVO"/>
</typeAliases>
<!-- DBCP -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${lec.driver}"/>
<property name="url" value="${lec.url}"/>
<property name="username" value="${lec.username}"/>
<property name="password" value="${lec.userpw}"/>
</dataSource>
</environment>
</environments>
<!-- mapper -->
<mappers>
<mapper resource="board-map-lec08.xml"/>
<mapper resource="user-map-lec08.xml"/>
</mappers>
</configuration>
Spring
을 사용한 설정이 아닌, Mybatis
자체의 설정이다.src/main/resources
)에 존재하는 db.properties를 가져옵니다. mybatis-context.xml
또한 classpath의 같은 위치에 존재하기에 따로 경로를 설정해줄 필요가 없습니ㅏㄷ.map.xml
VO
에 바인딩한다. ( mapper 설정 )log4j.xml
oracle.properties
mybatis-context.xml
*-map.xml
*-map.xml
을 연결이렇게 함으로써
App과 SQL을 분리할 수 있습니다.
DAO Class를 없애고, 이를 Mapper I/F로 대체 후 SQL 관련 작업은 XML로 뺄 수 있습니다.
Service는 이렇게 I/F화된 Mapper들을 묶어 트랜잭션 단위로 사용할 수 있습니다.
mapper의 id 값을 Java Mapper I/F의 FQCN(Full Qualified Class Name)으로 주어, mapper와 자바 인터페이스를 1대1 매핑할 수 있습니다.
DataSource
를 통해 DBCP에 접근할 수 있습니다.
이 때, 이 DataSource
에 TransactionManager
가 붙어 트랜잭션 관리를 해줍니다.
DataSource에 MyBatis의 SqlSessionFactoryBean이 접근하여 SqlSession ( DB Connection )을 잡아옵니다.
SqlSessionFactory Bean 설정 XML 내용
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDS_CONSOL_PRINT"/>
<property name="mapperLocations" value="classpath:lec09-mappers/*-map.xml"/>
<property name="configLocation" value="classpath:lec09-mappers/mybatis-context.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
트랜잭션 적용은 모두 AOP를 통해 진행합니다.
어노테이션 기반으로 설정할 수도 있고, XML기반으로 설정할 수도 있습니다.
1) 어노테이션 기반 설정
<tx:annotation-driven transaction-manager="txManager"/>
@Transactional
이 붙은 클래스와 메서드를 대상으로 트랜잭션 커밋/롤백을 진행해줍니다.
여기서 tx
는 spring-tx
의존성의 설정이라 생각하면 됩니다.
2) XML 기반 설정
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="cut" expression="execution(* com.lec09..*Impl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut" />
</aop:config>
3) 스프링 부트에서는 @Configuration
을 통해 DataSource 빈 설정, 트랜잭션 설정을 해줄 수 있습니다.