spring使用编程的方式进行事物处理_普通方式和jdbctemplate方式

1

在 applicationContext.xml添加

<import resource="applicationContext-transaction.xml"/>	

2 添加applicatonContext-transaction.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:oxm="http://www.springframework.org/schema/oxm"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/oxm
    http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    >
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/wanju</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
	</bean>
	
	<bean id="bookDao" class="com.transaction.BookDao">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
		<property name="transactionManager">
			<ref bean="transactionManager"></ref>
		</property>
		
	</bean>
	
</beans>
3

写编程方式的程序

package com.transaction;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;


public class BookDao {

	private DataSource dataSource;
	private PlatformTransactionManager transactionManager;
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public void setManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	
	@SuppressWarnings("")
	public int create(String msg)
	{
		TransactionTemplate template = new TransactionTemplate(transactionManager);
		Object result = template.execute(new TransactionCallback() {

			@Override
			public Object doInTransaction(TransactionStatus arg0) {
				return null;
			}
			
		});
		return 0;
	}
	
	
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void test()
	{
		DriverManagerDataSource source = null;
	}
}

	public int createWithTemplate(String msg){


		DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
		TransactionStatus status = transactionManager.getTransaction(definition);
		try {
			JdbcTemplate template = new JdbcTemplate(dataSource);
			template.update("insert into tb_book (l_name,l_price) valuses ('abc',44.3");
			
		} catch (Exception e) {
			transactionManager.rollback(status);
		}
		finally{
//			transactionManager.commit(status);
		}
		return 0;
	}
	






4测试类

package com.transaction;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		BookDao bookDao = context.getBean("bookDao",BookDao.class);
		bookDao.create("hello");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值