浏览 2508 次
锁定老帖子 主题:spring入门实例-DAO,事务
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-07-27
spring入门实例-DAO,事务
实例: 配置文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@192.168.32.227:1521:orcl</value> </property> <property name="password"> <value>lpromis</value> </property> <property name="username"> <value>lpromis</value> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> </bean> <bean id="userDAO" class="com.myspring.bussiness.CustomerDAO"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="transactionManager"> <ref local="transactionManager"></ref> </property> </bean> </beans> 实例中的事务使用的是DataSourceTransactionManager实现的,并且向其中注入了datasource bean代码:
public class CustomerDAO { /** * datasource */ private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void setTransactionManager(PlatformTransactionManager transactionManager) { this.transactionManager = transactionManager; } /** * 事务管理类,初始化已将datasource作为参数 */ private PlatformTransactionManager transactionManager; /** * 普通使用事务的方法 * 定义事务 * 记录事务点 * 提交事务点/回滚事务点 */ public void insertCustomer() { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname1')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname2')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025','testname3')"); transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); System.out.println("*********roll back"); } } /** * 事务模板,无返回值 */ public void insertCustomerTamplate() { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult(){ @Override protected void doInTransactionWithoutResult(TransactionStatus arg0) { // TODO Auto-generated method stub JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025,'testname6')"); } }); } /** * 事务模板,带有返回值 */ public void queryCustomerTamplate() { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); Object result = transactionTemplate.execute( new TransactionCallback<Object>() { @Override public Object doInTransaction(TransactionStatus arg0) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025','testname6')"); List<Map<String,Object>> rows = jdbcTemplate.queryForList("select eee from t_lpromis_yxgl_khxx"); return rows; } }); if(result instanceof List) { List re = (List) result; for (int i = 0; i < re.size(); i++) { Map<String,Object> one = (Map<String, Object>) re.get(i); System.out.println(one.get("id")); } } } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/myspring/bussiness/transactionManager.xml"); CustomerDAO cd = (CustomerDAO) context.getBean("userDAO"); cd.queryCustomerTamplate(); } }以上用JdbcTemplate实现的jdbc的一系列操作,以及设置事务,其中的sql语句故意错误,验证事务rollback效果 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |