JAVA程式設計121——Spring事務管理
阿新 • • 發佈:2018-12-01
一、目錄結構
二、程式碼詳解
1、maven配置檔案:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
< modelVersion>4.0.0</modelVersion>
<groupId>com.mollen</groupId>
<artifactId>spring_day04_transaction</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
< artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version >
</dependency>
<!--Spring DI核心依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--Spring jdbc依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--SpringAOP 相關依賴-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、spring配置檔案:beans.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--載入屬性檔案/自動尋找${}標記的屬性並對應-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置資料來源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置dao-->
<bean id="accountDao" class="com.mollen.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbaTamplate"></property>
</bean>
<!--配置service-->
<bean id="accountService" class="com.mollen.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置jdbctemplate-->
<bean id="jdbaTamplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<!--配置宣告式事物控制/Spring事務控制需要傳入一個數據源dataSource-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事務通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--查詢相關-->
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<!--新增相關-->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<!--修改相關-->
<tx:method name="update*" propagation="REQUIRED"/>
<!--刪除相關-->
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--SpringAOP配置-->
<aop:config>
<!--配置切入點,一般在業務層-->
<aop:pointcut id="txPointCut" expression="execution(* com.mollen.service.impl.*.*(..))"/>
<!--建立切入點表示式和事務通知之間的關係-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
3、建立實體類:Account.java
package com.mollen.bean;
public class Account {
private int id ;
private String name ;
private double money ;
public Account() {
}
public Account(String name, double money) {
this.id = id;
this.name = name;
this.money = money;
}
//getter/setter...
4、dao層:
1、dao層介面:AccountDao.java
package com.mollen.dao;
import com.mollen.bean.Account;
import java.sql.SQLException;
public interface AccountDao {
/**
* 根據使用者名稱稱查詢使用者資訊
* @param name
* @return
*/
Account findByName(String name) throws SQLException;
/**
* 更新賬戶資訊
* @param source
*/
void update(Account source) throws SQLException;
/**
* 新增賬戶
* @param account
*/
void add(Account account);
}
2、dao介面實現類:AccountDaoImpl.java
package com.mollen.dao.impl;
import com.mollen.bean.Account;
import com.mollen.dao.AccountDao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.sql.SQLException;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public Account findByName(String name) throws SQLException {
String sql = "select * from account where name = ? ";
try {
return getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), name);
} catch (DataAccessException e) {
e.printStackTrace();
}
return null ;
}
@Override
public void update(Account account) throws SQLException {
String sql = "update account set money = ? where name = ? ";
getJdbcTemplate().update(sql,account.getMoney(),account.getName());
}
@Override
public void add(Account account) {
String sql = "insert into account values(null,?,?)";
getJdbcTemplate().update(sql,account.getName(),account.getMoney());
}
}
5、service層
1、service層介面:AccountService.java
package com.mollen.service;
import com.mollen.bean.Account;
import java.sql.SQLException;
public interface AccountService {
/**
* 轉賬業務功能
* @param sourceName 轉出賬戶
* @param targetName 轉入賬戶
* @param money 轉賬金額
* @throws SQLException
*/
public void transfer(String sourceName, String targetName, double money) throws SQLException;
/**
* 新增賬戶資訊
* @param account
*/
public void add(Account account);
}
2、service層介面實現類
package com.mollen.service.impl;
import com.mollen.bean.Account;
import com.mollen.dao.AccountDao;
import com.mollen.service.AccountService;
import java.sql.SQLException;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao ;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 1、轉賬業務
* @param sourceName 轉出賬戶
* @param targetName 轉入賬戶
* @param money 轉賬金額
* @throws SQLException
*/
@Override
public void transfer(String sourceName, String targetName, double money) throws SQLException {
//1.根據名稱查詢轉出賬戶
Account source = accountDao.findByName(sourceName);
//1.1.轉出賬戶減錢
source.setMoney(source.getMoney()-money);
//1.2.更新轉出賬戶餘額
accountDao.update(source);
//int i = 10 / 0 ;
//2.根據名稱查詢轉入賬戶
Account target = accountDao.findByName(targetName);
//2.1 轉入賬戶加錢
target.setMoney(target.getMoney()+money);
//2.2 更新轉入賬戶餘額
accountDao.update(target);
}
/**
* 2、新增業務
* @param account
*/
@Override
public void add(Account account) {
account = new Account("ddd",1000);
accountDao.add(account);
}
}
6、編寫測試類:AccountServiceTest.java
package com.mollen.service;
import com.mollen.bean.Account;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
public class AccountServiceTest {
private AccountService accountService ;
//初始化
@Before
public void init(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:beans.xml");
accountService = (AccountService)applicationContext.getBean("accountService");
}
//轉賬測試
@Test
public void transfer() throws SQLException {
accountService.transfer("aaa","bbb",100);
}
//新增測試
@Test
public void add() throws SQLException {
Account account = new Account("ddd",100);
accountService.add(account);
}
//銷燬
@After
public void destory(){
}
}