java_SSH框架集成

一、创建web工程

  1、实现一个简单的测试页面(temp.jsp)

  2、实现一个用户实体类(User.java)

二、框架集成

  1)环境搭建(拷贝jar包至项目web-inf目录下的lib文件夹下)

    搭建struts2环境:将struts2的jar包和struts2集成spring的jar包加入工程  

    搭建spring环境:将spring的jar包加入工程

    搭建hibernate环境:将hibernate的jar包加入工程

  2)相关配置

    web-inf目录中的web.xml文件中加入过滤器和侦听器

    struts2配置文件:struts.xml

    spring配置文件:applicationContext.xml

    hibernate配置文件:hibernate.cfg.xml(配置文件)     User.hbm.xml(映射文件)

三、配置文件

 1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring_struts2_hibernate</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <!-- 配置spring全局参数 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml;classpath*:applicationContext*.xml</param-value>
    </context-param>
    <!-- 配置侦听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  <!-- 配置strusts2的过滤器 -->
    <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>
</web-app>

 2)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">
           <!-- 与spring集成时  class内容写的是spring中定义的bean的名字 -->
        <action name="addUser" class="userAction" method="add">
            <result name="success">/index.jsp</result>
        </action>
    </package>

    <include file="example.xml"/>

    <!-- Add packages here -->

</struts>

 

3)applicationContext.xml文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- 加入AOP的命名空间、tx的命名空间 -->
<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/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
  

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    

  <!-- 数据库连接数据文件(驱动、登录名密码等) -->
    <context:property-placeholder location="classpath:db.properties" />
    
    <!-- Hibernate的SessionFactory的配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 注入hibernate的配置文件 -->
        <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
    <!-- 定义事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 定义自动扫描包 -->
    <context:component-scan base-package="com.gm.anno"></context:component-scan>
    <!-- 定义注解驱动事务管理 -->
    <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
    
    <!-- 定义struts2的action -->
    <bean id="userAction" class="com.gm.user.web.UserAction">
        
    </bean>
</beans>

 

4)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>
        <!-- JDBC connection pool (use the built-in) 使用连接池连接数据库
        <property name="connection.pool_size">1</property>
        -->
        
        <!-- SQL dialect 数据库SQL‘方言’ -->
        <!-- <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>


        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        
        <!-- Disable the second-level cache 禁用二级缓存
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        -->
        
        <!-- Echo all executed SQL to stdout 显示相关信息在命令行界面-->
        <!-- 显示执行过程中的SQL语句 -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        

        <!-- Drop and re-create the database schema on startup -->
        <!-- hbm是映射文件  ddl是sql语句中的数据定义语言  auto:自动  这个参数是用来创建或者删除表的 -->
        <!-- update create delete -->
        <property name="hbm2ddl.auto">create</property>
        <!-- 这个是映射配置文件 -->
        <!-- 类路径,文件格式 -->
        <mapping resource="com/gm/user/entity/User.hbm.xml"/>
        <!-- <mapping class="com.gm.jpa2.User"/>  -->
        
</session-factory>
</hibernate-configuration>

5)映射文件ur-mapping      User.hbm.xml

  demo中使用的是mysql数据库,所以不需要配置主键策略,当使用Oracle时需要使用序列(sequence)作为主键生成策略

 

<?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 package="com.gm.user.entity">
    <!-- 定义User类对应的是哪个表 -->
    <class name="User" table="Users">
        <!-- id是一个特殊属性,用来定义主键(primary key) -->
        <id name="id" column="id">
            <!-- 主键生成策略 -->
            <!-- <generator class="native"/> -->
            <!-- <generator class="sequence">
                <param name="sequence">users_id_seq</param>
            </generator> -->
        </id>
        <!-- 只有在这里定义了的列才能被持久化到数据库中 -->
        <property name="name"/>
        <property name="age" column="ages"/>
    </class>
</hibernate-mapping>

posted @ 2017-02-15 20:36  因为足迹才美丽  阅读(101)  评论(0)    收藏  举报