SSM10.2【Spring:基于XML的声明式事务控制】
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.haifei</groupId> 8 <artifactId>SSM10_spring_tx</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 13 <dependencies> 14 <dependency> 15 <groupId>mysql</groupId> 16 <artifactId>mysql-connector-java</artifactId> 17 <version>5.1.32</version> 18 </dependency> 19 <dependency> 20 <groupId>c3p0</groupId> 21 <artifactId>c3p0</artifactId> 22 <version>0.9.1.2</version> 23 </dependency> 24 <dependency> 25 <groupId>com.alibaba</groupId> 26 <artifactId>druid</artifactId> 27 <version>1.1.10</version> 28 </dependency> 29 30 <dependency> 31 <groupId>junit</groupId> 32 <artifactId>junit</artifactId> 33 <version>4.12</version> 34 <scope>test</scope> 35 </dependency> 36 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-context</artifactId> 40 <version>5.0.5.RELEASE</version> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-test</artifactId> 45 <version>5.0.5.RELEASE</version> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-web</artifactId> 50 <version>5.0.5.RELEASE</version> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-webmvc</artifactId> 55 <version>5.0.5.RELEASE</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-jdbc</artifactId> 60 <version>5.0.5.RELEASE</version> 61 </dependency> 62 <!--spring事务--> 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-tx</artifactId> 66 <version>5.0.5.RELEASE</version> 67 </dependency> 68 <!--spring AOP--> 69 <dependency> 70 <groupId>org.aspectj</groupId> 71 <artifactId>aspectjweaver</artifactId> 72 <version>1.8.4</version> 73 </dependency> 74 75 <dependency> 76 <groupId>javax.servlet</groupId> 77 <artifactId>javax.servlet-api</artifactId> 78 <version>3.0.1</version> 79 <scope>provided</scope> 80 </dependency> 81 <dependency> 82 <groupId>javax.servlet.jsp</groupId> 83 <artifactId>javax.servlet.jsp-api</artifactId> 84 <version>2.2.1</version> 85 <scope>provided</scope> 86 </dependency> 87 <dependency> 88 <groupId>jstl</groupId> 89 <artifactId>jstl</artifactId> 90 <version>1.2</version> 91 </dependency> 92 93 <dependency> 94 <groupId>com.fasterxml.jackson.core</groupId> 95 <artifactId>jackson-core</artifactId> 96 <version>2.9.0</version> 97 </dependency> 98 <dependency> 99 <groupId>com.fasterxml.jackson.core</groupId> 100 <artifactId>jackson-databind</artifactId> 101 <version>2.9.0</version> 102 </dependency> 103 <dependency> 104 <groupId>com.fasterxml.jackson.core</groupId> 105 <artifactId>jackson-annotations</artifactId> 106 <version>2.9.0</version> 107 </dependency> 108 109 <dependency> 110 <groupId>commons-fileupload</groupId> 111 <artifactId>commons-fileupload</artifactId> 112 <version>1.3.1</version> 113 </dependency> 114 <dependency> 115 <groupId>commons-io</groupId> 116 <artifactId>commons-io</artifactId> 117 <version>2.3</version> 118 </dependency> 119 120 <dependency> 121 <groupId>commons-logging</groupId> 122 <artifactId>commons-logging</artifactId> 123 <version>1.2</version> 124 </dependency> 125 <dependency> 126 <groupId>org.slf4j</groupId> 127 <artifactId>slf4j-log4j12</artifactId> 128 <version>1.7.7</version> 129 </dependency> 130 <dependency> 131 <groupId>log4j</groupId> 132 <artifactId>log4j</artifactId> 133 <version>1.2.17</version> 134 </dependency> 135 </dependencies> 136 137 138 <build> 139 <plugins> 140 <!--jdk编译插件--> 141 <plugin> 142 <groupId>org.apache.maven.plugins</groupId> 143 <artifactId>maven-compiler-plugin</artifactId> 144 <version>3.1</version> 145 <configuration> 146 <target>1.8</target> 147 <source>1.8</source> 148 <encoding>UTF-8</encoding> <!--防止sout内部输出中文乱码--> 149 </configuration> 150 </plugin> 151 <!--tomcat7插件--> 152 <plugin> 153 <groupId>org.apache.tomcat.maven</groupId> 154 <artifactId>tomcat7-maven-plugin</artifactId> 155 <version>2.1</version> 156 <configuration> 157 <port>8080</port> 158 <path>/ssm10</path> 159 <uriEncoding>UTF-8</uriEncoding> <!--防止get请求url中中文参数乱码--> 160 </configuration> 161 </plugin> 162 </plugins> 163 </build> 164 165 166 </project>
1 package com.haifei.domain; 2 3 public class Account { 4 5 private String name; 6 private double money; 7 8 public String getName() { 9 return name; 10 } 11 12 public void setName(String name) { 13 this.name = name; 14 } 15 16 public double getMoney() { 17 return money; 18 } 19 20 public void setMoney(double money) { 21 this.money = money; 22 } 23 24 }
1 package com.haifei.controller; 2 3 import com.haifei.service.AccountService; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class AccountController { 8 9 public static void main(String[] args) { 10 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); 11 AccountService accountService = app.getBean(AccountService.class); 12 accountService.transfer("tom","lucy",500); 13 } 14 15 }
1 package com.haifei.service; 2 3 public interface AccountService { 4 5 public void transfer(String outMan,String inMan,double money); 6 7 }
1 package com.haifei.service; 2 3 import com.haifei.dao.AccountDao; 4 5 /** 6 * 声明式事务控制明确事项: 7 * 谁是切点? transfer() 8 * 谁是通知? 事务控制 9 * 配置切面? AOP配置 10 */ 11 public class AccountServiceImpl implements AccountService{ 12 13 private AccountDao accountDao; 14 public void setAccountDao(AccountDao accountDao) { 15 this.accountDao = accountDao; 16 } 17 18 public void transfer(String outMan,String inMan,double money){ 19 //开启事务 。。。 20 accountDao.out(outMan,money); //调用事务1-转出钱 21 // int i = 1/0; //java.lang.ArithmeticException: / by zero 22 accountDao.in(inMan,money); //调用事务2-转入钱 23 //提交事务 。。。 24 } 25 26 }
1 package com.haifei.dao; 2 3 public interface AccountDao { 4 5 public void out(String outMan,double money); 6 public void in(String inMan,double money); 7 8 }
1 package com.haifei.dao; 2 3 import org.springframework.jdbc.core.JdbcTemplate; 4 5 public class AccountDaoImpl implements AccountDao{ 6 7 private JdbcTemplate template = new JdbcTemplate(); 8 public void setTemplate(JdbcTemplate template) { 9 this.template = template; 10 } 11 12 //事务1-转出钱 13 public void out(String outMan, double money) { 14 template.update("update account set money=money-? where name=?",money,outMan); 15 } 16 17 //事务2-转入钱 18 public void in(String inMan, double money) { 19 template.update("update account set money=money+? where name=?",money,inMan); 20 } 21 22 }
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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 12 "> 13 14 <!--加载外部的properties文件--> 15 <context:property-placeholder location="classpath:jdbc.properties"/> 16 <!--配置数据源(数据库连接池)--> 17 <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 18 <property name="driverClass" value="${jdbc.driver}"></property> 19 <property name="jdbcUrl" value="${jdbc.url}"></property> 20 <property name="user" value="${jdbc.username}"></property> 21 <property name="password" value="${jdbc.password}"></property> 22 </bean> 23 <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource"> 24 <property name="driverClassName" value="${jdbc.driver}"></property> 25 <property name="url" value="${jdbc.url}"></property> 26 <property name="username" value="${jdbc.username}"></property> 27 <property name="password" value="${jdbc.password}"></property> 28 </bean> 29 <!--配置spring jdbcTemplate--> 30 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 31 <property name="dataSource" ref="dataSource_c3p0"/> <!--数据源可选c3p0和druid--> 32 </bean> 33 34 35 <bean id="accountDao" class="com.haifei.dao.AccountDaoImpl"> 36 <property name="template" ref="jdbcTemplate"/> 37 </bean> 38 <!--目标对象,其内部的方法transfer()就是切点--> 39 <bean id="accountService" class="com.haifei.service.AccountServiceImpl"> 40 <property name="accountDao" ref="accountDao"/> 41 </bean> 42 43 44 <!--配置平台事务管理器--> 45 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 46 <property name="dataSource" ref="dataSource_c3p0"/> 47 </bean> 48 <!--配置通知-事务的增强--> 49 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 50 <!--设置事务的属性信息--> 51 <tx:attributes> 52 <!--<tx:method name="*" />--> <!--哪些方法被增强--> 53 <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="1" read-only="false"/> 54 <!--<tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 55 <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> 56 <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>--> 57 </tx:attributes> 58 </tx:advice> 59 60 61 <!--配置事务的AOP织入--> <!--注意:在aop声明切面时,配置事务用aop:advisor,配置一般增强用aop:aspect--> 62 <aop:config> 63 <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.haifei.service.*.*(..))" />--> 64 <aop:pointcut id="txPointcut" expression="execution(* com.haifei.service.*.*(..))" /> 65 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> 66 </aop:config> 67 68 </beans>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!