大型运输行业实战_day10_1_自定义事务管理类

时间:2023-03-08 17:26:45

1.创建事务管理类  TransactionManager.java

 package com.day02.sation.transaction;

 import com.day02.sation.util.JdbcUtil;

 import java.sql.SQLException;

 /**
* Created by Administrator on 1/8.
*/
public class TransactionManager {
/**
* 关闭事务自动提交
*/
public void before1() {
System.out.println("-----before-----");
try {
JdbcUtil.getConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 手动提交事务
*/
public void after2() {
System.out.println("-----after-----");
try {
JdbcUtil.getConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 事务回滚
*/
public void rollback() {
System.out.println("-----rollback-----");
try {
JdbcUtil.getConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

2.创建配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--需要被管理的业务类-->
<bean id="ticket3Service" class="com.day02.sation.transaction.Ticket3Service"/>
<!--事务管理器-->
<bean id="txManager" class="com.day02.sation.transaction.TransactionManager"/>
<!-- aop配置-->
<aop:config>
<!-- 配置切面-->
<aop:aspect ref="txManager">
<!--地点:方法-->
<aop:pointcut id="txPoint" expression=" execution(* com.day02.sation.transaction.*Service.*(..) )"/>
<!--时间-->
<!-- 方法执行前-->
<aop:before method="before1" pointcut-ref="txPoint"/>
<!--方法执行后-->
<aop:after-returning method="after2" pointcut-ref="txPoint"/>
<!--抛出异常时-->
<aop:after-throwing method="rollback" pointcut-ref="txPoint"/>
</aop:aspect>
</aop:config>
</beans>

3.需要被管理的业务类 Ticket3Service.java

 package com.day02.sation.transaction;

 import com.day02.sation.util.JdbcUtil;

 import java.sql.Connection;
import java.sql.Statement; /**
* Created by Administrator on 1/8.
*/
public class Ticket3Service {
public void buyTicket() throws Exception {
Connection connection = JdbcUtil.getConnection();
Statement statement = connection.createStatement();
//关闭自动提交数据
String sql = " UPDATE ticket SET standby=14 WHERE id=3 ";
int i = statement.executeUpdate(sql);
System.out.println("===" + (1 / 0));
//新增订单
String sql2 = "INSERT INTO ticket_order (order_number,user_id,ticket_id) VALUES ('105',1,3)";
statement.executeUpdate(sql2);
}
}

4.测试方法

 package com.day02.sation.test;

 import com.day02.sation.transaction.Ticket3Service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* Created by Administrator on 12/27.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-myaop.xml")
public class TestTransaction {
@Autowired
private Ticket3Service ticket3Service; @Test
public void testGetList() {
try {
ticket3Service.buyTicket(); } catch (Exception e) {
e.printStackTrace();
}
}
}

5.使用到的jdbc工具类

 package com.day02.sation.util;

 import java.sql.Connection;
import java.sql.DriverManager; /**
* Created by Administrator on 1/8.
*/
public class JdbcUtil {
private static Connection connection = null; static {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station", "root", "admin");
} catch (Exception e) {
e.printStackTrace();
} } public static Connection getConnection() { return connection;
}
}

完成测试吧!