Spring事务案例
案例准备:
1.导入jar包
注意版本一致
<!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.3.Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.3.Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.3.Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.3.Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.3.RELEASE</version> </dependency> <!-- spring jdbc 相关的包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- 配置的 spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.3.RELEASE</version> </dependency>
- 创建数据库
在springjdbc库中直接创建表
/*Table structure for table `cardinfo` ( ` */ CREATE TABLE `cardinfo` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(20) NOT NULL, `money` DECIMAL(10,2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; /*Data for the table `cardinfo` */ INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES (1,'Helen','1000.00'); INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES (2,'Tom','1000.00');
- 创建dao接口
public interface ICardInfoDao { /** * 加钱方法 * @param id * @param money */ void increaseMoney(int id , float money); /** * 减钱方法 * @param id * @param money */ void decreaseMoney(int id , float money); }
- 创建dao实现类
@Component public class CardInfoDao implements ICardInfoDao { @Autowired private JdbcTemplate jdbcTemplate; public void increaseMoney(int id , float money) { jdbcTemplate.update("update cardinfo set money = money + ? where id = ? ;",money,id); } public void decreaseMoney(int id , float money) { jdbcTemplate.update("update cardinfo set money = money - ? where id = ? ;",money,id); } }
- 创建service接口
public interface ICardInfoService { //转账业务 void transfer(int from , int to , float money); }
- 创建service实现类
@Service public class CardInfoService implements ICardInfoService { @Autowired private ICardInfoDao cardInfoDao; public void transfer(Integer from, Integer to, Double money) { cardInfoDao.decreaseMoney(from,money); cardInfoDao.increaseMoney(to,money); } }
- 添加ioc配置文件
先不处理事务问题
注意xmlns的导入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.test"/> <context:property-placeholder location="db.properties" /> <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="${jdbc.driverClass}" p:jdbcUrl="${jdbc.jdbcUrl}" p:user="${jdbc.user}" p:password="${jdbc.password}" /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="comboPooledDataSource"/> </bean> </beans>
测试代码:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ICardInfoService service = (ICardInfoService) context.getBean("cardInfoService"); service.transfer(1,2,100.0);