spring ibatis 声明式事务

spring 配置文件 app-context.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsp="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsp:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


	<bean id="manuPlanNewDaoImpl" class="manuPlanNew.service.dao.ManuPlanDaoImpl"
		parent="daoTemplate.flm">
	</bean>


	<bean id="manuPlanNewServiceImpl" class="manuPlanNew.service.ManuPlanServiceImpl">
		<property name="manuPlanDao">
			<ref bean="manuPlanNewDaoImpl" />
		</property>
		<property name="txManager">
			<ref bean="txManager" />
		</property>


	</bean>
	<bean id="manuPlanNewAction" class="manuPlanNew.action.ManuPlanAction" scope="prototype">
		<property name="manuPlanService">
			<ref bean="manuPlanNewServiceImpl" />
		</property>
		    
	</bean>
	<bean id="manuPlanEntryNewAction" class="manuPlanNew.action.ManuPlanEntryAction" scope="prototype">
		<property name="manuPlanService">
			<ref bean="manuPlanNewServiceImpl" />
		</property>
		<property name="categoryService">    
            <ref bean="categoryServiceImpl"/>    
        </property> 
	</bean>


</beans>


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath*:**/*-context.xml"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="defaultAutoCommit" value="${defaultAutoCommit}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <property name="maxActive" value="${maxActive}"/>
        <property name="maxIdle" value="${maxIdle}"/>
        <property name="maxWait" value="${maxWait}"/>
    </bean>

    <bean id="sqlMapClient" class="common.ibatis.SimpleSqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="sqlMapLocations" value="classpath*:**/*sqlmap.xml"/>
	    <property name="statementCachingEnabled" value="true"/>
		<property name="cacheModelsEnabled" value="true"/>
		<property name="useStatementNamespaces" value="true"/>
	</bean>

	<bean id="sqlMapClientTemplate" class="common.ibatis.NewLifeSqlMapClientTemplate">
		<property name="outputLog" value="true"/>
		<property name="sqlMapClient" ref="sqlMapClient"/>
	</bean>

	<bean id="daoTemplate.flm" scope="prototype" abstract="true"
		class="org.springframework.orm.ibatis.support.SqlMapClientDaoSupport">
		<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/>
	</bean>
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource"/> 
	</bean>

	<alias name="daoTemplate.flm" alias="daoTemplate.com"/>
	<alias name="daoTemplate.flm" alias="daoTemplate.axs"/>
	<alias name="daoTemplate.flm" alias="daoTemplate.fop"/>
	<alias name="daoTemplate.flm" alias="daoTemplate.ext"/>
	<alias name="daoTemplate.flm" alias="daoTemplate.fli"/>

</beans>

service层 方法

public String insertManuPlan(ManuPlan manuPlan) {
		
		// 建立事务 
		DefaultTransactionDefinition def = new DefaultTransactionDefinition(); 
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); 
		def.setTimeout(12000); 
		String id=null;
		TransactionStatus status = txManager.getTransaction(def); 
		try {
			id=this.manuPlanDao.insertManuPlan(manuPlan);
		} catch(RuntimeException e) { 
			txManager.rollback(status); 
			throw e; 
			} 

			txManager.commit(status); 

		
		return id;
		
		
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值