专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 SSH框架搭建详细步骤

SSH框架搭建详细步骤

更新时间:2022-10-21 10:25:41 来源:动力节点 浏览793次

相信大家对SSH框架原理已经有所了解,SSH框架配置时这几个文件比较重要:Spring,Struts2,hibernate,web.xml。

SSH框架配置第一步:jar包加载

开始配置前只要把SSH需要的所有jar复制到WebRoot下的WEB-INF中的lib目录下。这里已经整合好的所有jar包,下载地址:点击打开链接用这种方法的优点是:既可以在myeclipse用也可以在eclipse中使用,不会出现jar包冲突的事情。

SSH框架配置第二步:hibernate配置

hibernate.cfg.xml文件配置

<span style="font-size:14px;"><?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 配置Hibernate的基本属性 -->
		<!-- 1. 数据源配置到 IOC 容器中, 在这里不需再配置 -->
		<!-- 2. 关联的 .hbm.xml 也在 IOC 容器 配置 SessionFactory 实例时 进行配置 -->
		<!-- 3. 配置 Hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及 二级缓存 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">false</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<property name="hibernate.temp.use_jdbc_metadata_defaults">fals </property>		
		<!-- 配置 Hibernate 二级缓存相关属性 -->
	</session-factory>
</hibernate-configuration>
</span>

db.properties文件配置

<span style="font-size:14px;">jdbc.user=root
jdbc.password=220316
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.initPoolSize=5
jdbc.maxPoolSize=10</span>

这里和传统的SSH配置不一样,目前主流的是把hibernate中关于数据库的相关配置信息给分离到db.properties文件配置中去。

这样做的好处是后期维护比较方法,代码比较简洁不容易出错。

SSH框架配置第三步:Spring配置

applicationContext.xml文件配置

<span style="font-size:14px;"><?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<context:component-scan base-package="com.ge"></context:component-scan>
	<!-- 导入外部资源文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- c3p0方式配置数据源 -->
	<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="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	<!-- 配置Hibernate的SessionFactory实例: 通过 Spring 提供的 LocalSessionFactoryBean 
		进行配置 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 配置数据源属性 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置 Hibernate 配置文件位置 和 名称 -->
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<!-- 配置 Hibernate 映射文件的位置和名称, 可以使用通配符 -->
		<property name="mappingLocations" value="classpath:com/ge/entity/*.hbm.xml"></property>
	</bean>
	<!-- 1. 配置Hibernate事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 2. 配置事务属性, 需要事务管理器 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
	<!-- 3. 配置事务切点 -->
	<aop:config>
		<aop:pointcut expression="execution( * com.ge.biz.imp.*.*(..))" id="txPointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>
	
</beans>
</span>

这里需要改的是:

1.映射文件的位置和名字,因为我测试用的是,这里需要修改的是:com/ge/entity也就是你映射文件所在的包位置。

2.还有就是配置事务切点的位置,我这里的位置是:,这里需要修改的是事务切点位置:com.ge.biz.imp也就是你biz层所在位置。

applicationContext-beans.xml文件配置

<span style="font-size:14px;"><?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">	
	<bean class="com.ge.dao.imp.EquipmentDaoImp" id="EquipmentDaoImp">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean class="com.ge.biz.imp.EquipmentBizImp" id="EquipmentBizImp">
		<property name="equipmentDao" ref="EquipmentDaoImp"></property>
	</bean>
	<bean class="com.ge.action.TestAction" id="testAction" scope="prototype">
		<property name="equipmentBiz" ref="EquipmentBizImp"></property>
	</bean>
</beans>
</span>

applicationContext-beans.xml文件配置配置的重点是:

<bean class="com.ge.dao.imp.EquipmentDaoImp" id="EquipmentDaoImp">
              <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

就是dao层中会话工厂的配置,这个如果不配置那hibernate的功能就都用不了了。

SSH框架配置第四步:Struts2配置

Struts2文件配置

<span style="font-size:14px;"><?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>
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.multipart.saveDir" value="C:/repository"/>  
	<constant name="struts.devMode" value="true" />
	<package name="HY" namespace="/" extends="struts-default">
		<action name="testAction" class="testAction">
			<result name="index">index.jsp</result>
			<result name="success">welcome.jsp</result>
			<result name="fail">fail.jsp</result>
		</action>
	</package>
</struts>
</span>

Struts2中没有什么难点,一般配置出错都不在这里。

SSH框架配置第五步:web.xml配置

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	<!-- 配置 Spring 配置文件的名称和位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>	
	<!-- 启动 IOC 容器的 ServletContextListener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- struts2配置 -->
	<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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app></span>

web.xml配置配置非常关键,如果配置出错,tomcat启动都启动不了。如果大家对此比较感兴趣,想了解更多相关知识,可以关注一下动力节点的SSH整合视频教程,里面有更丰富的知识等着大家去学习,希望对大家能够有所帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>