码迷,mamicode.com
首页 > 其他好文 > 详细

声明式事务

时间:2019-12-03 23:35:53      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:word   cto   连接   测试   publickey   contex   mysql   ESS   throw   

1 需求和技术要求

1.1 需求

  • 实现账户的声明式事务。

1.2 技术要求

  • 使用Spring的IOC实现对象的管理。
  • 使用C3P0作为数据库连接池。
  • 使用Spring的JdbcTemplate作为持久层解决方案。

 

2 应用示例

  • 示例:
  • Account.java
package com.sunxiaping.bean;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;

    private String name;

    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

}
  • AccountDao.java
package com.sunxiaping.dao;

import com.sunxiaping.bean.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save(Account account) {
        jdbcTemplate.update(" insert into account(`name`,`money`) value (?,?) ", account.getName(), account.getMoney());
    }
}
  • AccountService.java
package com.sunxiaping.service;

import com.sunxiaping.bean.Account;
import com.sunxiaping.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountService {

    @Autowired
    private AccountDao accountDao;

    public void saveAccount(Account account) {

        accountDao.save(account);

        int i = 1 / 0;

    }


}
  • SpringConfig.java
package com.sunxiaping.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@ComponentScan(value = "com.sunxiaping")
@EnableTransactionManagement
@Configuration //告诉Spring这是一个配置类
public class SpringConfig {

    /**
     * 注册数据源
     *
     * @return 数据源
     * @throws PropertyVetoException 异常
     */
    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    /**
     * 注册JdbcTemplate
     *
     * @param dataSource 数据源
     * @return JdbcTemplate
     */
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 注册事务管理器
     *
     * @param dataSource 数据源
     * @return 事务管理器
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


}
  • 测试:
package com.sunxiaping.test;


import com.sunxiaping.bean.Account;
import com.sunxiaping.config.SpringConfig;
import com.sunxiaping.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringTest {

    @Test
    public void test() {

        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        AccountService accountService = context.getBean(AccountService.class);

        Account account = new Account();
        account.setName("张三");
        account.setMoney(20.1);

        accountService.saveAccount(account);


    }

}

 

声明式事务

标签:word   cto   连接   测试   publickey   contex   mysql   ESS   throw   

原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/11979947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!