• SSH 项目建立过程


    1. 加入 Spring

      1). 加入 jar 包
      2). 配置 web.xml 文件

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

      3). 加入 Spring 的配置文件.  new一个spring 配置文件:   applicationContext.xml   (eclipse集成spring,   

      插件springsource-tool-suite-3.9.4.RELEASE-e4.8.0-updatesite.zip) 链接:https://pan.baidu.com/s/1nPBnX5i7JQyVbbsqFl56FQ 密码:vlwa

      

    2. 加入 Hibernate

      1). 配置  Hibernate

        ①. 加入 jar 包

        ②. 在类路径下加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性

    <?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 显示及格式, 生成数据表的策略以及二级缓存 -->
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL dialect -->
            <property name=" ">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
    
            <mapping class="com.test.entitys.Department"/>
            <mapping class="com.test.entitys.Employee"/>
    
        </session-factory>
    
    </hibernate-configuration>
    View Code

        ③. 使用注解配置持久化类  例如:需要进行多对一关联,  另行配置

    package com.test.entitys;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    public class Department {
        private Integer id;
        private String departmentName;
        
        @Id
        @GeneratedValue
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getDepartmentName() {
            return departmentName;
        }
        public void setDepartmentName(String departmentName) {
            this.departmentName = departmentName;
        }
    }
    View Code

    3.  和 Spring 进行整合 

      1)  加入 c3p0 和 MySQL 的驱动
      2)  配置 Spring 的配置文件 applicationContext.xml

      3)  配置mysql驱动等,  新建 db.properties 文件,(在数据库中创建对应的数据库)  内容如: 

    jdbc.user=root
    jdbc.password=root
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql:///ssh
    
    jdbc.initialPoolSize=5
    jdbc.maxPoolSize=10 

      4)  配置 c3p0 数据源

    <!-- 导入资源文件 -->
        <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="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>

      5)  配置  SessionFactory

        <!-- 配置 Hibernate 的 SessionFactory 实例 : 通过Spring 提供的 LocalSessionFactoryBean 进行配置 -->
        <bean id="sessionFactroy" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <!-- 配置数据源属性 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 配置 hibernate 配置文件的位置及名称 -->
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>

      6)  到现在,启动项目, 会看到生成对应的数据表

    4. 配置 Spring 的声明式事物

      1)配置 hibernate 事物管理器   

      <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactroy"></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.service.*.*(..))" id="txPointcut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>

    5. 加入 Struts2

    1). 加入 jar 包: 若有重复的 jar 包, 则需要删除版本较低的
    2). 在 web.xml 文件中配置 Struts2 的 Filter

       <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>

    3). 加入 Struts2 的配置文件   当配置了下面  6-2  后,  action类就可以直接使用 IOC 中的id

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <constant name="struts.devMode" value="true" />    <!-- 实时编译 -->
        <constant name="struts.i18n.encoding" value="GBK" />    <!-- 解决中文乱码问题 -->
        <constant name="struts.multipart.maxSize" value="10000000000" />
        <package name="default" namespace="/" extends="struts-default">   
             <action name="emp_*" class="employeeAction" method="{1}">
                   <result name="list">
                       /WEB-INF/views/emp_list.jsp
                   </result>
               </action> 
         </package> 
    </struts>
    View Code

    6. 整合 Spring
      1) 加入 Struts2 的 Spring 插件的 jar 包  struts2-spring-plugin-2.3.15.3.jar
      2) 在 Spring 的配置文件中正常配置 Action, 注意 Action 的 scope 为 prototype(非单例)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="employeeAction" class="com.actions.EmployeeAction" scope="prototype">
        </bean>
    </beans>
    View Code

      3) 在 Struts2 的配置文件中配置 Action 时, class 属性指向该 Action 在 IOC 中的 id

    4. 完成功能.

    1). 查询数据库信息

      (1) 新建dao,  dao使用 IOC 容器管理,   需要在  applicationContext.xml  中配置 启动注解扫描包 来使用注解方式 管理 IOC

    <!-- 配置注解扫描包 -->
        <context:component-scan base-package="com"></context:component-scan>

     如果要使用 spring 事物管理,  参考 spring 事物管理

  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    Python_匿名函数
    Python_内置函数之map()
    Python_面向对象_单例模式
  • 原文地址:https://www.cnblogs.com/redhat0019/p/9146347.html
Copyright © 2020-2023  润新知