前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ssh框架搭建的基本步骤

ssh框架搭建的基本步骤

作者头像
林老师带你学编程
发布2022-11-30 10:25:42
5220
发布2022-11-30 10:25:42
举报
文章被收录于专栏:强仔仔强仔仔

我这里搭建的企业级开发框架是hibernate+Struts2+Spring。单个框架使用起来出错的几率比较少但是如果将三个整合到一起就很容易出错。稍微配置有问题或者jar不合适就会出现一大推的问题,本人也深受其害啊。因为最近要开发一个项目所以就认真的研究了SSH框架的搭建,并且成功搭建成功。这里拿出来分享一下。

SSH框架配置时这几个文件比较重要:Spring,Struts2,hibernate,web.xml。

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

开始配置前只要把SSH需要的所有jar复制到WebRoot下的WEB-INF中的lib目录下。这里有我已经整合好的所有jar包,下载地址:点击打开链接

用这种方法的优点是:既可以在myeclipse用也可以在eclipse中使用,不会出现jar包冲突的事情。

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

hibernate.cfg.xml文件配置

代码语言:javascript
复制
<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文件配置

代码语言:javascript
复制
<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文件配置

代码语言:javascript
复制
<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.映射文件的位置和名字,因为我测试用的是<property name="mappingLocations" value="classpath:com/ge/entity/*.hbm.xml"></property>,这里需要修改的是:com/ge/entity也就是你映射文件所在的包位置。

2.还有就是配置事务切点的位置,我这里的位置是:<aop:pointcut expression="execution( * com.ge.biz.imp.*.*(..))" id="txPointcut"/>,这里需要修改的是事务切点位置:com.ge.biz.imp也就是你biz层所在位置。

applicationContext-beans.xml文件配置

代码语言:javascript
复制
<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文件配置

代码语言:javascript
复制
<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配置

代码语言:javascript
复制
<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搭建就全部结束了,如果大家按这个步骤还是出问题,可以下载我搭建成功的一个demo,这是下载地址:点击打开链接

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-04-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档