浏览 2772 次
锁定老帖子 主题:spring入门实例-DAO,声明事务
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-07-27
spring入门实例-DAO,事务 实例使用代理模式,使用DAO,并且添加事务功能 配置文件:
<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-2.5.xsd"> <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@192.168.32.227:1521:orcl</value> </property> <property name="password"> <value>lpromis</value> </property> <property name="username"> <value>lpromis</value> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> </bean> <bean id="userDAO" class="com.myspring.bussiness.declare.DataSourceDAO"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> </bean> <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target"> <ref bean="userDAO"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans> 使用 org.springframework.transaction.interceptor.TransactionProxyFactoryBean实现代理事务,事务处理类 transactionManager,DAO的bean是userDAO,代理事务属性设置为insert操作, 其中PROPAGATION_REQUIRED,是事务操作属性,还有许多,有兴趣可以自己看看api。
bean的实现:
/** * use declarative transaction * @author logichina * */ public class DataSourceDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplate = new JdbcTemplate(dataSource); } private JdbcTemplate jdbcTemplate; public void insertCustomer() { jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')"); jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname6')"); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/myspring/bussiness/declare/datasourceDAO.xml"); DataSourceDAO bean = (DataSourceDAO) context.getBean("userDAOProxy"); bean.insertCustomer(); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |