トッカンソフトウェア

SpringFrameworkでMyBatisを使ってみる⑥(Javaアプリケーション)

今回はトランザクションをやります。

MyBatis⑤の続きなので、先にそちらで環境を構築して下さい。


単純なUPDATE(XMLで設定)

SpringTest.xml

				
<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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="org.postgresql.Driver" />
		<property name="url"
			value="jdbc:postgresql://localhost:5432/postgres" />
		<property name="username" value="postgres" />
		<property name="password" value="ps" />
	</bean>

	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations"
			value="C:\\eclipseee\\workspace\\springTest\\resource\\sql.xml" />
	</bean>

	<bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="spring.test.UserMapper" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>

	<bean id="helloWorld" class="spring.test.SpringBean">
		<constructor-arg ref="mapper" />
	</bean>

</beans>

			

sql.xml

				
<?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="spring.test.UserMapper">
	<select id="getUserList" resultType="spring.test.User">
		select id,name from m_user order by id
	</select>
	<update id="updateUser" parameterType="spring.test.User">
		UPDATE M_USER SET NAME = #{name}
		 WHERE ID = #{id}
	</update>
</mapper>


			

UserMapper.java

				
package spring.test;

import java.util.List;

public interface UserMapper {

	public List<User> getUserList();

	public void updateUser(User user);
}


			

SpringBean.java

				
package spring.test;

import java.util.List;

public class SpringBean {

	private final UserMapper userMapper;

	public SpringBean(UserMapper mp) {
		this.userMapper = mp;
	}

	public void update() {
		User user = new User();
		user.setId("test1");
		user.setName("更新前");
		userMapper.updateUser(user);
		System.out.println("更新");
	}

	public void show() {

		List<User> list = userMapper.getUserList();
		for (User user : list) {
			System.out.println(user.getId() + "-" + user.getName());
		}
	}
}

			
pom.xml、User.java、HelloWorldTest.java はMyBatis⑤を見て下さい。

トランザクションの処理と設定追加

Spring設定ファイルに tx:annotation-driven と transactionManager の設定を追加します。

SpringTest.xml

				
<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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="org.postgresql.Driver" />
		<property name="url"
			value="jdbc:postgresql://localhost:5432/postgres" />
		<property name="username" value="postgres" />
		<property name="password" value="ps" />
	</bean>

	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations"
			value="C:\\eclipseee\\workspace\\springTest\\resource\\sql.xml" />
	</bean>

	<bean id="mapper"
		class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface"
			value="spring.test.UserMapper" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>

	<bean id="helloWorld" class="spring.test.SpringBean">
		<constructor-arg ref="mapper" />
	</bean>

	<tx:annotation-driven />

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>

			
トランザクション制御をするメソッドに@Transactionalを付けます。

SpringBean.java

				
package spring.test;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

public class SpringBean {

	private final UserMapper userMapper;

	public SpringBean(UserMapper mp) {
		this.userMapper = mp;
	}

	@Transactional(rollbackFor = Exception.class)
	public void update() {
		User user = new User();
		user.setId("test1");
		user.setName("更新後");
		userMapper.updateUser(user);
		System.out.println("更新");

		// Exceptionが発生するとロールバックされる。通常に終わるとコミットされる。
		// String str = null;
		// System.out.println(str.length());
	}

	public void show() {

		List<User> list = userMapper.getUserList();
		for (User user : list) {
			System.out.println(user.getId() + "-" + user.getName());
		}
	}
}

			
トランザクション処理が行われない場合、こちらを確認して下さい。

ページのトップへ戻る
ウェア All Rights Reserved.