更多内容,前往 IT-BLOG

一、添加事务配置文件:applicationContext-tx.xml


 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
11  
12 
13     <!-- 事务管理器  -->  
14     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
15         <property name="dataSource" ref="dataSource" />  
16     </bean>  
17       
18     <!-- 开启事务控制的注解支持 -->  
19     <tx:annotation-driven transaction-manager="transactionManager"/>   
20 </beans>

二、在 web.xml 中引入 applicationContext-tx.xml 配置文件


 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     version="2.5">    
 6     
 7     <!-- 加载spring容器 -->
 8     <context-param>
 9         <param-name>contextConfigLocation</param-name>
10         <param-value>classpath*:spring/applicationContext*.xml</param-value>
11     </context-param>
12     <listener>
13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14     </listener>
15 </web-app>

三、在方法上添加注解:@Transactional 注解

注解可以添加在方法上,表示当前方法具有事务,如果注解添加在类上表上该类的所有方法都具有事务


@Service
@Transactional
public class GoodsServiceImpl implements GoodsService{
........
}

四、扩展-----也可以通过 java类来替代 xml配置文件,如下:


 1 /**
 2  * Created by Administrator on 2019/11/20 0020.
 3  * 导入相关依赖:数据源,数据库驱动,spring-jdbc模块
 4  * 配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)操作数据
 5  */
 6 
 7 @ComponentScan("com.spring")
 8 @Configuration
 9 @EnableTransactionManagement
10 public class TxConfig {
11     //数据源
12     @Bean
13     public DataSource dataSource() throws PropertyVetoException {
14         ComboPooledDataSource dataSource = new ComboPooledDataSource();
15         dataSource.setUser("root");
16         dataSource.setPassword("123");
17         dataSource.setDriverClass("com.mysql.jdbc.Driver");
18         dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
19         return dataSource;
20     }
21     @Bean
22     public JdbcTemplate jdbcTemplate() throws PropertyVetoException {
23         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
24         return jdbcTemplate;
25     }
26 
27     @Bean
28     public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException {
29         DataSource dataSource = dataSource();
30         return new DataSourceTransactionManager(dataSource);
31     }
32 }

注解式事务解决方案-小白菜博客注解式事务解决方案-小白菜博客
 ----关注公众号,获取更多内容----