`
zhoujiangzi
  • 浏览: 92747 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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的过程

 

 

分享到:
评论

相关推荐

    spring整合hibernate示例代码

    标题"spring整合hibernate示例代码"提示我们,我们将讨论如何在实际项目中结合这两个框架。Spring可以帮助管理Hibernate的SessionFactory和Transaction,提供声明式事务管理,以及通过AOP(面向切面编程)实现更灵活...

    Spring整合Hibernate.jar

    标题"Spring整合Hibernate.jar"意味着我们将讨论如何将这两个强大的框架集成在一起,以便在Spring管理的环境中使用Hibernate进行数据库操作。这通常涉及到以下步骤和知识点: 1. **引入依赖**:首先,你需要在项目...

    spring整合hibernate实现事务处理

    2. **Spring整合Hibernate实现事务处理 - XML方式** 对于不熟悉注解或者需要更细粒度控制的开发者,Spring也支持XML配置方式来管理事务。首先,需要在配置文件中定义事务管理器,例如`HibernateTransactionManager...

    Spring+hibernate整合源代码

    这个“Spring+hibernate整合源代码”应该包含了实现上述整合步骤的示例代码,可以作为学习和参考的资源。通过学习和实践这些代码,你可以更好地理解和掌握 Spring 和 Hibernate 整合的细节,提升你的 Java Web 开发...

    spring整合hibernate的jar包

    标题中的“spring整合hibernate的jar包”指的是在Java Web开发中,Spring框架与Hibernate持久层框架的集成。这两个框架结合使用可以提供强大的数据访问和业务逻辑处理能力。Spring作为一个全面的轻量级框架,提供了...

    Spring与Hibernate整合

    SSH整合,即Struts、Spring和Hibernate的集成,是Java Web开发中常见的一种技术栈,它能有效地解决MVC模式下的业务逻辑处理和数据持久化问题。本篇将详细介绍Spring3.3与Hibernate2.1整合的过程及其相关知识点。 ...

    Spring整合Hibernate案例

    在本文中,我们将深入探讨如何将Spring框架与Hibernate ORM(对象关系映射)框架整合,以便在实际项目中实现高效、灵活的数据访问层。Spring以其强大的依赖注入和管理能力,结合Hibernate的数据库操作功能,可以构建...

    spring整合hibernate所用相关jar包

    本文将深入探讨Spring整合Hibernate的相关知识点,适合初学者入门。 首先,我们需要理解Spring的核心理念。Spring框架提供了一个轻量级的容器,它能够管理应用程序中的对象,包括初始化、配置和依赖注入。AOP则是...

    spring-hibernate.jar

    spring-hibernate.jar

    Spring3整合Hibernate4测试Demo

    **Spring3整合Hibernate4测试Demo** 在Java Web开发中,Spring和Hibernate是两个非常重要的框架。Spring是一个全面的后端应用程序框架,它提供了依赖注入、AOP(面向切面编程)、MVC(模型-视图-控制器)以及其他...

    spring mvc + spring + hibernate 全注解整合开发视频教程 11

    在本教程中,我们将深入探讨如何使用Spring MVC、Spring和Hibernate三大框架进行全注解的整合开发。这个视频教程系列的第11部分,重点可能是建立在前几部分的基础之上,进一步深化对这三个核心技术的理解和实践。 ...

    使用spring整合hibernate和struts时所要用到的所有jar包

    - **配置文件**:`hibernate.cfg.xml`, `spring-config.xml`, `struts.xml`,这些文件分别定义了Hibernate、Spring和Struts2的配置信息。 - **事务管理**:Spring提供编程式和声明式事务管理,推荐使用声明式事务,...

    spring整合hibernate实例

    这篇名为"spring整合hibernate实例"的内容,显然是关于如何将这两个框架协同工作,构建一个高效、灵活的Java应用的教程。在整合过程中,我们将探讨以下几个关键知识点: 1. **Spring的ApplicationContext**: 这是...

    最新版本的Struts2+Spring4+Hibernate4框架整合

    整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...

    Spring,Hibernate整合源码

    2. **SessionFactory的管理**:Spring可以管理Hibernate的SessionFactory,通过`org.springframework.orm.hibernate5.LocalSessionFactoryBean`进行配置,这样可以避免在代码中直接创建SessionFactory,提高可维护性...

    struts2+spring+hibernate 整合的jar包

    6. **整合Hibernate和Spring**:使用Spring的Hibernate支持,配置SessionFactory,可以通过`&lt;bean&gt;`标签创建SessionFactory并注入到需要的地方。 7. **测试**:编写单元测试和集成测试,验证SSH整合是否成功,确保...

    Spring整合HIbernate

    《Spring整合Hibernate实战指南》 在Java开发领域,Spring框架以其强大的依赖注入、AOP(面向切面编程)以及丰富的模块支持,成为了企业级应用开发的首选。而Hibernate作为持久层框架,以其对象关系映射(ORM)能力...

    spring与hibernate的整合

    Spring 和 Hibernate 的整合是企业级 Java 应用开发中的常见实践,它将 Spring 框架的控制反转(IoC)和面向切面编程(AOP)优势与 Hibernate 的持久化能力相结合,提供了更高效、灵活的数据库操作。下面我们将深入...

    Spring整合Hibernate示例

    Spring整合Hibernate配置测试示例

    spring+hibernate整合demo

    在"标签"中,"spring 整合 hibernate helloworld"进一步强调了这是关于Spring与Hibernate整合的基础教学,可能包含了一个简单的入门级示例。"helloworld"通常表示最基础的程序,用于展示框架的基本用法。 在"压缩...

Global site tag (gtag.js) - Google Analytics