ssh框架搭建的基本步骤

àì夳堔傛蜴生んèń 2022-07-28 09:59 236阅读 0赞

我这里搭建的企业级开发框架是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文件配置

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

db.properties文件配置

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

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

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

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

applicationContext.xml文件配置

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  10. <context:component-scan base-package="com.ge"></context:component-scan>
  11. <!-- 导入外部资源文件 -->
  12. <context:property-placeholder location="classpath:db.properties" />
  13. <!-- c3p0方式配置数据源 -->
  14. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  15. <property name="user" value="${jdbc.user}"></property>
  16. <property name="password" value="${jdbc.password}"></property>
  17. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  18. <property name="driverClass" value="${jdbc.driverClass}"></property>
  19. <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
  20. <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
  21. </bean>
  22. <!-- 配置Hibernate的SessionFactory实例: 通过 Spring 提供的 LocalSessionFactoryBean
  23. 进行配置 -->
  24. <bean id="sessionFactory"
  25. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  26. <!-- 配置数据源属性 -->
  27. <property name="dataSource" ref="dataSource"></property>
  28. <!-- 配置 Hibernate 配置文件位置 和 名称 -->
  29. <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
  30. <!-- 配置 Hibernate 映射文件的位置和名称, 可以使用通配符 -->
  31. <property name="mappingLocations" value="classpath:com/ge/entity/*.hbm.xml"></property>
  32. </bean>
  33. <!-- 1. 配置Hibernate事务管理器 -->
  34. <bean id="transactionManager"
  35. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  36. <property name="sessionFactory" ref="sessionFactory"></property>
  37. </bean>
  38. <!-- 2. 配置事务属性, 需要事务管理器 -->
  39. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  40. <tx:attributes>
  41. <tx:method name="get*" read-only="true"/>
  42. <tx:method name="*"/>
  43. </tx:attributes>
  44. </tx:advice>
  45. <!-- 3. 配置事务切点 -->
  46. <aop:config>
  47. <aop:pointcut expression="execution( * com.ge.biz.imp.*.*(..))" id="txPointcut"/>
  48. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  49. </aop:config>
  50. </beans>
  51. </span>

这里需要改的是:

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

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

applicationContext-beans.xml文件配置

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  10. <bean class="com.ge.dao.imp.EquipmentDaoImp" id="EquipmentDaoImp">
  11. <property name="sessionFactory" ref="sessionFactory"></property>
  12. </bean>
  13. <bean class="com.ge.biz.imp.EquipmentBizImp" id="EquipmentBizImp">
  14. <property name="equipmentDao" ref="EquipmentDaoImp"></property>
  15. </bean>
  16. <bean class="com.ge.action.TestAction" id="testAction" scope="prototype">
  17. <property name="equipmentBiz" ref="EquipmentBizImp"></property>
  18. </bean>
  19. </beans>
  20. </span>

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

  1. <property name="sessionFactory" ref="sessionFactory"></property>

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

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

Struts2文件配置

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  7. <constant name="struts.multipart.saveDir" value="C:/repository"/>
  8. <constant name="struts.devMode" value="true" />
  9. <package name="HY" namespace="/" extends="struts-default">
  10. <action name="testAction" class="testAction">
  11. <result name="index">index.jsp</result>
  12. <result name="success">welcome.jsp</result>
  13. <result name="fail">fail.jsp</result>
  14. </action>
  15. </package>
  16. </struts>
  17. </span>

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

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

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="3.1"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  5. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  6. <!-- 配置 Spring 配置文件的名称和位置 -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext*.xml</param-value>
  10. </context-param>
  11. <!-- 启动 IOC 容器的 ServletContextListener -->
  12. <listener>
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <!-- struts2配置 -->
  16. <filter>
  17. <filter-name>struts2</filter-name>
  18. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  19. </filter>
  20. <filter-mapping>
  21. <filter-name>struts2</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. <welcome-file-list>
  25. <welcome-file>index.jsp</welcome-file>
  26. </welcome-file-list>
  27. </web-app></span>

web.xml配置配置非常关键,如果配置出错,tomcat启动都启动不了。

到这里SSH搭建就全部结束了,如果大家按这个步骤还是出问题,可以下载我搭建成功的一个demo,这是下载地址:点击打开链接

要是还有问题的话可以加我微信或QQ联系我:QQ:208017534 微信:qiang220316 欢迎一起交流一起进步。

Center

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读