Struts+Spring+Hibernate整合

1.导入jar包

Struts的jar包:

Spring的jar包:

Hibernate的jar包:

注意:只保留一个高版本的common-logging.jar包,Struts的jar包中struts2-spring-plugin-2.1.8.jar是必须要导入的,因为其是整合Struts和Spring的一个关键性jar包

2.Hibernate和Spring的整合

建议先整合Spring和Hibernate。

2.1方法一 保留hibernate.cfg.xml文件

Hibernate的hibernate.cfg.xml代码不变,如下示例:

 
  1. <!DOCTYPE hibernate-configuration PUBLIC

  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

  4.  
  5. <!-- Generated by MyEclipse Hibernate Tools. -->

  6. <hibernate-configuration>

  7.  
  8. <session-factory>

  9. <property name="connection.driver_class">

  10. oracle.jdbc.driver.OracleDriver

  11. </property>

  12. <property name="connection.url">

  13. jdbc:oracle:thin:@localhost:1521:orcl

  14. </property>

  15. <property name="connection.username">admin</property>

  16. <property name="connection.password">admin</property>

  17.  
  18. <property name="hibernate.c3p0.max_size">20</property>

  19. <property name="hibernate.c3p0.max_statements">1</property>

  20. <property name="hibernate.c3p0.timeout">1800</property>

  21. <property name="hibernate.c3p0.max_statements">50</property>

  22.  
  23. <property name="hibernate.dialect">

  24. org.hibernate.dialect.Oracle10gDialect

  25. </property>

  26. <property name="show_sql">true</property>

  27. <property name="hibernate.hbm2ddl.auto">update</property>

  28.  
  29. <mapping resource="com/wzm/bean/User.hbm.xml"/>

  30.  
  31. </session-factory>

  32.  
  33. </hibernate-configuration>

Spring的application.xml文件整合Hibernate代码如下:

 
  1. <beans

  2. xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:p="http://www.springframework.org/schema/p"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  6.  
  7. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  8. <property name="configLocation" value="classpath:hibernate.cfg.xml">

  9. </property>

  10. </bean>

  11. </beans>

2.2 方法二 废弃使用hibernate.cfg.xml文件

将数据库的配置放在Spring的配置文件application.xml文件中,代码如下:

 
  1. <beans

  2. xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xmlns:p="http://www.springframework.org/schema/p"

  8. xsi:schemaLocation="http://www.springframework.org/schema/beans

  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  10. http://www.springframework.org/schema/context

  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  12. http://www.springframework.org/schema/tx

  13. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  14. http://www.springframework.org/schema/aop

  15. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  16.  
  17. <context:component-scan base-package="com.wzm"/>

  18.  
  19. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

  20. <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>

  21. <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>

  22. <property name="user" value="admin"/>

  23. <property name="password" value="admin"/>

  24. <property name="maxPoolSize" value="20"/>

  25. <property name="minPoolSize" value="1"/>

  26. <property name="initialPoolSize" value="1"/>

  27. <property name="maxIdleTime" value="20"/>

  28. </bean>

  29. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  30. <property name="dataSource" ref="dataSource"/>

  31. <property name="mappingResources">

  32. <list>

  33. <value>com/wzm/bean/User.hbm.xml</value>

  34. </list>

  35. </property>

  36. <property name="hibernateProperties">

  37. <value>

  38. hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

  39. hibernate.hbm2ddl.auto=update

  40. hibernate.show_sql=true

  41. </value>

  42. </property>

  43. </bean>

  44. </beans>

2.3 使用properties文件提供数据库接口

这里的接口并非Java中的接口,而是指提供可扩展的接入点,properties文件中的代码如下:

 
  1. URL=jdbc:oracle:thin:@localhost:1521:orcl

  2. DRIVER=oracle.jdbc.driver.OracleDriver

  3. USERNAME=admin

  4. PASSWORD=admin

  5.  
  6. #URL=jdbc:jtds:sqlserver://localhost:1433/qhit02

  7. #DRIVER=net.sourceforge.jtds.jdbc.Driver

  8. #USERNAME=sa

  9. #PASSWORD=sa1234

该文件中提供了Oracle11g和SQL Server两种数据库接口(接入点),甚至可以写入更多的数据库的信息,“#”是注销,即其后的代码不可用。
Spring的application.xml中的配置代码如下:

 
  1. <beans

  2. xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xmlns:p="http://www.springframework.org/schema/p"

  8. xsi:schemaLocation="http://www.springframework.org/schema/beans

  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  10. http://www.springframework.org/schema/context

  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  12. http://www.springframework.org/schema/tx

  13. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  14. http://www.springframework.org/schema/aop

  15. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  16.  
  17. <context:component-scan base-package="com.wzm"/>

  18. <context:property-placeholder location="classpath:DBOption.properties"/>

  19.  
  20. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

  21. <property name="driverClass" value="${DRIVER}"/>

  22. <property name="jdbcUrl" value="${URL}"/>

  23. <property name="user" value="${USERNAME}"/>

  24. <property name="password" value="${PASSWORD}"/>

  25. <property name="maxPoolSize" value="20"/>

  26. <property name="minPoolSize" value="1"/>

  27. <property name="initialPoolSize" value="1"/>

  28. <property name="maxIdleTime" value="20"/>

  29. </bean>

  30. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  31. <property name="dataSource" ref="dataSource"/>

  32. <property name="mappingResources">

  33. <list>

  34. <value>com/wzm/persistence/entiy/User.hbm.xml</value>

  35. </list>

  36. </property>

  37. <property name="hibernateProperties">

  38. <value>

  39. hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

  40. hibernate.hbm2ddl.auto=update

  41. hibernate.show_sql=true

  42. </value>

  43. </property>

  44. </bean>

  45. </beans>

3.整合Struts和Spring

整合Struts和Spring时,只需在WebRoot\WEB-INF\web.xml文件中加入Struts的过滤器和Spring的监听器即可,代码如下:

 
  1. <filter>

  2. <filter-name>struts2</filter-name>

  3. <filter-class>

  4. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

  5. </filter-class>

  6. </filter>

  7. <filter-mapping>

  8. <filter-name>struts2</filter-name>

  9. <url-pattern>/*</url-pattern>

  10. </filter-mapping>

  11.  
  12. <!-- 配置监听器 -->

  13. <context-param>

  14. <param-name>contextConfigLocation</param-name>

  15. <param-value>

  16. classpath:application.xml

  17. <span style="background-color: rgb(255, 0, 0);">(注意:配置多个Spring Bean文件时,这样写classpath:applicationContext.xml,classpath:bean.xml)</span>

  18. </param-value>

  19. </context-param>

  20. <listener>

  21. <listener-class>

  22. org.springframework.web.context.ContextLoaderListener

  23. </listener-class>

  • </listener>

猜你喜欢

转载自blog.csdn.net/chengxuyuan_110/article/details/81119133