定义测试类: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的过程