整合S2SH框架

  S2SH框架(Struts2,Spring,Hibernate)整合

  Struts2、Hibernate和Spring。其中在Struts2部分主要为MVC设计思想,Struts2的处理流程及配置,Struts2常用控制器组件,以及Struts2常用标签库的使用。在Hibernate部分主要为O/R Mapping的设计理念,Hibernate对O/R Mapping的支持,Hibernate的配置及多种关系映射的实现,以及HQL查询数据。在Spring部分主要为IoC的原理,Spring对Bean的管理机制,Spring AOP编程,以及声明事务的配置和管理。

 

  下面来配置一下SSH搭建环境:

 

  首先配置Hibernate环境,参考我之前的博客:http://www.cnblogs.com/claricre/p/6509931.html

  接下来就是要配置Struts环境,参考我的博客:http://www.cnblogs.com/claricre/p/6542757.html

  然后是配置Spring,参考博客:http://www.cnblogs.com/claricre/p/6686231.html

 

  整合流程就是上面三篇博客,下面贴出来整个配置环境:

NationAction.java:

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class NationAction extends ActionSupport {
    public String show(){
        return SUCCESS;
    }
}

Nation.java实体类:

package com.model;
// Generated 2017-4-11 16:20:44 by Hibernate Tools 5.2.0.CR1

/**
 * Nation generated by hbm2java
 */
public class Nation implements java.io.Serializable {

    private String code;
    private String name;

    public Nation() {
    }

    public Nation(String code) {
        this.code = code;
    }

    public Nation(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Nation.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-4-11 16:20:44 by Hibernate Tools 5.2.0.CR1 -->
<hibernate-mapping>
    <class name="com.model.Nation" table="nation" catalog="mydb" optimistic-lock="version">
        <id name="code" type="string">
            <column name="Code" length="50" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="Name" length="50" />
        </property>
    </class>
</hibernate-mapping>

以上两个文件是通过Hibernate配置直接根据数据库表生成。

beans.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    
    <context:property-placeholder location="classpath:db.properties"/>
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="user"  value="${user}"></property>
        <property name="password"  value="${password}"></property>
        <property name="jdbcUrl"  value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>

        <property name="minPoolSize"  value="${minPoolSize}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
    </bean>
    
    <!-- hibernateTemplate --><!-- 如果需要可以引入hibernatTemolate来替代sessionFactory -->
    <!-- <bean class="org.springframework.orm.hibernate5.HibernateTemplate" id="hibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> -->
    <!-- jdbcTemplate --><!-- 如果需要可以jdbcTemplate -->
    <!-- <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean> -->
    
    <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/model/*.hbm.xml"></property>
    </bean>
    
    <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- <tx:advice transaction-manager="transactionManager" id="txAdvice">
        <tx:attributes>
            <tx:method name="*"/> 
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut expression="execution(* com.model.*.*(..))" id="pointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
    </aop:config> -->

</beans>

db.properties:

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb
user=root
password=
minPoolSize=5
maxPoolSize=20
initialPoolSize=5

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">jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK</property>
        <property name="hibernate.connection.username">root</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        
       <!--  <mapping resource="com/model/Nation.hbm.xml"/> -->
    </session-factory>
</hibernate-configuration>

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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="" extends="struts-default">

        

        <action name="*_*" class="com.action.{1}Action" method="{2}">
            <result>
                {1}/{2}.jsp
            </result>
        </action>
    </package>

    

</struts>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <display-name>Struts Blank</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
    <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.html</welcome-file>
    </welcome-file-list>

   

</web-app>

在页面中直接跑http://localhost:8080/SSH_zhenghe/Nation_show即可调出show.jsp页面的内容来了。

posted @ 2017-04-20 14:15  Claricre  阅读(349)  评论(0编辑  收藏  举报