5.Spring+Struts+Hibernate配置文件整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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配置文件整合的主要内容,如果未能解决你的问题,请参考以下文章
Struts + Spring + Hibernate 集成
Struts+Hibernate+Spring面试题合集及答案
struts2集成Spring,Hibernate的问题!!