春Javaアノテーションは、総務を設定し

春Javaアノテーションは、総務を設定し

銀行振込は、より古典的なシナリオである、ジョー・スミスがジョン・ドウに100ドルを転送しますが、銀行システムが突然停電した方法は、常識がジョンのお金を変更してはならないことを前提としています。しかし、唯一の90元を着座プログラムの実施、によると、それでも唯一ジョン・ドウ100は、トランザクションの重要性は自明であり、より深刻な問題がありました。

Mavenのプロジェクト、輸入依存度を作成し、注目依存バージョンが一致していなければならない、そうでない場合はエラーが発生します。

 	   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>

データベースのテーブル:

CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `money` int(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

ここに画像を挿入説明

コンフィギュレーション・クラスを作成します。

JdbcConfig.java


@EnableTransactionManagement //等同于xml配置中的 <tx:annotation-driven/>
@Configuration
@ComponentScan(basePackages = "org.youyuan")
public class JdbcConfig {

    @Bean
    DataSource dataSource(){
        //DruidDataSource build = DruidDataSourceBuilder.create().build();
        DruidDataSource build = new DruidDataSource();
        //DriverManagerDataSource build = new DriverManagerDataSource();
        build.setUsername("root");
        build.setPassword("root");
        build.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC");
        build.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return build;
    }

    @Bean
    JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }
    // 配置事务管理器 注入连接池
    @Bean
    DataSourceTransactionManager dataSourceTransactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }
}

AccountDao.java


@Repository
@Transactional //也可加在方法上
public class AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void addMoney(String username, Integer money) {
        System.out.println(this.jdbcTemplate);
        jdbcTemplate.update("update account set money=money+? where name=?", money, username);
    }

    public void minMoney(String username, Integer money) {
        jdbcTemplate.update("update account set money=money-? where name=?", money, username);
    }

}

AccountService.java


@Service
public class AccountService {

    @Autowired
    AccountDao accountDao;
    public void updateMoney(){
        accountDao.addMoney("zs",10);
        int a = 1/0;//异常
        accountDao.minMoney("ls",10);
    }
}

テストを追加します。


public class TransactionTest {
    @Autowired
    private AccountService accountService;
    @BeforeEach
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JdbcConfig.class);
        accountService=context.getBean(AccountService.class);
    }
    @Test
    public void accountTest(){
        accountService.updateMoney();
    }

結果:

異常なプログラムエラーは、データベース内のデータは変更されません。
ここに画像を挿入説明

公開された25元の記事 ウォンの賞賛0 ビュー285

おすすめ

転載: blog.csdn.net/qq_42219004/article/details/105204165