创建Dao层
package blog.csdn.net.mchenys.dao;
import blog.csdn.net.mchenys.domain.Customer;
public interface CustomerDao {
void save(Customer c);
}
package blog.csdn.net.mchenys.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import blog.csdn.net.mchenys.dao.CustomerDao;
import blog.csdn.net.mchenys.domain.Customer;
//dao层,继承HibernateDaoSupport
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public void save(Customer c) {
//使用hibernate模板操作数据库
this.getHibernateTemplate().save(c);
}
}
修改service层
package blog.csdn.net.mchenys.service.impl;
import org.springframework.transaction.annotation.Transactional;
import blog.csdn.net.mchenys.dao.CustomerDao;
import blog.csdn.net.mchenys.domain.Customer;
import blog.csdn.net.mchenys.service.CustomerService;
//service层,添加事务注解,开启事务管理
@Transactional
public class CustomerServiceImpl implements CustomerService {
//提供set方法注入dao
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
@Override
public void save(Customer c) {
//调用dao层保存用户
customerDao.save(c);
}
}
修改applicationContext.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- web层 -->
<!-- 注册CustomerAction
Spring框架默认生成CustomerAction是单例的,而Struts2框架是多例的。所以需要配置 scope="prototype"
-->
<bean id="customerAction" class="blog.csdn.net.mchenys.action.CustomerAction" scope="prototype">
<!-- 注入属性 -->
<property name="customerService" ref="customerService"/>
</bean>
<!-- dao层 -->
<!-- 编写bean,名称都是固定,加载hibernate.cfg.xml的配置文件获取sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<!-- 配置Hibernate平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启事务的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--注册CustomerDaoImpl -->
<bean id="customerDao" class="blog.csdn.net.mchenys.dao.impl.CustomerDaoImpl">
<!-- 注入sessionFactory属性, HibernateDaoSupport会自动创建HibernateTemplate-->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- service层 -->
<!-- 注册CustomerServiceImpl -->
<bean id="customerService" class="blog.csdn.net.mchenys.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
</beans>
测试
访问添加客户页面
点击提交后,查看数据库是否有该条记录,有的话说明整合成功。