spring(JDBC)事务配置
来源:互联网 发布:js 延迟1s 编辑:程序博客网 时间:2024/05/17 06:18
实现原理:
采用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(); }
0 0
- spring配置JDBC事务
- spring(JDBC)事务配置
- spring jdbc事务配置demo
- spring 同时配置hibernate and jdbc 事务
- spring 同时配置hibernate and jdbc 事务
- spring jdbc 事务配置----TransactionTemplate使用
- 在spring中使用JDBC事务配置
- spring 同时配置hibernate and jdbc 事务
- spring 同时配置hibernate and jdbc 事务
- 【Spring】在Spring框架下使用注解配置JDBC事务
- 使用注解方式配置spring中的jdbc事务
- 配置Spring JDBC的事务回滚失效
- Spring JDBC-使用XML配置声明式事务
- Spring JDBC-使用注解配置声明式事务
- 配置JDBC事务\配置hibernate事务
- spring JDBC事务使用经验
- spring jdbc 编程式事务
- spring jdbc 声明式事务
- 观察者模式及Java实现例子
- spring事务传播属性和隔离级别
- 数据结构--单链表
- Xamarin.Android 如何使用圆形Image
- usaco 1.2 complete search
- spring(JDBC)事务配置
- Android 任务栈、Intent.FlAG_ACTIVITY_xxx、Activity四种启动模式
- SpringBoot定时任务说明
- 复杂可变数组的排序---(数组中存的是对象)---mutablearray
- Could not build module 'UIKit'问题
- hdu3585 maximum shortest distance
- mongodb学习笔记
- 自定义Dialog时,会遇到dialog wrap_content,这个时候需要手动设置Dialog大小
- 1、字符串的操作