SSH三大框架整合

SSH三大框架整合

头文件

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

applicationContext.xml

<?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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

</beans>

Struts 和 Spring 的整合

web.xml 中完成对 Struts 和 Spring 的整合。在web-app标签中添加代码。

<!-- 让spring随着web项目启动而启动 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 读取配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 扩大session范围,一定要在struts前 -->
  <filter>
  	<filter-name>openSession</filter-name>
  	<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>openSession</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 让struts启动 -->
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

Spring 和 Hibernate 的整合

applicationContext.xml

<?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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql:///ssh_forum"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	
	<!-- 配置sessionFactory -->
	<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<!-- 方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
				<!-- 生成表的策略 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 是否生成语句 -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 是否格式化生成语句 -->
				<prop key="hibernate.format_sql">true</prop>
			</props>
			
			
		</property>
		
		<property name="mappingDirectoryLocations" value="classpath:com/zsj/domain"></property>
	</bean>
	
	<!-- 配置事务的核心管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 通知 -->
	<tx:advice id="advice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
	<!-- 织入 -->
	<aop:config>
		<!-- 切入点 -->
		<!-- 第一个* 代表public之类的,然后来到com.zsj.service包下的所有的service的类下的所有方法,所有方法下的参数 -->
		<aop:pointcut expression="execution(* com.zsj.service.*.*(..))" id="pc" />	
		<!-- 配置切面 -->
		<aop:advisor advice-ref="advice" pointcut-ref="pc"/>
	</aop:config>

</beans>

映射文件hbm.xml详解

user.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- package:实体所在的包 -->   
<hibernate-mapping package="com.zsj.domain">
	<!-- name:对应的类  table对应数据库里的表的名字 -->
	<class name="User" table="user">
		
		<id name="id">
			<!-- 主键生成策略 -->
			<generator class="uuid"></generator>
		</id>
		<!-- <property >包含两个属性,name表示类中的成员变量column表示对应的数据库表中的相对应的字段名。剩下的就是hibernate对应的type类型,和字段长度 -->
		<property name="username" column="username"></property>
		<property name="password" column="password"></property>
		<property name="name" column="name"></property>
		<property name="email" column="email"></property>
		<property name="telephone" column="telephone"></property>
		<property name="state" column="state"></property>
		<property name="code" column="code"></property>
		<property name="iamge" column="iamge"></property>
		<property name="level" column="level"></property>
		<property name="coin" column="coin"></property>
		
	</class>
	
</hibernate-mapping>

具体看这篇博文
https://blog.csdn.net/qq_33322074/article/details/80173709

struts.xml

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 开启动态方法调用 -->
	<constant name="struts.devMode" value="true"></constant>
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
	
	<package name="SSH_Forum" namespace="/" extends="struts-default">
		<!-- 允许全部方法 -->
		<global-allowed-methods>regex:.*</global-allowed-methods>
		<action name="UserAction_*" class="com.zsj.web.UserAction">
			<result name="toLogin" type="redirect">/login.jsp</result>
		</action>
	</package>

</struts>

配置spring

要让spring帮我创建UserService和UserDao,要现在UserAction和UserService中为其创建get和set方法。

<!-- 配置Action -->
	<bean name="userAction" class="com.zsj.web.UserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
	
	<!-- 配置Service -->
	<bean name="userService" class="com.zsj.service.UserService">
		<property name="userService" ref="userDao"></property>
	</bean>
	
	<!-- 配置Dao -->
	<bean name="userDao" class="com.zsjdao.UserDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

告诉struts,不同自己创建Action了,spring帮他创建

回到struts.xml中加上这段代码

<constant name="struts.objectFactory" value="spring"></constant>

这样spring就配置好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值