Hibernate+spring整合相关

其实,Hibernate+struts或者struts+spring整合,是没什么大问题的,至于为什么讲解spring+Hibernate的整合呢,是因为。里面涉及到Hibernate对spring 的托管的问题!!

所以就在这里唠叨一下了!!

首先呢,还是按顺序,建议先添加对spring的支持,然后再添加对Hibernate的支持!

 

第一步:新建一个web项目

建好项目后,添加相关spring的jar包,至于应该添加哪些包,看你自己需要那些了,在这里不再罗嗦~~

然后,点击右键---->myeclipse------>add  spring capabilities...    然后按要求,一步一步配置即可!

之后,你就会发现,在你src根目录下多出了一个applacationContext.xml文件~~,这就是spring的配置文件了!!

 

接下来,按照上述步骤,添加Hibernate的支持~~~

 

配置完之后,你就会发现,又多出了一个文件hibernate.cfg.xml文件,这是hibernate的配置文件,可在里设置一些配置信息!

这时候,运行项目,没什么错误,就可以建立相关包 了,包怎么命名,在这里不再多说!

 

在这里我们会发现,以前单纯的利用Hibernate来来对数据库进行操作的话,还得建立sessionFactory,打开session,建立transaction等

觉得很麻烦!!!

那么有没有一种可以简化Hibernate的操作呢???答案是必须有啊!!

 

这时候,就需要设置Hibernate对spring的托管了!

 

spring会提供HibernateTemplate,HibernateDaoSupport,HibernateCallback三个API对数据库进行操作!!

至于这三个类都有什么用,大家可以参考《SSH的基础整合相关》博文,在这里不再多说!

 

好了,那我们就开始配置了!!

 

Hibernate.cfg.xml

 

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/firstspring</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.autocommit">true</property><!-- 没有这句话,用hibernateTemplate.save()保存失败 -->
    <mapping resource="com/model/User.hbm.xml"/>
    </session-factory>

</hibernate-configuration>


 

applactionContext.xml

 

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Hibernate托管spring第一步:,加载sessionFactory文件,产生sessionFactory对象 -->
    
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	
    <!-- Hibernate托管spring第二步:,产生HibernateTamplate模板对象,并注入sessionFactory对象 -->
    
    <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
    <!--Transaction事务托管spring第一步:,产生HibernateTransactionManager对象,并注入sessionFactory对象 -->

	<bean name="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<!--事务托管有一点类似AOP的思想,针对类的方法进行事务设置,是其思想的一个延伸-->
	
    <!-- Transaction事务托管spring第二步:生成事务代理bean,并将事务管理Manager注入,并设置属性以及目标类。
          (也可以不设置生成事务代理bean,可以使用TransactionInterceptor来针对多个类进行事务过滤,如:第三步 )-->
          
	 <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
        <property name="transactionAttributes">
        
        <!--这对以下方法设置,设置以get开头的方法为 采用requeired事务策略,并设置为可读--><props>
             <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
             <prop key="*">PROPAGATION_REQUIRED</prop>
             
        </props>
        </property>
        <!--   目标类,就是针对该类里的方法进行事务设置 -->
        <property name="target">
        <ref bean="userdaoimpl"></ref>
        </property>
        </bean>
        
        <!--
         
        
         第三步 
         
        <bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>UserDAO</value>
				
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>
     
   
   -->
   
   
   
   <bean name="userdaoimpl" class="com.daoImpl.UserDaoImpl">
   <property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property>只要继承HibernateDaoSupport,必须将hibernateTemplate进行注入
   </bean>
    <bean name="loginregistserviceimpl" class="com.serviceImpl.LoginRegistServiceImpl">
   <property name="userdao"><ref bean="userdaoimpl"/></property>
   </bean>
   <bean name="mainserviceimpl" class="com.serviceImpl.MainServiceImpl">
   <property name="userdao"><ref bean="userdaoimpl"/></property>
   </bean>
   
</beans>


 在web.xml中添加以下代码

 

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value><!-- 指定applicationContext.xml的位置,是项目启动时,预加载 -->
    </context-param>
    <listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	


 

 

 

 

 

 

 

这就是一些基础配置了~~~

 

添加好之后,就可以利用那三个类进行操作了,

 

 

这里涉及到spring的两大功能:Ioc控制反转(依赖注入)和AOP面向切面思想。

大家可以参考网上相关资料,这里不再多说!!

 

强烈给大家推荐一本SSH整合入门书籍《java web 程序设计与项目实践》,这本书讲的不怎么深入,面向实战!比较适合初学者!看完之后可以看李刚老师的《J2EE轻量级》

这本书讲得很详细!但个人觉得不适合初学者!

 

 

 

 

 

 

 

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值