栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring——事务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring——事务

目录

1.Spring开启事务的操作

1.1需求​编辑

1.2 操作步骤

1.3 实现

2. Spring事务角色​编辑

3.事务属性

3.1事务配置

3.2事务传播行为 ​编辑

3.3 案例:转账业务追加日志


1.Spring开启事务的操作

1.1需求

 

1.2 操作步骤

1.3 实现
public class JdbcConfig {
    //1.定义一个方法获得要管理的对象
    //2.添加@Bean,表示当前方法的返回值是一个bean
    @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 druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(passWord);
        return druidDataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}



public class MyBatisConfig {

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

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        //Mapper代理的配置
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.dao");
        return msc;
    }
}



@Configuration
@ComponentScan("com")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement  //注解式的事务驱动
public class SpringConfig {
}
public interface AccountDao {

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

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

    @Select("select * from account")
    public List selectAll();

}


public interface AccountService {

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

    List selectAll();

}



@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public void tansfer(String in, String out, Double money) {
        accountDao.inMoney(in,money);
        int i = 1/0;//异常
        accountDao.outMoney(out,money);
    }

    public List selectAll() {
        return accountDao.selectAll();
    }
}

2. Spring事务角色

3.事务属性

3.1事务配置

public interface AccountService {

    @Transactional(rollbackFor = {IOException.class})
    //IOException默认不参与回滚,需要手动加上
    public void tansfer(String in, String out, Double money);

    List selectAll();

}

3.2事务传播行为
 

 

 3.3 案例:转账业务追加日志

public interface AccountDao {

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

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

}

public interface LogDao {

    @Insert("insert into account_log(info,createData) values(#{info},now())")
    public void addLog(String info);
}




public interface AccountService {

    @Transactional(rollbackFor = {IOException.class})
    //IOException默认不参与回滚,需要手动加上
    public void tansfer(String in, String out, Double money);

}

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private LogService logService;

    public void tansfer(String in, String out, Double money) {
        try{
            accountDao.inMoney(in, money);
            int i = 1/0;//异常
            accountDao.outMoney(out, money);
        }finally {
            logService.addLog(in, out, money);
        }
    }

}




public interface LogService {

    @Transactional(propagation = Propagation.REQUIRES_NEW)  //事务的传播行为:开启单独的事物
    public void addLog(String in, String out, double money);
}

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogDao logDao;

    public void addLog(String in, String out, double money) {
        logDao.addLog(out+"给"+in+"转账"+money+"元");
    }
}



public class App {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

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

        accountService.tansfer("zhangsan", "lisi", 50.0);

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号