纯JDBC操作, 对某些项目来说, 也许更好, Spring JDBC Framework让你不用关心Connection, Statement, ResultSet.

定义数据源
spring事务编程的例子<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
spring事务编程的例子 <property name="jndiName">
spring事务编程的例子 <value>java:/comp/env/jdbc/oracle</value>
spring事务编程的例子 </property>
spring事务编程的例子</bean> 定义事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> 定义dao
spring事务编程的例子<bean id="customerDAO" class="com.waterye.dao.impl.CustomerDAOImpl">
spring事务编程的例子 <property name="dataSource" ref="dataSource" />
spring事务编程的例子 <property name="transactionManager" ref="transactionManager" />
spring事务编程的例子</bean> public class CustomerDaoImpl extends JdbcDaoSupport implements CustomerDAO {
private DataSource dataSource;
pirvate TransactionManager transactionManager; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} public void setTransactionManager(DataSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
} public Map get(Integer id) throws Exception {
String querySql = "select * from customer where id = ?"; return getJdbcTemplate().queryForMap(querySql, new Object[] { id });
} public void insert(final Map customer) throws Exception {
String seqSql = "select customer_seql.nextval from dual";
String insertSql = "insert into customer (id, code, name, status) values (?, ?, ?, ?)"; TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
int id = jdbcTemplate.queryForInt(seqSql);
Object[] params = new Object[] { new Integer(id), customer.get("code"), customer.get("name"), map.get("status") };
jdbcTemplate.update(insertSql, params);
}
}
} public void update(final Map customer) throws Exception {
//
} public void delete(Integer id) throws Exception {
String deleteSql = "delete from customer where id = ?"; TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
getJdbcTemplate().update(deleteSql, new Object[] { id });
}
}
} public List findValidCustomers() throws Exception {
String querySql = "select * from customer where status = 'valid' order by code"; return getJdbcTemplate().query(querySql, new OracleColumnMapRowMapper());
}
}

(转)Spring的编程式事务例子的更多相关文章

  1. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  2. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  3. Spring的编程式事务和声明式事务

    事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务管理的两种方式 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务使用Transa ...

  4. 【Spring】编程式事务和声明式事务

    一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...

  5. Spring笔记(4) - Spring的编程式事务和声明式事务详解

    一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...

  6. spring 采用编程式事务

    1.getCurrentSession()与openSession()的区别? * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession() ...

  7. spring中编程式事务控制

    step1:配置xml文件 <!-- 事务管理bean --> <bean id="transactionManager" class="org.spr ...

  8. 转:全面分析 Spring 的编程式事务管理及声明式事务管理

    转:from: https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/

  9. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

随机推荐

  1. Python学习笔记1——人人都爱列表

    一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...

  2. 从new Function创建函数联想到MVC模式

    我们知道任何一个自定义函数都是Function构造器的实例,所以我们可以通过new Function的方式来创建函数,使用语法很简单, new Function(形参1, 形参2, ..., 形参N, ...

  3. CodeForces 478B 第八次比赛 B题

    Description n participants of the competition were split into m teams in some manner so that each te ...

  4. String面试题

    //a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...

  5. matlab实现插值法sin函数

    插值法实现sin函数: %calculate and print the sine function %input: x %output: sin(x) similar function y = si ...

  6. C++中用辗转相除法求两个数的最大公约数和最小公倍数

    两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0?  max=min,min=res return min; ...

  7. 使用JS启动本地应用程序、屏幕键盘

    问题描述:     现在希望在Web端使用JS调用本地应用程序 问题解决:   (1)使用JS启动本地应用程序 使用上述代码重点是创建了一个ActiveXObject的对象     参考说明:     ...

  8. 【BZOJ】【1430】小猴打架

    排列组合 蛮逗的…… 这题题干描述的就一股浓浓的Kruskal的气息……很容易就想到是求一个n个点的完全图的生成树个数,然后由于有序,再乘一个n-1的排列数(n-1条边的全排列)即(n-1)! 但是我 ...

  9. mybatis集成spring的事务管理

    第一 创建一个测试实体 public class Order { private int id; private String orderName; public Order(String order ...

  10. zend studio 10 字体,颜色,快捷键等相关设置

    一.修改字体 没想到zend studio 10中对中文显示不太好看,似乎有点小了.修改如下:打开 Window->Preferences->General->Appearance- ...