本文主要是介绍学习Spring的第十八天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
注解方式配值声明式事务控制
直接上源码配置类
package com.itheima.config;import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@MapperScan("com.itheima.mapper")
@EnableTransactionManagement //<tx:annotation-driven/>
public class SpringConfig {@Beanpublic DataSource dataSource(@Value("${jdbc.driver}") String driver,@Value("${jdbc.url}") String url,@Value("${jdbc.username}") String username,@Value("${jdbc.password}") String password){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean;}@Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);return dataSourceTransactionManager;}}
里面包含 DataSourse方法 , sqlsessionfactoryBean方法 , 及事务管理器 DataSourseTransactionMAnager方法,
@ComponentScan("com.itheima") 扫描注解component
@PropertySource("classpath:jdbc.properties") 加载Propertys
@MapperScan("com.itheima.mapper") 扫描Mapper
@EnableTransactionManagement //<tx:annotation-driven/> 事务的自动代理 ,注解驱动
Service层 :
@Service("accountService")
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountMapper accountMapper;@Override@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)public void transferMoney(String outAccount, String inAccount, Integer money) {accountMapper.decrMoney(outAccount,money);int i = 1/0;accountMapper.incrMoney(inAccount,money);}
}
mapper
public interface AccountMapper {//+钱@Update("update tb_account set money=money+#{money} where account_name=#{accountName}")public void incrMoney(@Param("accountName") String accountName,@Param("money") Integer money);//-钱@Update("update tb_account set money=money-#{money} where account_name=#{accountName}")public void decrMoney(@Param("accountName") String accountName,@Param("money") Integer money);}
测试省略(spring_trans_test)
这篇关于学习Spring的第十八天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!