配置文件配置事务
声明式事务管理
BaseDaoImpl.java类
package com.mingde.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class BaseDaoImpl implements IBaseDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List findAll(String hql) {
Session session=sessionFactory.getCurrentSession();
return session.createQuery(hql).list();
}
@Override
public void save(Object obj) {
Session session=sessionFactory.getCurrentSession();
session.save(obj);
}
@Override
public Object findObjectById(Class<?> class1, int id) {
Session session =sessionFactory.getCurrentSession();
return session.get(class1, id);
}
@Override
public void delete(Object obj) {
Session session =sessionFactory.getCurrentSession();
session.delete(obj);
}
@Override
public void update(Object obj) {
Session session= sessionFactory.getCurrentSession();
session.merge(obj);
}
}
配置文件application.xml中定义声明事务
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="scott" ></property>
<property name="password" value="123" ></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate常用属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
<!-- 配置加载映射文件 -->
<property name="mappingResources">
<list>
<value>com/mingde/po/Bus_company.hbm.xml</value>
<value>com/mingde/po/Bus_route.hbm.xml</value>
<value>com/mingde/po/Bus_emp.hbm.xml</value>
</list>
</property>
</bean>
<!-- 如下进行声明式事务的配置 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- 定义通知 -->
<tx:advice id="myadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 使用AOP配置声明式事务 -->
<aop:config>
<!-- 配置aop切入点,即在哪个类的哪个方法中调用相关的代码(定义调用的位置) -->
<aop:pointcut expression="execution(* com.mingde.dao..*.*(..))" id="myponintcut1"/>
<!-- 定义访问者 -->
<aop:advisor advice-ref="myadvice" pointcut-ref="myponintcut1"/>
</aop:config>
</beans>
dao层bd配置的sessionFactory
可以将bd的sessionFactory配置单独独立出来,但sessionFactory仍然可以引用到applicationContext.xml中定义的sessionFactory
<?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="bd" class="com.mingde.dao.impl.BaseDaoImpl">
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
</beans>
Web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Spring_008_AOP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Struts过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载在classpath目录下的配置文件 (有了这个配置才能加载classpath目录下的applicationContext.xml文件) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application*.xml</param-value>
</context-param>
</web-app>
注解式事务管理
配置文件applicationContext.xml中定义注解式事务
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<!-- 定义注解式事务 -->
<tx:annotation-driven/>
BaseDaoImpl.java中定义注解式事务
package com.minde.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.minde.dao.IBaseDao;
import com.minde.po.Dept;
@Transactional //代表该类中的所有方法都启用事务
public class BaseDaoImpl implements IBaseDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional(readOnly=true) //代表该方法为只读事务,其他方法不写都默认为Proprgation.REQUIRED
@Override
public List findAll(String hql) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery(hql);
return query.list();
}
@Override
public void save(Object obj) {
Session session = sessionFactory.getCurrentSession();
session.save(obj);
}
@Override
public void update(Object obj) {
Session session = sessionFactory.getCurrentSession();
session.merge(obj);
}
@Transactional(readOnly=true) //代表该方法为只读事务,其他方法不写都默认为Proprgation.REQUIRED
@Override
public Object findObjectById(Class clazz, Serializable id) {
Session session = sessionFactory.getCurrentSession();
Object obj = session.get(clazz, id);
return obj;
}
@Override
public void delete(Object obj) {
Session session = sessionFactory.getCurrentSession();
session.delete(obj);
Dept dt = null;
System.out.println(dt.getDcity());
}
}
注意:注解式事务中所有的事务的传播方式都为Proprgation.REQUIRED,在此前面定义@Transactional这个注解,代表此类中的所有方法都使用事务。如果要定义某个方法是只读事务,则需要在其方法上另外重新定义
声明式事务的传播方式
声明式事务的传播方式在org.Springframework.transaction.TransactionDefinition接口下定义,有如下七种: