spring+struts2+hibernate整合

spring+struts2+hibernate整合

web.xml需要配置

spring+struts2+hibernate整合
   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
   </context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring+struts2+hibernate整合

db.properties

spring+struts2+hibernate整合
jdbc.user=root
jdbc.password=tiger
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///ssh jdbc.initPoolSize=5
jdbc.maxPoolSize=10
spring+struts2+hibernate整合

applicationContext.xml

spring+struts2+hibernate整合
<context:property-placeholder location="classpath:db.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/my/ssh/entities/*.hbm.xml"></property>
</bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="isValidate" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution(* com.my.ssh.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
spring+struts2+hibernate整合

applicationContext-beans.xml

  配置各种bean  注意:对于Struts2 的Action类  scope 需要配置为prototype (不为单例)

<bean id="employeeAction" class="com.my.ssh.actions.EmployeeAction" scope="prototype">
  <property name="employeeService" ref="employeeService"></property>
  <property name="departmentService" ref="departmentService"></property>
</bean>

hibernate.cfg.xml

spring+struts2+hibernate整合
<hibernate-configuration>
<session-factory>
    <!-- 配置基本信息-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 二级缓存 --> </session-factory>
</hibernate-configuration>
spring+struts2+hibernate整合

struts.xml

<!-- 只需要把class 配置为 在applicationContext-beans.xml 文件中Action所指向的id-->
<action name="emp-*" class="employeeAction" method="{1}"> </action>
上一篇:Android 自定义光标样式


下一篇:SAPCAR 压缩解压软件的使用方法