5.Spring+Struts+Hibernate配置文件整合
一:配置文件整合SSH
1.創建Hibernate實體類的映射文件,一般在resource下建文件夾下放置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--創建映射文件-->
<hibernate-mapping>
<class name="cn.yunhe.model.User" table="user">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="pwd" type="java.lang.String"/>
<property name="age" type="java.lang.Integer"/>
</class>
</hibernate-mapping>
2.Struts.xml文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!--struts方法配置-->
<struts>
<constant name="struts.devMode" value="true"/>
<!--允許動態方法調用-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="my" extends="struts-default">
<action name="user" class="userAction">//在Spring文件中創建的bean
<result>/list.jsp</result>
</action>
</package>
</struts>
3.Spring文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--數據源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root"/>
<property name="password" value="076634"/>
</bean>
<!--創建session工廠-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>hbm/User.hbm.xml</value>
</list>
</property>
</bean>
<!--定義事物管理器(聲明事物)-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--持久層-->
<bean id="userDao" class="cn.yunhe.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--業務層-->
<bean id="userBiz" class="cn.yunhe.biz.UserBizImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--控制層-->
<bean id="userAction" class="cn.yunhe.controller.UserController">
<property name="userBiz" ref="userBiz"/>
</bean>
</beans>
4.web.xml的配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--Struts的過濾器-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring的監聽器,在系統啟動的時候創建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
5.Spring+Struts+Hibernate配置文件整合