67,512
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="sys"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Person.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save" propagation="REQUIRED" />
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* business.*.*(..))" advice-ref="txAdvice"/>
</aop:config>
<bean id="person" class="business.PersonImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
package business;
import org.hibernate.SessionFactory;
public interface Person {
public void save(entity.Person person);
public void delete(entity.Person person);
}
import org.springframework.orm.hibernate3.HibernateTemplate;
import entity.Person;
public class PersonBusiness {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");
Person person = new Person();
p.save(person);
p.delete(person);
}
}
package business;
import org.hibernate.SessionFactory;
public class PersonImpl implements Person {
private SessionFactory sf;
public void setSessionFactory(SessionFactory sf) {
this.sf = sf;
}
public void save(entity.Person person) {
sf.getCurrentSession().save(person);
}
public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}
}
public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}
public void save(entity.Person person) {
sf.getCurrentSession().save(person);
delete();//假如你在这里调用delete方法,那在执行时他们是同一个事务
}
ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");
Person person = new Person();
p.save(person);
加载中
试试用AI创作助手写篇文章吧