struts.xml
applicationContext.xml日志记录
第六步: applicationContext.xml <!-- 配置Service --> <bean id="customerService" class="com.lee.ssh.service.impl.CustomerServiceImpl"> </bean>
struts.xml <struts> <!-- 配置struts2的常量 --> <constant name="struts.action.extension" value="action"></constant> <!-- 配置Action --> <package name="ssh" extends="struts-default" namespace="/"> <action name="customer_*" class="com.lee.ssh.web.action.CustomerAction" method="{1}"> </action> </package> </struts>
在struts.xml引入 <struts> <!-- 配置struts2的常量 --> <constant name="struts.action.extension" value="action"></constant> <!-- 配置Action --> <package name="ssh1" extends="struts-default" namespace="/"> <!-- class="CustomerAction"是applicationContext.xml中的Action的id,引入的就是它 --> <action name="customer_*" class="customerAction" method="{1}"> </action> </package> </struts>
第九步:Service调用DAO 将DAO交给Spring管理 <!-- 配置DAO --> <bean id="customerDao" class="com.lee.ssh.dao.impl.CustomerDaoImpl"> </bean>
package com.lee.ssh.service.impl;class CustomerServiceImpl //注入DAO private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; }
第十步:Spring整合Hibernate框架 编写实体和映射在Spring配置文件中引入Hibernate的配置信息 <!-- Spring整合Hibernate --> <!-- 引入Hibernate的配置信息,并根据classpath:hibernate.cfg.xml生成配置文件======= --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBuilder"> <!-- 引入Hibernate的配置文件 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>
在Spring和Hibernate整合后,Spring提供了一个Hibernate的模板简化Hibernate的开发 改写DAO继承HibernateDaoSupport package com.lee.ssh.dao.impl; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.lee.ssh.dao.CustomerDao; import com.lee.ssh.domain.Customer; import com.lee.ssh.utils.HibernateUtils; public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao { @Override public void save(Customer customer) { System.out.println("CustomerDaoImpl中的save方法执行了"); } }
在DAO中直接注入Session Factory:一旦注入,就会自动创建Hibernate模板类 <!-- 配置DAO --> <bean id="customerDao" class="com.lee.ssh.dao.impl.CustomerDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
在DAO中使用Hibernate的模板完成保存操作 public void save(Customer customer) { System.out.println("CustomerDaoImpl中的save方法执行了"); this.getHibernateTemplate().save(customer); }
第十一步:配置Spring的事务管理 public void save(Customer customer) { System.out.println("CustomerDaoImpl中的save方法执行了"); this.getHibernateTemplate().save(customer); }
开启注解事务
<!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/>在业务层使用注解 @Transactional public class CustomerServiceImpl implements CustomerService {
转载于:https://www.cnblogs.com/spring-lee/p/9636172.html
