這裏通過轉賬來對spring 例子進行說明
小明是個程序員,七夕節到了,小明想着給老婆發紅包,用自己的開發的某個支付平臺,給老婆發了1314的紅包,但是程序出現了問題,發生了異常,結果不僅老婆沒收到錢,小明的1314元錢也丟失了,這把小明氣壞了,決心更改自己的支付平臺,通過學習spring ,他想到了事務管理,通過回滾來解決這個問題
1 首先整理下配置管理需要的jar包
2 創建數據庫
3 創建業務邏輯層和Dao層
業務邏輯代碼
package tx;
/**
* @Author: Han Yu
* @Description:
* @Date: Create in 19:17 2019/1/13
*/
public class OrdersService {
private OrdersDao ordersDao;
public void setOrdersDao(OrdersDao ordersDao) {
this.ordersDao = ordersDao;
}
public void accountMonry(){
//張三少錢
ordersDao.lessMoney();
//但是如果中間出現異常 需要回滾
int i=10/0;
//李四多錢
ordersDao.moreMoney();
}
}
dao層代碼
package tx;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* @Author: Han Yu
* @Description:
* @Date: Create in 19:17 2019/1/13
*/
public class OrdersDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//少錢的方法
public void lessMoney(){
String sql="update account set salary=salary-? where username=?";
jdbcTemplate.update(sql,1000,"張三");
}
//多錢的方法
public void moreMoney(){
String sql="update account set salary=salary+? where username=?";
jdbcTemplate.update(sql,1000,"李四");
}
}
4 基於配置文件實現事務管理
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!--創建對象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入屬性值-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///springjdbc"></property>
<property name="user" value="root"></property>
<property name="password" value="mysql"></property>
</bean>
<!--創建jdbcTemplate對象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--dataSource傳入jdbcTemplate對象中-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="ordersDao" class="tx.OrdersDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="ordersService" class="tx.OrdersService">
<property name="ordersDao" ref="ordersDao"></property>
</bean>
<!--第一步 配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--第二步 配置事務的增強-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!--設置事務操作方法的匹配規則-->
<tx:attributes>
<!--<tx:method name="account*"/>-->
<tx:method name="accountMoney"/>
</tx:attributes>
</tx:advice>
<!--第三步配置切面 -->
<aop:config>
<!--切入點-->
<aop:pointcut id="pointcut1" expression="execution(* tx.OrdersService*(..))"></aop:pointcut>
<!--切面-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"></aop:advisor>
</aop:config>
</beans>
5 註解的方式開發
1 配置事務管理器
<!--第一步 配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
2 開啓事務註解掃描
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
3 在使用事務的方法上所在類上面添加註解
package txaspecj;
import org.springframework.transaction.annotation.Transactional;
/**
* @Author: Han Yu
* @Description:
* @Date: Create in 19:17 2019/1/13
*/
@Transactional
public class OrdersService {
private OrdersDao ordersDao;
public void setOrdersDao(OrdersDao ordersDao) {
this.ordersDao = ordersDao;
}
public void accountMonry(){
//張三少錢
ordersDao.lessMoney();
//但是如果中間出現異常 需要回滾
int i=10/0;
//李四多錢
ordersDao.moreMoney();
}
}
註解開發總的xml文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!--創建對象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入屬性值-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///springjdbc"></property>
<property name="user" value="root"></property>
<property name="password" value="mysql"></property>
</bean>
<!--創建jdbcTemplate對象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--dataSource傳入jdbcTemplate對象中-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="ordersDao" class="tx.OrdersDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="ordersService" class="tx.OrdersService">
<property name="ordersDao" ref="ordersDao"></property>
</bean>
<!--第一步 配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--開啓事務註解掃描 要指定用的哪個事務管理器-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
6 測試類
package tx;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author: Han Yu
* @Description:
* @Date: Create in 19:28 2019/1/13
*/
public class OrdersTest {
@Test
public void TestDemo(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
OrdersService ordersService = (OrdersService)context.getBean("ordersService");
ordersService.accountMonry();
}
}
package txaspecj;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author: Han Yu
* @Description:
* @Date: Create in 19:28 2019/1/13
*/
public class OrdersTest {
@Test
public void TestDemo(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
OrdersService ordersService = (OrdersService)context.getBean("ordersService");
ordersService.accountMonry();
}
}