SSH整合框架搭建

SSH环境搭建:

1、创建WEB项目,引入jar包

struts2的基本jar包:

struts2的注解开发包:

struts2-convention-plugin-2.3.31.jar

struts2用于整合spring开发包:

struts2-spring-plugin-2.3.31.jar

Hibernate基础jar包:

hibernate-distribution-3.6.10.Final/hibernate3.jar

hibernate-distribution-3.6.10.Final/lib/required/*.jar

hibernate-distribution-3.6.10.Final/lib/jpa/*/jar

日志记录包: slf4j-log4j12-1.7.5.jar

数据库驱动包:mysql-connector-java-5.1.25-bin.jar

spring的基础jar包:

IOC:

spring-beans-3.2.9.RELEASE.jar

spring-context-3.2.9.RELEASE.jar

spring-core-3.2.9.RELEASE.jar

spring-expression-3.2.9.RELEASE.jar

log4j-1.2.15.jar

commons.logging-1.1.1.jar

 AOP:

spring-aop-3.2.5.RELEASE.jar

spring-aspects-3.2.5.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

事物管理:

spring-tx-3.2.5.RELEASE.jar

spring-jdbc-3.2.5.RELEASE.jar

整合Hibernate的包:

spring-orm-3.2.5.RELEASE.jar

整合web项目:

spring-web-3.2.5.RELEASE.jar

整合Junit单元测试:

spring-test-3.2.5.RELEASE.jar

连接池:

com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

引入配置文件:

struts2配置文件:

web.xml

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

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

*.hbm.xml

spring配置文件:

web.xml

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

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"
    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-3.2.xsd">

    <!-- 配置业务层类 -->
    <bean id="productService" class="cn.nyan.service.ProductService">
        <property name="productDao" ref="productDao"/>
    </bean>
    <!-- 配置DAO类 -->
    <bean id="productDao" class="cn.nyan.dao.ProductDao"/>
    
</beans>

创建页面:

<%@ taglib uri="/struts-tags" prefix="s"%>
<!--simple不用struts自带的form格式-->
<s:form action="" method="post" namespace="/" theme="simple">
        <table border="1" width="400">
            <tr>
                <td>商品名称:</td>
                <td><s:textfield name="pname"/></td>
            </tr>
            <tr>
                <td>商品价格:</td>
                <td><s:textfield name="pname"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="添加"/></td>
            </tr>
        </table>
    </s:form>

struts整合spring:

在Action层可以不用通过原始的工厂模式方法得到Service层的业务逻辑

直接通过自动注入装配即可

在strut core 的jar包里有配置:

struts.objectFactory.spring.autoWire = name

public class ProductAction extends ActionSupport implements ModelDriven<Product>{

    private static final long serialVersionUID = 1L;
    //struts的传参数方式
    Product product =new Product();
    @Override
    public Product getModel() {
        return product;
    }
    private ProductService productService;
  //根据名字自动注入
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

 Struts2和spring整合的两种方式:

1.Action的类有struts2自身去创建

<package name="cn.nyan" extends="struts-default" namespace="/">
        <action name="product_*" method="{1}" class="cn.nyan.action.ProductAction">
            <result>/{1}.jsp</result>
        </action>
    </package>

2.Action的类交给Spring框架创建

<!-- 配置Action类 -->
    <!-- 在struts中配置创建对象是多例的,spring配置是单例的,所以要加上scope -->
    <bean id="productAction" class="cn.nyan.action.ProductAction" scope="prototype">
        <!-- 手动注入Service -->
        <property name="productService" ref="productService"/>
    </bean>

<package name="cn.nyan" extends="struts-default" namespace="/">
        <action name="product_*" method="{1}" class="productAction">
        </action>
    </package>

这种方式的Action可以交给Spring的AOP进行管理,增强操作。

Action:
public
String save(){ System.out.println("Action中的save方法执行了"); productService.save(product); //不进行页面跳转 return NONE; } Service: public void save(Product product) { System.out.println("Service层的save方法执行了"); productDao.save(product); } Dao: public void save(Product product) { System.out.println("DAO的save方法执行了"); }

Spring整合Hibernate框架:

创建数据库:create database ssh;

创建映射文件:

在Hibernate.jar的/org/hibernate/hibernate-mapping-3.0.dtd找到Hibernate声明头

<?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 name="cn.nyan.domain.Product" table="product">
        <id name="pid">
            <generator class="native"/>
        </id>
        <property name="pname"></property>
        <property name="price"></property>
    </class>
</hibernate-mapping>

 Spring整合Hibernatejar包:

spring-orm-3.2.5.RELEASE.jar

spring-jdbc-3.2.5.RELEASE.jar

spring-tx-3.2.5.RELEASE.jar-->org.springframework.dao.support.PersistenceExceptionTranslator

hibernate-jpa-2.0-api-1.0.1.Final.jar-->javax.persistence.EntityListeners

java persistence API

<!-- 引入外部属性文件Jdbc.properties -->
    <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>
    
    <!-- 配置Hibernate的相关属性 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.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>
        <!-- 配置Hibernate中的映射文件 -->
        <property name="mappingResources">
            <list>
                <value>cn/nyan/domain/product.hbm.xml</value>
            </list>
        </property>
    </bean>

 

 继承HibernateDaoSupport来实现数据保存操作:

public class ProductDao extends HibernateDaoSupport{

    public void save(Product product) {
        this.getHibernateTemplate().save(product);
    }
//里面有setSessionFactory方法所以如果没有注入sessionFactory,
//则会出现'sessionFactory' or 'hibernateTemplate' is required错误 }
<!-- 配置DAO类 -->
    <bean id="productDao" class="cn.nyan.dao.ProductDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

配置事务管理器

spring-tx-3.2.5.RELEASE.jar

spring-aop-3.2.5.RELEASE.jar

spring-aspects-3.2.5.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

 

 

posted @ 2017-02-26 13:45  Nyan  阅读(496)  评论(0编辑  收藏  举报