Spring2.5+Struts2+Hibernate3整合之六
2014-07-21 09:47
363 查看
转载请注明:来自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,其代码如下:
[b][xhtml]
view plaincopy[/b]
<?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,修改后的代码如下:
[b][xhtml]
view plaincopy[/b]
<?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配置文件的整合方式介绍完毕。[/b]
作者:张燕广
该篇主要修改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,其代码如下:
[b][xhtml]
view plaincopy[/b]
<?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,修改后的代码如下:
[b][xhtml]
view plaincopy[/b]
<?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配置文件的整合方式介绍完毕。[/b]
相关文章推荐
- Spring2.5+Struts2+Hibernate3整合之五
- Spring2.5+Struts2+ Hibernate3整合之二
- Spring2.5+Struts2+Hibernate3整合之五
- Spring2.5+Struts2+Hibernate3整合之四
- Spring2.5+Struts2+Hibernate3整合之三
- Spring2.5+Struts2+Hibernate3整合之一
- Spring2.5+Struts2+Hibernate3整合之六
- Spring2.5+Struts2+ Hibernate3整合之二
- Spring2.5+Struts2+Hibernate3整合之三
- Spring2.5+Struts2+Hibernate3整合之一
- Spring2.5+Struts2+Hibernate3整合之四
- Struts2+Spring3+Hibernate3整合
- spring2.5+struts2.1.8+hibernate3整合配置
- Spring2.5+Struts2+Ibatis整合之一
- struts2+hibernate3+spring2.5整合配置
- SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- Struts2+Spring2.5+Hibernate3整合
- Spring2.5+Struts2+Ibatis整合之一