spring 整合hibernate (2)

定义测试类:SpringHibernateTransactionTest

package com.jacksoft.spring.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jacksoft.spring.hibernate.model.UserModel;

/**
 *  spring管理事务
 * @author Jack
 *
 */
public class SpringHibernateTransactionTest {

	private SessionFactory sessionFactory;
	
	@Before
	public void initSessionFactory(){
		ApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml");
		sessionFactory = context.getBean("sessionFactory", SessionFactory.class);
	}
	
	/**
	 *  
	 */
	@Test
	public void testSessionTransaction(){
		Session session = sessionFactory.openSession();
		Transaction tr = null;
		try{
			tr = session.beginTransaction();
			UserModel model = new UserModel();
			model.setLevelCode(3);
			model.setUsername("spring");
			model.setPassword("a111");
			session.save(model);
			System.out.println("数据已经存储");
			tr.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			tr.rollback();
		}finally{
			tr.begin();
			session.close();
		}
	}
}

 从上面能发现,开启事务,提交事务,回滚事务都需要自己来操作,如下:

Session session = sessionFactory.openSession();
		Transaction tr = null;
		try{
			tr = session.beginTransaction();

			//xxx 操作

			tr.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			tr.rollback();
		}finally{
			tr.begin();
			session.close();
		}

 很显然采用整合spring后不需要这么多步骤,spring为我们提供了hibernate对应的HibernateDaoSupport类来支持数据库的一致访问,类似于JdbcDaoSupport,下面就来实践下:

 1. 定义IDao接口

package com.jacksoft.spring.hibernate.service;

public interface IDao<T> {

	public void save(T t);
}

 2.定义其实现类UserDao

   

package com.jacksoft.spring.hibernate.service.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jacksoft.spring.hibernate.model.UserModel;
import com.jacksoft.spring.hibernate.service.IDao;

public class UserDao extends HibernateDaoSupport implements IDao<UserModel> {

	@Override
	public void save(UserModel t) {
		getHibernateTemplate().save(t);
	}

}

 由于需要获取HibernateTemplate,所以我们需要继承HibernateDaoSupport来获取

3.配置对于bean,将UserDao交给spring管理,因为继承HibernateDaoSupport,所以需要为sessionFactory赋值,这里采用继承的方式,在spring中声明一个抽象类,定义sessionFactory

	<bean id="abstractDao" abstract="true">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="userDao" class="com.jacksoft.spring.hibernate.service.impl.UserDao" parent="abstractDao"/>
	

 

4.创建测试类SpringHibernateDaoSupportTest

package com.jacksoft.spring.hibernate;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jacksoft.spring.hibernate.model.UserModel;
import com.jacksoft.spring.hibernate.service.IDao;

public class SpringHibernateDaoSupportTest {

	@Test
	public void testSave(){
		ApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml");
		IDao<UserModel> userDao = context.getBean("userDao", IDao.class);
		UserModel model = new UserModel();
		model.setLevelCode(4);
		model.setUsername("spring");
		model.setPassword("a111");
		userDao.save(model);
		
	}
}

 

这样就基本完成了spring整合hibernate dao的过程

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值