【SSH】SSH框架整合开发环境的搭建

本文中总结使用SSH框架开发前的环境搭建的主要步骤。

在这里搭建的是Struts2+Spring4+Hibernate5的环境,对于新版本的整合其实也大同小异。

首先回顾一下SSH框架涉及各层的大致实现:

接下来进行环境的搭建:

无障碍整合

  • 1.创建Web项目,导入jar包
    • Struts2
      • struts-2.3.24\apps\struts2-blank\WEB-INF\lib\*.jar   --Struts2开发所必须包
      • struts2-convention-plugin-2.3.24.jar    --Struts2注解开发包(不用注解方式可以不导入)
      • struts2-spring-plugin-2.3.24.jar       --Struts2整合Spring开发包
    • Hibernate
      • hibernate-release-5.0.7.Final\lib\required\*.jar    --Hibernate开发必须包
      •  mysql-connector-java-5.1.7-bin.jar                             -- MySQL驱动包
      •         
        •                                        --日志记录包(为了方便使用属性文件管理日志所以用1.x)
        •                                       --C3P0连接池
      • Struts2和Hibernate都引入了一个相同的jar包(javassist包)。需要删除一个
    • Spring
      • IOC开发
      • AOP开发
      • JDBC模板开发
      • 事务管理
      • 整合单元测试的开发(不需要可不引入)
      • 整合hibernate的开发
      • 整合web项目的开发
  • 2.引入配置文件
    • Struts2
      • web.xml
      • 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>
    • Hibernate
      • 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">
          <hibernate-configuration>
              <session-factory>
                  <!-- 连接数据库的基本参数 -->
                  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                  <property name="hibernate.connection.url"> </property>
                  <property name="hibernate.connection.username"> </property>
                  <property name="hibernate.connection.password"> </property>
                  <!-- 配置Hibernate的方言 -->
                  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                  
                  <!-- 可选配置================ -->

                  <!-- 配置C3P0连接池 -->
                  <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
                  <!--在连接池中可用的数据库连接的最少数目 -->
                  <property name="c3p0.min_size">5</property>
                  <!--在连接池中所有数据库连接的最大数目  -->
                  <property name="c3p0.max_size">20</property>
                  <!--设定数据库连接的过期时间,以秒为单位,
                  如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
                  <property name="c3p0.timeout">120</property>
                   <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
                  <property name="c3p0.idle_test_period">3000</property>
                  
                  <!-- 引入映射 -->
                  <mapping resource=" "/>
              </session-factory>
          </hibernate-configuration>
      • 实体映射文件
        • <?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">
          <hibernate-mapping>
             
              </class>
          </hibernate-mapping>
    • Spring
      • web.xml
      • 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>
      • 日志记录文件(看版本可不引入)
  • 3.Spring整合Struts2的两种方式
    • 方式一:Action由Struts2自己创建
      • 在struts.xml中action的class属性以全路径配置
    • 方式二:Action由Spring创建(要引入Struts2与Spring整合的插件包)
      • 在Spring中配置Action,且注意要要配置成多例
      • 在struts.xml中action的class属性以spring中的id配置
    • 在Action中引入Service
      • 传统方式
      • spring与struts2整合的方式
        • xml中在Spring中配置Service和Action,并在Action中注入Service
        • 在Action类提供变量和set方法
  • 4.Spring整合Hibernate的两种方式
    • Hibernate由自己配置   在Spring中引入hibernate的配置文件
    • Hibernate由Spring配置
      • <!-- 引入外部属性文件=============================== -->
            <context:property-placeholder location="classpath:jdbc.properties"/>
            
            <!-- 配置C3P0连接池=============================== -->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="${jdbc.driverClass}"/>
                <property name="jdbcUrl" value="${jdbc.url}"/>
                <property name="user" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </bean>
            
            <!-- Spring整合Hibernate -->
            <!-- 引入Hibernate的配置的信息=============== -->
            <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
                <!-- 注入连接池 -->
                <property name="dataSource" ref="dataSource"/>
                <!-- 配置Hibernate的相关属性 -->
                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                        <prop key="hibernate.show_sql">true</prop>
                        <prop key="hibernate.format_sql">true</prop>
                        <prop key="hibernate.hbm2ddl.auto">update</prop>
                    </props>
                </property>
                
                <!-- 设置映射文件 -->
                <property name="mappingResources">
                    <list>
                        <value>com/itheima/ssh/domain/Customer.hbm.xml</value>
                    </list>
                </property>
            </bean>

至此,SSH的基本开发环境已经搭建完毕,其它开发需求再进行相关的配置。 

发布了20 篇原创文章 · 获赞 8 · 访问量 3077

猜你喜欢

转载自blog.csdn.net/weixin_40423032/article/details/104447358