`

SpringMVC4整合Hibernate3

 
阅读更多

记录一下Spring4(4.2.5)整合Hibernate3。虽然现在已经有Hibernate5的稳定版了,但是一直使用的是Hibernate3。

 

下载Spring的release发布包,包含Spring的应用大杂烩文档,但文档例子可能久未更新,未必就能正确运行。

 

基本SpringMVC Web项目整合Hibernate3用到的jar包(加入c3p0数据库连接池支持)(虽然Maven被极力推广,依赖库配置比较精准,但是很多环境下没有访问外部库的优越条件,各种下载,各种框架和包找精准的支持文档也不是那么容易,搭建个基本框架目也比较费时间):

c3p0-0.9.1.jar
commons-collections-3.2.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-validator.jar
hibernate3.jar
javassist-3.20.0-GA.jar
jstl-1.2.jar
jta-1.1.jar
mysql-connector-java-5.1.6-bin.jar
slf4j-api-1.7.7.jar
slf4j-simple-1.7.7.jar
spring-aop-4.2.5.RELEASE.jar
spring-beans-4.2.5.RELEASE.jar
spring-context-4.2.5.RELEASE.jar
spring-core-4.2.5.RELEASE.jar
spring-expression-4.2.5.RELEASE.jar
spring-jdbc-4.2.5.RELEASE.jar
spring-orm-4.2.5.RELEASE.jar
spring-tx-4.2.5.RELEASE.jar
spring-web-4.2.5.RELEASE.jar
spring-webmvc-4.2.5.RELEASE.jar

 

hibernate3的有关数据源及ORM配置hibernate.cfg.xml内容略、具体的持久化数据库对象略。

 

与在Struts2中静态方法初始化并被调用不同,这里的SessionFactory是交给Spring框架来初始化并自动注入为DAO实现类的属性的,Spring上下文配置文件:

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xsi:schemaLocation="
    	http://www.springframework.org/schema/mvc 
    	http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
       "> 
	
	<context:component-scan  base-package="com.merrick.control" />

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>	
	</bean>
	
	<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"   />
	</bean>
	
	<bean id="mybasedao" class="com.merrick.db.BaseHibernateImpl">
		<property name="sessionFactory" ref="mySessionFactory"/>
	</bean>
	      
</beans>

 这里没有配置为Spring托管数据库事务,还是在DAO实现类中手动提交事务,并且无需手动关闭session:

	private Session session = null;
	private Transaction transaction = null;

	private SessionFactory sessionFactory;		

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	//插入
	public int insert(Object object) {		
		int returnvalue;			
		try {			
			session = this.sessionFactory.getCurrentSession();
			transaction = session.beginTransaction();	
			session.persist(object);
			transaction.commit();
			returnvalue =1;			
			
		} catch (HibernateException e) {		
			returnvalue = 0;
		} finally {
		}		

		return returnvalue;
	}

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics