티스토리 뷰

728x90
반응형

개발 환경

java 21.0.4 2024-07-16 LTSJava(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)

Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)

 

Tomcatv 10.1

EclipseVersion: 2024-06 (4.32.0)Build id: 20240606-1231


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://jakarta.ee/xml/ns/jakartaee"
	xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
	id="WebApp_ID" version="5.0">
	<display-name>com.mvc</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>default.htm</welcome-file>
	</welcome-file-list>
	
	<!-- 스프링의 ContextLoaderListener 등록 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 스프링 설정 파일 (web-applicationContext.xml)의 위치 설정 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-applicationContext.xml</param-value>
    </context-param>
</web-app>

MyBatis 설정

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           https://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context 
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc 
           https://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd">
           
	<!-- MyBatis DataSource -->
    <bean id="dataSourcForMyBatis" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
        <property name="driver" value="oracle.jdbc.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521/xe" />
        <property name="username" value="system" />
        <property name="password" value="1234" />
    </bean>
    
    <!-- MyBatis SqlSessionFactoryBean -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourcForMyBatis" />
        <property name="configLocation" value="classpath:mybatis/model/modelConfig.xml" />
        <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml" />
    </bean>

    <!-- MyBatis SqlSessionTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactoryBean" />
    </bean>
    
     <!-- MyBatis Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourcForMyBatis" />
    </bean>

    <!-- MyBatis Enable Transaction Management -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
	<!-- 빈 정의 -->
	<bean id="memberDAOImp" class="com.mvc.member.MemberDAOImp">
		<constructor-arg name="dataSource" ref="dataSource" />
		<constructor-arg name="sqlSessionTemplate" ref="sqlSessionTemplate" />
	</bean>
		
</beans>

 

tx namespace 관련 파일 설정

 

<beans 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd">

트랜잭션 설정

"@Transactional(propagation=Propagation.REQUIRED)" 클래스(class)에 트랜잭션(transaction)을 적용한 예제

package com.mvc.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED)
public class MemberDAOImp implements MemberDAO {
	private Connection connection;
	private PreparedStatement preparedStatement;
	
	private final DataSource dataSource;
	private final SqlSessionTemplate sqlSessionTemplate;
	
	public MemberDAOImp(DataSource dataSource, SqlSessionTemplate sqlSessionTemplate) {
		this.dataSource = dataSource;
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	@Override
	public void updateBalance1() {
		this.sqlSessionTemplate.update("mapper.member.updateBalance1");
		this.sqlSessionTemplate.update("mapper.member.updateBalance2");
	}
}
728x90
반응형