spring 事务操作 声明方式
来源:互联网 发布:台湾2016进出口数据 编辑:程序博客网 时间:2024/06/03 08:35
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/kaoqin"></property> <property name="username" value="root"></property> <property name="password" value="1234"></property> </bean> <!--声明方式--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dateSource"></property> </bean> <bean id="userDao1" class="com.sanmao.dao.UserDao1"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="userDao2" class="com.sanmao.dao.UserDao2"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--=============================================--> <!--声明方式配置多个类--> <bean id="user1" parent="transactionProxy"> <property name="target" ref="userDao1"></property> </bean> <bean id="user2" parent="transactionProxy"> <property name="target" ref="userDao2"></property> </bean> <!--===================================================--> <!--声明方式需要配置代理--> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"></property> <!--目标 === 要管理的bean 要处理多个bean的时候,将不这样,而是把代理设置为父类 目标 bean 在子类中声明--> <!--<property name="target" ref="userDao1"></property>--> <!--事物的传播属性--> <property name="transactionAttributes"> <props> <!--有事物就调用事物,否则就新建一个事物--> <prop key="add">PROPAGATION_REQUIRED</prop> <prop key="update">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
public class UserDao1 { JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void add(){ jdbcTemplate.update("insert into sanmao (name,pass) VALUES ('u1','u1')"); } public void update(){ jdbcTemplate.update("insert into sanmao (name,pass) VALUES ('2','2')"); }}
public class UserDao2 { JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void add(){ jdbcTemplate.update("insert into sanmao (name,pass) VALUES ('u2','u2')"); }}
public class test { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml"); UserDao1 userDao=(UserDao1)applicationContext.getBean("userDao1"); userDao.add(); UserDao2 userDao2=(UserDao2)applicationContext.getBean("userDao2"); userDao2.add(); }}
0 0
- spring 事务操作 声明方式
- Spring声明式事务2.0方式
- spring配置声明式事务--注解方式
- Spring配置声明式事务--注解方式
- Spring声明式事务的配置方式
- Spring 基于注解方式声明式事务
- spring 事务操作 编程方式
- 关于Spring声明性事务的几种定义方式
- 关于Spring声明性事务的几种定义方式
- Spring 声明式事务的配置方式(一)
- Spring 声明式事务的配置方式(二)
- Spring 声明式事务的配置方式(三)
- Spring 声明式事务的配置方式(四)
- Spring 声明式事务的配置方式(五)
- Spring 声明式事务的配置方式(六)
- Spring声明式事务的四种方式
- Spring声明式事务(Annotation注解方式)
- Spring声明式事务(AOP的tx方式)
- 接口回调机制详解
- 抽象类
- LogStash 常用配置文件
- EtherCAT ET1100从站信息接口介绍
- Java enum
- spring 事务操作 声明方式
- Adroid软键盘处理
- Tengine
- SQLServer的分页优化使用Row_Number()分页存在的问题
- android app 构建过程
- 常用排序算法
- HDU1072
- Sublime Text 3 安装Package Control
- 写得一手好代码,先学会设计模式