1. 程式人生 > >Spring+mybatis事物提交

Spring+mybatis事物提交

整體結構

applicationContext.xml 配置檔案

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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-4.1.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.1.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

	<context:component-scan base-package="com.lanou.test.*"></context:component-scan>

	<!-- 資料連線池  dbcp連線池-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
		
	<!-- 不需要定義SqlSessionFactory,注意configLocation當您使用MapperFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 自動掃描doamin目錄, 省掉Configuration.xml裡的手工配置 -->
		<property name="typeAliasesPackage" value="com.lanou.test.domain" /><!-- 
			以類名當別名 -->
		<property name="dataSource" ref="dataSource" />
		<!-- <property name="configLocation" value="classpath:mybatis-config.xml" 
			/> -->
			<!-- 載入mapper.xml檔案 -->
		<property name="mapperLocations" value="classpath:com/lanou/test/mapper/*Mapper.xml" />
	</bean>	
	
	<!-- 掃描對映器 Mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- Mapper介面所在包名,Spring會自動查詢其下的Mapper  -->
		<!-- 代替mybatis中的載入類  <mappers> <mapper resource="com/lanou/test/mapper/BookMapper.xml"/> </mappers> -->
		<property name="basePackage" value="com.lanou.test.mapper" />
	<!-- 		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
	</bean>
	
	<!-- 事務處理 -->
	
	<!-- 事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 開啟事務註解 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

BookMapper


@Service
public class BookService {
	
	@Autowired
	private BookMapper bookMapper;
	@Transactional
	public void save(Book book,Book book1) {
		bookMapper.save(book1);
		bookMapper.save(book);
	}

}

BookMapper.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="com.lanou.test.mapper.BookMapper">
	<insert id="save" parameterType="Book">
		insert into book
			(bookid,bookname,bookprice) 
		values
			(#{bookid},#{bookname},#{bookprice})
	</insert>
</mapper>