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");}}
- spring使用编程的方式进行事物处理_普通方式和jdbctemplate方式
- Spring-事物的处理方式。。。
- Spring-事物的处理方式。。。
- Spring 使用注解方式进行事物管理
- Spring 使用注解方式进行事物管理
- Spring 使用注解方式进行事物管理
- Spring 使用注解方式进行事物管理
- Spring + Jta +JDBCTemplate 分布式事物实现方式
- spring的事物配置方式
- [spring]spring三种事物处理方式
- 使用spring的JdbcTemplate进行查询的三种回调方式的比较
- 使用spring的JdbcTemplate进行查询的三种回调方式的比较
- spring三种事物处理方式
- spring三种事物处理方式
- Spring 和JdbcTemplate 和 JdbcDaoSupport方式对比
- Spring 使用JdbcTemPlate的方式实现与数据库的连接
- Spring 注解方式使用声明式事物
- Spring访问数据库--DataSource方式和JdbcTemplate方式
- 贝叶斯方法
- 为什么static 成员函数不能为const
- C#字符串加密和解密
- spring整合hibernate的步骤详细
- POJ.1.多项式加法
- spring使用编程的方式进行事物处理_普通方式和jdbctemplate方式
- java随机数的使用
- 构建自定义组件
- Lenove G450 安装64位 UBuntu 后安装32位库
- c++多线程(一)
- KNN算法
- What do Java objects look like in memory during run-time?
- httpd: Could not open configuration file /xxx/conf/httpd.conf: No such file or directory
- 根据 字体来确定UILabel的行高和宽度