applicationContext.xml配置
- 开启事务管理器(transactionManager),其子标签property为sessionFactory
- 配置通知,其中查找类方法propagation=“SUPPORTS”,read-only=“true”
- 配置切点切面,expression=“execution(* service.impl..(…))”,切点为service.impl层的任意方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.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 ">
<!-- 读取db.properties文件 -->
<context:property-placeholder
location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 将SessionFactory配置到spring容器中 -->
<bean name="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<!-- 必选配置 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 可选配置 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations"
value="classpath:domain"></property>
</bean>
<!-- 核心事务管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"
read-only="false" />
<tx:method name="find*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="login" propagation="SUPPORTS"
read-only="true" />
<tx:method name="get*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="show*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="select*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*Get*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*Show*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*Find*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*Select*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="checkForStu" propagation="SUPPORTS"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置将通知织入目标对象 配置切点 配置切面 -->
<aop:config>
<aop:pointcut
expression="execution(* service.impl.*.*(..))" id="txPc" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="action" />
<context:component-scan base-package="service.impl" />
<context:component-scan base-package="dao.impl" />
<!-- action -->
<!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
</beans>
serviceImpl方法实现
不需要在手动控制事务
@Override
// 用户登录获取个人信息
public User login(User u) {
User existU = userDao.login(u);
return existU;
}
dao层
使用SessionFactory的getCurrentSession,此时session会自动关闭
private SessionFactory sessionFactory;
public User login(User u) {
// TODO Auto-generated method stub
Session session = sessionFactory.getCurrentSession();
}