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

来源:互联网 发布:rete算法原理视频 编辑:程序博客网 时间:2024/06/07 08:16

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() {@Overridepublic 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");}}



原创粉丝点击