日期:2014-05-20  浏览次数:20942 次

ibati整合spring的事物管理问题
1、public class TestClass extends TestCase {
  public void testInsertAll(){
  Client client = new Client();
client.insertAll();
  }
}


2、
public class Client {
ApplicationContext factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");

public void insertStudent() {
StudentDao studentDao = (StudentDao) factory.getBean("studentDao");
Student student = new Student();
student.setEndTime("lishipeng");
student.setName("lishipeng");
student.setSex("lishipeng");
student.setStartTime("lishipeng");
studentDao.insertStudent(student);
}

public void insertFather() {
FatherDao fatherDao = (FatherDao) factory.getBean("fatherDao");
Father father = new Father();
father.setEndTime("lishipeng");
father.setName("lishipeng");
father.setSex("lishipeng");
father.setStartTime("lishipeng");
fatherDao.insertFather(father);
}

public void insertAll() {
insertFather();
insertStudent();
}




}

3、<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-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">

<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>configfile/jdbc.properties</value>
</property>
</bean>

<!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>

<!-- Spring声明式事务管理 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="select*" read-only="true" />