三、SSH整合之Spring整合Hibernate

创建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>

测试

访问添加客户页面
在这里插入图片描述
点击提交后,查看数据库是否有该条记录,有的话说明整合成功。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值