Connection conn = null; try { conn.setAutoCommit(false); Statement stmt = conn.createStatement(); stmt.executeUpdate("update person set name='888' where id=1"); Savepoint savepoint = conn.setSavepoint(); try{ conn.createStatement().executeUpdate("update person set name='222' where sid=2"); }catch(Exception ex){ conn.rollback(savepoint); } stmt.executeUpdate("delete from person where id=9"); conn.commit(); stmt.close(); } catch (Exception e) { conn.rollback(); }finally{ try { if(null!=conn && !conn.isClosed()) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } |
创建web工程 导入jar包 在src目录编写配置 applicationContext.xml log4j.properties jdbc.properties ![]() |
2. 编写DAO注入 注入JdbcTemplate public class AccountDAO extends JdbcDaoSupport{ public void outMoney(String account, double money) { this.getJdbcTemplate().update( "update account set money = money - ? where name = ?", money,account); } public void inMoney(String account, double money) { this.getJdbcTemplate().update( "update account set money = money + ? where name = ?", money,account); } } |
编写Service注入DAO public class AccountService { private AccountDAO accountDAO; // 转账的业务操作 public void transfer() { // aaa 向 bbb 转账 200元 accountDAO.outMoney("aaa", 200); int d = 1 / 0; accountDAO.inMoney("bbb", 200); } public void setAccountDAO(AccountDAO accountDAO) { this.accountDAO = accountDAO; } |
配置applicationContext.xml <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- ${key} 可以读取properties文件中配置 key对应value --> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="accountService" class="cn.itcast.service.AccountService"> <property name="accountDAO" ref="accountDAO"></property> </bean> <bean id="accountDAO" class="cn.itcast.dao.AccountDAO"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> |
编写测试用例 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringTest { @Autowired private AccountService accountService; @Test public void demo() { accountService.transfer(); } } |
使用XML配置声明式事务 基于tx/aop
![]() |
<tx:method>配置 ![]() |
使用XML配置声明式事务 基于tx/aop 修改测试用例 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringTest { @Autowired private AccountService accountService; @Test public void demo() { accountService.transfer(); } } |
使用注解配置声明式事务 使用@Transactional注解 修改AccountService @Transactional public class AccountService { private AccountDAO accountDAO; // 转账的业务操作 public void transfer() { // aaa 向 bbb 转账 200元 accountDAO.outMoney("aaa", 200); // int d = 1 / 0; accountDAO.inMoney("bbb", 200); } public void setAccountDAO(AccountDAO accountDAO) { this.accountDAO = accountDAO; } } |
使用注解配置声明式事务 修改applicationContext.xml <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- ${key} 可以读取properties文件中配置 key对应value --> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> ... <tx:annotation-driven/> |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |