spring事务声明基于注解

1.导入相关jar坐标

 <dependencies>
        <!--spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
        </dependency>
        <!--spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
        <!--c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <!--aspectjweaver -->
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.5.4</version>
        </dependency>

        <!--spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.9</version>
        </dependency>
        <!--spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.9</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

2.编写domain,Dao,Imp和service

public class Account {
    private String name;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Account(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

public interface AccountDao {
    void in(Account account);
    void out(Account account);
}



@Repository("AccountImp")
public class AccountImp implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void in(Account account) {
        jdbcTemplate.update("update account set price=price+? where name=?",account.getPrice(),
                account.getName());
    }

    @Override
    public void out(Account account) {
        jdbcTemplate.update("update account set price=price-? where name=?",account.getPrice(),
                account.getName());
    }
}


@Service
public class AccountService {
    private AccountImp accountImp;

    public AccountService(AccountImp accountImp) {
        this.accountImp = accountImp;
    }

    @Transactional(isolation = Isolation.DEFAULT ,timeout = -1,readOnly = false)
    public void transfer(String name, String in, double money){
        Account account=new Account(name,money);
        accountImp.out(account);
        account=new Account(in,money);
        int i=1/0;
        accountImp.in(account);
    }
}

3.编写配置文件

<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
   <!--扫描组件-->
    <context:component-scan base-package="com"/>
    <!--引入外部文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置jdbcTemplate-->
    <bean name="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource"/>
    </bean>
    <!--配置事务平台管理器-->
    <bean name="plate" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="datasource"/>
    </bean>
   <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="plate"/>
</beans>

4.外部文件

jdbc.url=jdbc:mysql://localhost:3306/test-spring
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=

5.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test {
    @Autowired
    private AccountService accountService;
    @org.junit.Test
    public void test(){
        accountService.transfer("xu","tom",100);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值