Spring学习笔记(六)

我们知道在hibernate中我们为了程序的严谨和规范一般会这样子写与数据库交互的部分代码如下:

Session s = null;
		try {
		    s = sessionFactory.openSession();
			s.getTransaction().begin();
			s.save(user);
			s.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			s.getTransaction().rollback();
		}finally{
			if(s!=null){
				s.close();
				s=null;
			}

这样在每一个交互中都这样写会很麻烦,Hibernate3中提供了一个HibernateTemplate实际上它是一个设计模式,来帮我们解决这样的重复性工作,(Hibernate4中却把它抛弃了)在beans.xml中的配置如下:

<bean id="hibernateTempl" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

我们观察HibernateTemplate类发现它持有sessionFactory并且封装了sessionsave,load等方法,所以原来的代码就变为下面了:

private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate(){
	Return hibernateTemplate;
}
@Resource
public HibernateTemplate setHibernateTemplate(HibernateTemplate hibernateTemplate){
	Return this.hibernateTemplate= hibernateTemplate;
}
public void save(User user){
	hibernateTemplate.save(user);
} 

 

那么HibernateTemplate类中的save方法是怎么实现的呢?源码如下:

public Serializable save(final Object entity) throws DataAccessException {
		return executeWithNativeSession(new HibernateCallback<Serializable>() {
			public Serializable doInHibernate(Session session) throws HibernateException {
				checkWriteOperationAllowed(session);
				return session.save(entity);
			}
		});
	}


 

其中所有异常都会被封装为DataAccessException属于RuntimeException这样就统一处理一种异常。executeWithNativeSession方法原理如下:

先定义一个接口:

import org.hibernate.Session;

public interface MyHibernateCallback {
	public void doInHibernate(Session s);
}

这个doInHibernate接受一个Session参数,利用Session来进行增删改查,当然接口调用者可以根据具体需要去重写这个方法接着去实现一个类来调用这个接口:

import org.hibernate.Session;

public class MyHibernateTemplate {
	public void executeWithNativeSession(MyHibernateCallback callback){
		Session s = null;
		try{
			s = getSession();
			s.beginTransaction();
			
			callback.doInHibernate(s);
			
			s.getTransaction().commit();
		}catch(Exception e){
			s.getTransaction().rollback();
		}finally{
			//...
		}
	}

	private Session getSession() {
		return null;
	}
	
	public static void main(String[]args){
		new MyHibernateTemplate().executeWithNativeSession(new MyHibernateCallback(){
			public void doInHibernate(Session s) {
				s.save(null);
			}
			
		});
	}
}

其中executeWithNativeSession接受一个MyHibernateCallback参数这样不会写死处理程序。当然调用者就需要自己去重写接口的方法。Callback是回调也称钩子函数,很形象描述了其他地方都写好了,然后在这里伸出一个钩子callback.doInHibernate(s)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值