Spring Transaction 1+ Getting Started Case (Simple Bank Transfer)

0. Basic concepts of transactions

1. Transaction role:

 2. Transaction related configuration

1. Writing of configuration files

1.JDBC configuration file

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }

    //2.配置事务管理器,mybatis使用的是jdbc事务
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }



}

 jdbc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/www
jdbc.username=root
jdbc.password=root

2.Mybatis配置文件

public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}

 msc.setBasePackage("com.itheima.dao"); The dao package is a file that operates the database and corresponds to MapperScannerConfiger.

The domain package in ssfb.setTypeAliasesPackage("com.itheima.domain"); stores real classes, corresponding to SqlSessionFactoryBean.

2.SpringConfigh configuration file

@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
//3.开启注解式事务驱动
@EnableTransactionManagement
public class SpringConfig {
}

2. Writing at the bottom level

The Dao layer is a file that operates with the database, and the syntax is that of mybatis.

public interface AccountDao {

    @Update("update account set money = money + #{money} where name = #{name}")
    void inMoney(@Param("name") String name, @Param("money") Double money);

    @Update("update account set money = money - #{money} where name = #{name}")
    void outMoney(@Param("name") String name, @Param("money") Double money);
}

 3. Writing of Service layer

AccountService interface

public interface AccountService {

//    方法或者类说明注释,自动带参数和返回值
// 在需要注释的位置,输入/**,然后按一下enter即可实现,自动根据参数和返回值生成注释,
    /**
     * 转账操作
     * @param out 传出方
     * @param in 转入方
     * @param money 金额
     */
    //1.配置当前接口方法具有事务

    @Transactional
    public void transfer(String out,String in ,Double money) ;


}

 AccountServiceImpl implementation class

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired  //自动装配
    private AccountDao accountDao;

    public void transfer(String out,String in ,Double money) {
        //转账业务
        accountDao.outMoney(out,money);
        //int i = 1/0;
        accountDao.inMoney(in,money);
    }

}

4. Test layer writing

@RunWith(SpringJUnit4ClassRunner.class) //绑定类别
@ContextConfiguration(classes = SpringConfig.class) //上下文配置类
public class AccountServiceTest {

    @Autowired //自动装配
    private AccountService accountService;

    @Test //测试注解
    public void testTransfer() throws IOException {
        accountService.transfer("B","A",200.0);
    }

}

5. Define the transaction process

@Transactional can be added directly to the interface. The effect is that all methods in this interface are transactions.

Guess you like

Origin blog.csdn.net/m0_61395860/article/details/133210431