9 spring aop事务配置

1.创建表

CREATE TABLE account(
  id INT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50),
  money INT
);
INSERT INTO account(username,money) VALUES('jack','10000');
INSERT INTO account(username,money) VALUES('rose','10000');

2.导包
9 spring aop事务配置_第1张图片
3.创建Dao,DaoImpl

public interface Dao {
    public  void out(String outer ,Integer money);
    public void in(String inner ,Integer money);
}

package spring.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class DaoImpl extends JdbcDaoSupport implements Dao {
   String sql="insert into user(name,password) values ('jiang',12345)";
   public  void add(){
       String sql="insert into user(name,password) values ('jiang','fuck')";
       this.getJdbcTemplate().update(sql);
   }

    @Override
    public void out(String outer, Integer money) {
        String sql="update account set money=money-? where username=?";
        this.getJdbcTemplate().update(sql, money, outer);
    }

    @Override
    public void in(String inner, Integer money) {
        String sql="update account set money=money+? where username=?";
        this.getJdbcTemplate().update(sql, money, inner);
    }
}

4.创建Service,ServiceImpl

public interface Service {
    public  void transer(String outer,String inner ,Integer money);
}

public class ServiceImpl implements Service {
    private Dao dao;
    @Autowired
    private TransactionTemplate template;

    public void setTemplate(TransactionTemplate template) {
        this.template = template;
    }

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    @Override
    public void transer(String outer, String inner, Integer money) {
        this.template.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
               dao.out(outer,money);

                dao.in(inner,money);
            }
        });

    }
}

5.beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p ="http://www.springframework.org/schema/p"
       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/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
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd  ">

    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${driverClass}"></property>
    <property name="jdbcUrl" value="${jdbcUrl}"></property>
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>
</bean>
    <bean id="daoImpl" class="spring.dao.DaoImpl">
        <property name="dataSource" ref="datasource"></property>
    </bean>
    <bean id="service" class="spring.service.ServiceImpl">
        <property name="dao" ref="daoImpl"  ></property>

    </bean>
    <bean id="tx_template" class="org.springframework.transaction.support.TransactionTemplate">
            <property name="transactionManager" ref="tx_manager"></property>
    </bean>
    <bean id="tx_manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"></property>
    </bean>
    <tx:advice id="tx_aop_manager" transaction-manager="tx_manager">
        <tx:attributes>
            <tx:method name="transer" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor advice-ref="tx_aop_manager" pointcut="execution(* spring.service..*.*(..))"></aop:advisor>
    </aop:config>

</beans>

6.db.properties

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///db1
user=root
password=dljs0922

7.目录结构
9 spring aop事务配置_第2张图片
8.结果很完美
9.感想
(1)报错信息一定要看,这个可以省去很多时间
(2)使用spring-test jar 包时,一定要注意使用注解@Auto
(3)讲真,使用这个来插入事务配置的时候很方便,下面的标签可以规定事物的所有细节

<tx:advice id="tx_aop_manager" transaction-manager="tx_manager">
        <tx:attributes>
            <tx:method name="transer" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

你可能感兴趣的:(spring)