您的位置:首页 > 编程语言 > Java开发

spring(JDBC)事务配置

2016-02-14 16:24 736 查看
实现原理

采用spring AOP技术实现

spring事务的架构



事务的定义



事务的状态



说明:通过spring的事务处理架构,再通过配置文件具体的实现事务的类,就可以让
spring容器知道是什么样的技术来操作数据库,通过对事务状态的判断,通过事务的
定义就可以知道具体的目标方法采用什么样的事务策略来处理了。


/**
* dao 接口定义
* @author w7
*
*/
public interface StudentDao {
public void saveStudent(String sql);
}


/**
* dao接口实现
*
*  继承JdbcDaoSupport ,使用jdbc模板编程
* @author w7
*
*/
public class StudentDaoImpl extends JdbcDaoSupport implements StudentDao{
public void saveStudent(String sql) {
this.getJdbcTemplate().execute(sql);
}
}


/**
* service 接口定义
* @author w7
*
*/
public interface StudentService {
public void saveStudent();
}


/**
* service 接口实现
* @author w7
*
*/
public class StudentServiceImpl implements StudentService{

//dao
private StudentDao studentDao;

public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}

//事务测试
public void saveStudent() {
this.studentDao.saveStudent("insert into person(name,age) values('1',1)");
// int a = 1/0;
this.studentDao.saveStudent("insert into person(name,age) values('2',2)");
}
}


//spring 事务配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--
引入dataSource
把dao层和service层的类导入进来
-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>

<!--
数据库连接源
-->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!--
dao对象
-->
<bean id="studentDao" class="com.itheima09.spring.jdbc.transaction.dao.StudentDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>

<!--
service对象
-->
<bean id="studentService" class="com.itheima09.spring.jdbc.transaction.service.StudentServiceImpl">
<property name="studentDao">
<ref bean="studentDao"/>
</property>
</bean>

<!--
事务管理器
告诉spring容器要采用什么样的技术处理事务
使用哪个技术,则实例化 哪个技术的事务管理器
-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>

<!--
配置声明的事务策略
id  唯一标示
transaction-manager  事务管理器

事务策略理解:
1.spring事务的结构

2.spring事务的定义
1.事务的传播
2.事务的隔离
3.事务的只读

3.事务的状态
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!--
以save开头的方法,

propagation :采用的传播属性是默认值,
isolation:隔离机制是默认值,
read-only:是读写事务
-->
<tx:method
name="save*"
propagation="REQUIRED"
isolation="DEFAULT"
read-only="false"/>
</tx:attributes>
</tx:advice>

<!--
AOP配置
-->
<aop:config>
<aop:pointcut
expression="execution(* com.itheima09.spring.jdbc.transaction.service.*.*(..))"
id="perform"/>

<!--
直接配置事务的通知
-->
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans>


@Test
public void testStudent(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService =
(StudentService)context.getBean("studentService");
//事务测试
studentService.saveStudent();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: