配置文件的方式
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder location="classpath:jd.properties"/>
<!--加载属性的文件(使用开源连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务的通知(没有自己编写切面类,通知方法也不是自己编写,spring框架提供的)-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--对pay进行增强,设置隔离级别,传播行为,超时的时间-->
<!--isolation="DEFAULT":设置事务隔离级别为数据库默认级别。
propagation="REQUIRED":设置事务传播行为为REQUIRED(如果当前没有事务就新建一个,如果已存在就加入)。-->
<!--增删改需要进行事务管理-->
<tx:method name="pay" isolation="DEFAULT" propagation="REQUIRED"/>
<!--read-only="true":设置为只读事务(优化性能,适合查询操作),查询不需要-->
<tx:method name="find" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--配置AOP的增强-->
<aop:config>
<!--spring框架提供系统通知,使用advisor标签,对哪个方法进行事务管理-->
<aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.qcby.demo2.AccountServiceImpl.pay(..))"/>
</aop:config>
<!--配置 service-->
<bean id="accountService" class="com.qcby.demo2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置 dao-->
<bean id="accountDao" class="com.qcby.demo2.AccountDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
配置文件+直接的方式
配置文件代码
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder location="classpath:jd.properties"/>
<!--加载属性的文件(使用开源连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置jdbc模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解扫描-->
<context:component-scan base-package="com.qcby.demo2"/>
<!--开启事务注解的支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>
service代码
@Service
@Transactional(isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void pay(String out, String in, Double money) {
accountDao.outMoney(out,money);
//模拟异常
//int a = 1/0;
accountDao.inMoney(in,money);
}
}
dao代码
@Component
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void outMoney(String out, double money) {
jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);
}
public void inMoney(String in, double money) {
jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);
}
}
纯注解方式
配置类
@Configuration
@ComponentScan(basePackages = "com.qcby.demo2") //开启注解
@EnableTransactionManagement //开启事务注解
public class SpringConfig {
//获取连接需要的属性
@Bean(name = "dataSource")
public DataSource createDataSource() throws Exception{
//创建连接池对象,spring框架内置了连接池对象
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//设置四个参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_db");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
//创建模板对象
@Resource(name = "dataSource") //不仅可以作用在属性上,也可以作用在方法上
@Bean(name = "jdbcTemplate") //把JdbcTemplate保存到IOC容器中
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
JdbcTemplate template = new JdbcTemplate(dataSource);
return template;
}
//创建平台事务管理器对象
@Resource(name = "dataSource")
@Bean(name = "transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource){
DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);
return manager;
}
}