首页 > 代码库 > spring-编程式事务管理
spring-编程式事务管理
一、创建spring项目
项目名称:spring101310
二、在项目上添加jar包
1.在项目中创建lib目录
/lib
2.在lib目录下添加spring支持
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging.jar
junit-4.10.jar
log4j.jar
mysql-connector-java-5.1.18-bin.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
三、在项目中添加配置文件与属性文件
1.在项目中创建conf目录
2.在conf目录下添加属性文件
属性文件名称:jdbc.properties
属性文件内容:
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
2.在conf目录下添加spring核心配置文件
配置文件名称:applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.加载属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="http://www.mamicode.com/${jdbc.url}"></property>
<property name="driverClass" value="http://www.mamicode.com/${jdbc.driver}"></property>
<property name="user" value="http://www.mamicode.com/${jdbc.username}"></property>
<property name="password" value="http://www.mamicode.com/${jdbc.password}"></property>
</bean>
</beans>
四、实现bean设计
1.在src目录下创建实体bean的包
包名:cn.jbit.spring101310.domain
2.在包下创建实体bean
public class Account {
private Integer id;
private String name;
private Double money;
//省略get and set
}
五、设计Dao层
1.在src目录下创建dao层的包
包名:cn.jbit.spring101310.dao
2.在包下创建dao层的接口与实现类
1)接口设计
接口名称:IAccountDao.java
接口内容:
public interface IAccountDao {
/**
* 转出
*/
public void outMoney(Account outaccount);
/**
* 转入
*/
public void inMoney(Account inaccount);
}
2)接口实现类设计
实现类名称:AccountDaoImpl.java
实现类内容:
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
/**
* 转入
*/
@Override
public void inMoney(Account inaccount) {
String sql = "update account set money = money + ? where id = ?";
this.getJdbcTemplate().update(sql,inaccount.getMoney(),inaccount.getId());
}
/**
* 转出
*/
@Override
public void outMoney(Account outaccount) {
String sql = "update account set money = money - ? where id = ?";
this.getJdbcTemplate().update(sql,outaccount.getMoney(),outaccount.getId());
}
}
六、在核心配置文件中配置Dao
<!-- 3.配置Dao -->
<bean id="accountDao" class="cn.jbit.spring101310.dao.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
七、在核心配置文件中配置事务相关信息
<!-- 4.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.配置事务模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
八、业务层设计
1.在src目录下创建业务层的包
包名:cn.jbit.spring101310.service
2.在包下创建业务层的接口与实现类
1)接口设计
接口名称:AccountService.java
接口内容:
public interface AccountService {
public void transfer(Account outAccount,Account inAccount);
}
2)接口实现类设计
实现类名称:AccountServiceImpl.java
实现类内容:
public class AccountServiceImpl implements AccountService {
private IAccountDao accountDao;
private TransactionTemplate transactionTemplate;
@Override
public void transfer(final Account outAccount, final Account inAccount) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
//转出
accountDao.outMoney(outAccount);
int a = 1/0;
//转入
accountDao.inMoney(inAccount);
}
});
}
//省略get and set
}
九、在核心配置文件中配置业务层
<!-- 6.配置Service -->
<bean id="accountService" class="cn.jbit.spring101310.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
十、测试
1.在项目上创建test目录
/test
2.在test目录下创建测试包
包名:cn.jbit.spring101310.service
3.在测试包下创建测试类
测试类名:AccountServiceTest.java
测试类的内容:
public class AccountServiceTest {
@Test
public void testTranser(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
Account outAccount = new Account();
outAccount.setId(1);
outAccount.setMoney(500D);
Account inAccount = new Account();
inAccount.setId(2);
inAccount.setMoney(500D);
accountService.transfer(outAccount, inAccount);
}
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1563389
spring-编程式事务管理