9 Spring学习:Spring事务管理( 四 )

第三步 , 创建一个实体包com.envy.entity , 接着在里面新建Account实体类:
package com.envy.entity;public class Account {private int id;private String name;private Long money;public int getId(){return id;}public void setId(int id){this.id=id;}public String getName(){return name;}public void setName(String name){this.name=name;}public Long getMoney(){return money;}public void setMoney(Long money){this.money = money;}public String toString(){return "Account:id is"+id+";name is"+ name+ ";money is"+money;}}第四步 , 创建一个Dao包com.envy.dao , 接着在里面新建TransferDao接口:
package com.envy.dao;public interface TransferDao {//付款,name账户名称 , amount支出金额void payMoney(String name,Long amount);//收款 , name账户名称 , amount收到金额void collectMoney(String name,Long amount);}然后在com.envy.dao中新建一个Impl包 , 在Impl中新建TransferDao接口的实现类TransferDaoImpl:
package com.envy.dao.Impl;import com.envy.dao.TransferDao;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class TransferDaoImpl extends JdbcDaoSupport implements TransferDao {//付款,name账户名称 , amount支出金额public void payMoney(String name, Long amount) {String sql = "update account set money = money-amount where name=?";this.getJdbcTemplate().update(sql,amount,name);}//收款 , name账户名称 , amount收到金额public void collectMoney(String name, Long amount) {String sql = "update account set money = money+amount where name=?";this.getJdbcTemplate().update(sql,amount,name);}}第五步 , 创建一个service包com.envy.service , 接着在里面新建TransferService接口:
package com.envy.service;public interface TransferService {void transferMoney(String source,String destination, Long amount);}然后在com.envy.service中新建一个Impl包 , 在Impl中新建TransferService接口的实现类TransferServiceImpl:
package com.envy.service.Impl;import com.envy.dao.TransferDao;import com.envy.service.TransferService;public class TransferServiceImpl implements TransferService {private TransferDao transferDao;public void setTransferDao(TransferDao transferDao){this.transferDao=transferDao;}//转账操作 , source支出方账户 , destination收款方账户 , amount转账金额public void transferMoney(String source, String destination, Long amount) {//付款操作transferDao.payMoney(source,amount);int i = 100/0;//此处用于测试抛异常时是否会回滚//收款操作transferDao.collectMoney(destination,amount);}}第六步 , 创建一个db.properties配置文件:
#加载驱动druid.driverClassName=com.mysql.jdbc.Driver#加载数据库druid.url=jdbc:mysql://localhost:3306/spring_money?useUnicode=true&characterEncoding=UTF-8#用户名druid.username=root#密码druid.password=root第七步 , 创建一个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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--不使用外部db.properties配置文件--><!--配置数据源--><!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">--><!--<property name="driverClassName" value=https://www.isolves.com/it/cxkf/cxy/2020-07-19/"com.mysql.jdbc.Driver"/>-->


推荐阅读