您的位置:首页 > 编程语言 > Java开发

Spring2.5+Struts2+Hibernate3整合之六

2009-05-17 14:57 309 查看
转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广
该篇主要修改spring配置文件applicationContext.xml实现spring整合hibernate的另一种配置。
Spring2.5+Hibernate3的整合过程中,把hibernate sessionFactory和mappingResources相关的配置直接在spring配置文件applicationContext.xml中以bean的形式进行了配置。
其实可以在hibernate配置文件hibernate.cfg.xml中编写sessionFactory和mappingResources相关的配置,然后在spring配置文件中引用hibernate.cfg.xml
src下创建hibernate配置文件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">
<hibernate-configuration>
<session-factory>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="connection.url">jdbc:mysql://localhost:3306/vote?characterEncoding=utf-8</property>
	
	<property name="connection.username">root</property>
	<property name="connection.password">zyg</property>
	<property name="dialect">
		org.hibernate.dialect.MySQL5Dialect
	</property>
	<!-- 连接池启动时的初始值 -->
	<property name="initialSize">1</property>
	<!-- 连接池的最大值 -->
	<property name="maxActive">500</property>
	<!-- 最大空闲值,当经过一个高峰期后,连接池可以慢慢将已经用不到的连接进行释放,一直达到maxIdel为止 -->
	<property name="maxIdle">2</property>
	<!-- 最小空闲值,当空闲的连接小于阀值时,连接池会去预审一些连接,以免洪峰来时来不及去申请 -->
	<property name="minIdle">1</property>
	<property name="show_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="hibernate.format_sql">true</property>
	<mapping resource="com/zyg/ssh/bean/Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

修改spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd            http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 	 <context:annotation-config/>
        
    <bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:/hibernate.cfg.xml</value>
		</property>		
	</bean>
	
	<bean id="txManger" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	<tx:annotation-driven transaction-manager="txManger"/>
	
  	<bean id="studentDao" class="com.zyg.ssh.dao.hibernate.StudentDaoImpl">
  		<property name="sessionFactory" ref="sessionFactory"/>
  	</bean>
  	
  	<bean id="studentService" class="com.zyg.ssh.service.impl.StudentServiceImpl">
  		<property name="studentDao" ref="studentDao"></property>
  	</bean>
</beans>


[b]至此,在
spring配置文件中引入hibernate配置文件的整合方式介绍完毕。

有兴趣的读者请关注后续Spring2.5+Struts2+Ibaits整合的系列文章。
[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: