04、spring Aop 事务入门配置
项目目录结构:

1、导入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2、创建实体类(数据库表以类的属性一致)
public class Account implements Serializable {
    private Integer id;
    private String name;
    private double money;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
3、创建JdbcBase类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class JdbcBase {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setDataSource(DataSource dataSource) {
if(jdbcTemplate == null){
jdbcTemplate = new JdbcTemplate();
}
jdbcTemplate.setDataSource(dataSource);
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
4、创建DAO
接口:
import com.boat.entity.Account;
import java.util.List;
public interface AccountDao {
int save(Account account);
List<Account> list();
}
实现类:
import com.boat.dao.AccountDao;
import com.boat.dao.impl.base.JdbcBase;
import com.boat.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
public class AccountDaoImpl extends JdbcBase implements AccountDao{
@Override
public int save(Account account) {
return getJdbcTemplate().update("insert into account(name,money) value(?,?)",account.getName(),account.getMoney());
}
@Override
public List<Account> list() {
return getJdbcTemplate().query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
}
}
5、创建service服务
接口:
import com.boat.entity.Account;
import java.util.List;
public interface AccountService {
public abstract int save(Account account);
public abstract List<Account> listAccount();
}
实现类:
import com.boat.dao.AccountDao;
import com.boat.entity.Account;
import com.boat.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public int save(Account account) {
int falg = accountDao.save(account);
int i = 1 / 0;
return falg;
}
@Override
public List<Account> listAccount() {
return accountDao.list();
}
}
6、配置bean.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:constext="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置dao-->
<bean id="accountDao" class="com.boat.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<!--配置service-->
<bean id="accountService" class="com.boat.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置jdbcTemplate-->
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"></property>
</bean>
<!--配置数据源-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
<property name="username" value="root"></property>
<property name="password" value="root123"></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>
<!--,
isolation="DEFAULT" 设置事务隔离级别,默认是DEFAULT,表示使用数据库隔离级别
propagation="REQUIRED" 事务的传播行为,默认REQUIRED,表示一定会有事务,增删改的选择
查询方法选择SUPPORTS
read-only="true" 用于设置事务是否只懂,只有查询方法才设置为true
timeout="" 设置事务的超时时间,默认是-1,表示永不超时;如果设置值,以秒为单位
rollback-for="" 设置一个异常,遇到指定的异常时,事务回滚,遇见其它异常,事务不回滚;没有默认值,表示任何异常都回滚
no-rollback-for="" 设置一个异常,遇到指定的异常时,事务不回滚,遇见其它异常,事务回滚;没有默认值,表示任何异常都回滚
-->
<tx:method name="*" propagation="REQUIRED" read-only="false" />
<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!--配置事务aop-->
<aop:config>
<!--配置事务切入点表达式-->
<aop:pointcut id="execution" expression="execution(* com.boat.service.impl.*.*(..))"/>
<!--建立事务通知和切入点的对应关系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="execution"></aop:advisor>
</aop:config>
</beans>
7、测试事务
import com.boat.entity.Account;
import com.boat.service.AccountService;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:service.xml"})
public class TestSpring {
@Autowired
private AccountService accountService;
@Test
public void show(){
Account account = new Account();
account.setName("王五3");
account.setMoney(100.0);
accountService.save(account);
}
@Test
public void list(){
System.out.println(accountService.listAccount());
}
}
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号