一些xml与对应的注解
applicationContext---------@Configuration
加载jdbc的文件 <context :property-placeholder location = " classpath:jdbc.peoperties"/>----@propertySource
<context:component-scan beae-package =""/> -----------@componentScan
如果要使用注解注入或者set方式注入,都必须要先将对象加入到ioc容器中,注解用@bean,xml用<bean id= >
###事务:
一组最小单位的执行单元(一组SQL语句),要么一起成功,要么一起失败!!
事务的四个特性(ACID)?
原子性(A):如果使用事务控制对操作进行控制,这些操作要么一起成功,要么一起失败!!
一致性(C):事务可以让数据库从一个一致性状态切换到另一个一致性的状态(转账总额不变)
隔离性(I):多个并发事务之间应该相互隔离。
如果多个并发事务隔离出问题,可能导致以下现象:
1)脏读:一个事务读到另一个未提交的数据(必须防止的)
2)不可重复读:一个事务读到另一个已经提交的更新(update)数据
3)幻读(虚读) : 一个事务读到另一个已经提交的插入(insert)数据
注意:
1)mysql的默认隔离级别:repeatable read
oracle的默认隔离级别: read committed
2)隔离级别越高,事务并发性能越差
7.Spring声明式事务管理 —Api
导入spring-tx包,查看Api
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
|- PlatformTransactionManager 顶层接口 (spring-tx) (Spring提供用于进行事务控制的方法的接口)
|- DataSourceTransactionManager 实现类 (spring-jdbc)
(Spring提供用于进行事务控制的方法的实现代码,底层调用JDBC事务控制代码)
|- HibernateTransactionManager 实现类(spring-orm)(spring提供对Hibernate事务控制实现)
|- JpaTransactionManager 实现 (spring-orm)(spring提供对Jpa事务控制实现)
TransactionDefinition 接口: Spring提供的用于管理事务的特征的接口
作用:
设置事务的隔离级别
设置事务的传播行为(设置事务的边界!)
PROPAGATION_REQUIRED: 默认值,必须让当前(业务)方法在事务的控制下执行。
如果该方法没有事务,自动添加事务
如果该方法已经有了事务,直接使用当前事务。
项目的常用的方法:增加,删除,更新方法!
PROPAGATION_SUPPORTS: 让当前(业务)方法在可选事务中执行(事务可有可无)
如果该方法没有事务,该方法就没有事务。
如果该方法已经有了事务,直接使用当前事务。
项目的常用的方法:查询方法!
(xml实现事务配置)项目演示步骤:
1.创建spring_tx_xml项目,导入依赖
2.编写AccountDao接口和实现
3.编写AccountService接口和实现
4.编写applicationContext.xml(事务控制)
5.编写测试
在applicationContext.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:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.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">
<!-- 读取jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 1.数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 2.创建JdbcTemplate,注入数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3.创建Dao对象,注入JdbcTemplate -->
<bean id="accountDao" class="com.huihui.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 4.创建Service对象,注入Dao对象 -->
<bean id="accountService" class="com.huihui.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- Spring事务管理- XML配置方式 -->
<!-- 1. 创建事务管理器(TransactionManager) :导入Spring提供的事务控制实现代码(commit,rollback方法) -->
<!--
JDBC事务控制: getDataSource().getConnection().setAutoCommit(false)
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 2. 配置事务通知 (创建执行切面的通知,关联事务管理器) -->
<!--
id: 给事务通知起一个别名,切面引用
transaction-manager: 关联事务管理器
tx:method: 配置事务特征(隔离级别,传播行为)
name:对哪些方法添加事务特征
isolation: 设置事务的隔离级别
propagation: 设置事务的传播行为 , 默认值REQUIRED
read-only: 只读事务,在该方法中不能修改行为,如果执行修改行为,报错
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--<tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true"/>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"/>-->
<!-- 对所有find开头的查询方法设置事务特征 -->
<tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true"/>
<!-- 对所有除了find开头的方法(增删改方法)设置事务特征 -->
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切面 = 通知+切入点-->
<aop:config>
<!-- 3.1 配置切入点 -->
<aop:pointcut id="pt" expression="execution(* com.huihui.service.impl.*ServiceImpl.*(..))"/>
<!--
advisor: Spring设计用于配置事务切面的标签
advice-ref: 引用事务通知
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
测试
ackage com.huihui.test;
import com.huihui.pojo.Account;
import com.huihui.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("classpath:applicationContext.xml")
public class Demo1 {
@Autowired
private AccountService accountService;
@Test
public void test1(){
Account account = new Account();
account.setUid(46);
account.setMoney(4000D);
accountService.save(account);
}
}
(xml+注解实现事务配置)项目演示步骤:
要在业务类上添加:@Transactional注解
账户Service实现
* @Transactional 注解的使用
* 1.位置问题
* 1.1 业务类上: 对类的所有方法添加事务控制(推荐使用)
* 1.2 业务类的方法上: 对某个类的方法添加事务控制
* 1.3 业务接口上: 对接口声明的所有方法添加事务控制
* 1.3 业务接口的方法上: 对接口声明的指定方法添加事务控制
*
* 注意:类和方法同时存在,优先使用方法上的
* 2.属性问题
* transactionManager: 引用的事务管理器的id
* 默认@Transactional根据类型进行注入
* 如果有多个类型才会按照名称进行注入
* value: 和transactionManager的值一样的
*
* isolation: 设置事务的隔离级别
* propagation: 设置事务的传播行为
* rollbackFor: (理论)针对某些异常进行事务回滚 (实际测试,所有异常都可以进行事务控制 )
* noRollbackFor: 针对某些异常不进行事务回滚 (实际测试是OK的)
*/
@Transactional(isolation = Isolation.DEFAULT,
propagation = Propagation.REQUIRED,
noRollbackFor = NullPointerException.class ) // 事务注解
public class AccountServiceImpl implements AccountService{
applicationContext.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:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.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">
<!-- 读取jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 1.数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 2.创建JdbcTemplate,注入数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3.创建Dao对象,注入JdbcTemplate -->
<bean id="accountDao" class="com.huihui.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 4.创建Service对象,注入Dao对象 -->
<bean id="accountService" class="com.huihui.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- Spring事务管理- 注解配置方式 -->
<!-- 注意:不管是XML还是注解,或者编程式,都必须要配置事务管理器!!! -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解扫描 : 不需要设置扫描包,自动整个项目扫描 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
测试代码:
import com.huihui.pojo.Account;
import com.huihui.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("classpath:applicationContext.xml")
public class Demo1 {
@Autowired
private AccountService accountService;
@Test
public void test1(){
System.out.println("代理对象:"+accountService.getClass());
Account account = new Account();
account.setUid(46);
account.setMoney(4000D);
accountService.save(account);
}
}
(纯注解实现事务配置)项目演示步骤:
配置类:
/**
* 配置类
*/
@Configuration
@ComponentScan(basePackages = "com.huihui")
@EnableTransactionManagement // 开启事务管理注解扫描,代替:<tx:annotation-driven/>
@Import(JdbcConfig.class) //导入数据源配置类
public class SpringConfig {
}
数据源配置类:
/**
* 数据源配置类
*/
@PropertySource("jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 创建数据源
*/
@Bean("dataSource") // 放入Spring的IOC容器
public DataSource createDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setDriverClassName(driver);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
/**
* 创建JdbcTemplate
* @Qualifier("dataSource") DataSource dataSource: 在SpringIOC容器查询一个dataSource的对象,注入进来
*/
@Bean("jdbcTemplate")
public JdbcTemplate createJdbcTemplate(@Qualifier("dataSource") DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
/**
* 创建事务管理器
*/
@Bean("transactionManager")
public DataSourceTransactionManager createTransactionManager(@Qualifier("dataSource") DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}