S2SH框架的整合—传统配置文件方式

SSH框架的传统的整合方式:

(仅仅是S2SH框架的整合,每个框架的具体使用将发布在其他篇)

  1. 导jar包

Struts2 spring hibernate的jar包

  1. 配置文件

1). 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>

 

    * 在src目录下创建struts.xml,用来配置Action

 

2). Hibernate框架

    * 在src目录创建hibernate.cfg.xml配置文件

    * 在JavaBean所在的包下映射的配置文件

 

3). Spring框架

    * 在web.xml配置整合WEB的监听器

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

 

    * 在src目录下创建applicationContext.xml

    * 在src目录下log4j.proerties

 

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

    id="WebApp_ID" version="2.5">

    <display-name>Demo_ssh</display-name>

 

    <!-- 解决SSH框架整合后的延迟加载出错的问题 -->

    <filter>

       <filter-name>OpenSessionInViewFilter</filter-name>

    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>

    </filter>

    <filter-mapping>

       <filter-name>OpenSessionInViewFilter</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

 

    <!-- Struts2的核心过滤器 -->

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

   

    <!-- Spring容器的监听器 -->

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

 

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

 

整合Struts2框架:

    整合Struts2框架的主要问题是:Action由谁创建的问题:

    第一种:ActionStruts2创建

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

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

       <action name="userDemo_*" class="com.demo.web.action.UserDemoAction" method="{1}" >

       </action>

    </package>

</struts>

       第二种:Action由spring创建:

              在spring配置文件中加入bean

                     <bean id="userDemorAction" class=" com.demo.web.action.UserDemoAction "  scope="prototype">

                   Struts2的配置文件要做更改:

                     struts.xml中的修改,把全路径修改成ID * <action name="userDemo_*" class=" userDemorAction " method="{1}">

       * 第二种方式需要有两个注意的地方

        * Spring框架默认生成CustomerAction是单例的,而Struts2框架是多例的。所以需要配置 scope="prototype"

        * CustomerService现在必须自己手动注入了

 

 

整合hibernate框架:

    整合hibernate框架的主要问题是sessionFactory放在哪个配置文件

    第一种:带有hibernate.cfg.xml的配置文件。强调:不能加绑定当前线程的配置

       1. 编写CustomerDaoImpl的代码,加入配置并且在CustomerServiceImpl中完成注入

2. 编写映射的配置文件,并且在hibernate.cfg.xml的配置文件中引入映射的配置文件

3. applicationContext.xml的配置文件,配置加载hibernate.cfg.xml的配置

           <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

                <property name="configLocation" value="classpath:hibernate.cfg.xml"/>

           </bean>

4. CustomerDaoImpl中想完成数据的添加,Spring框架提供了一个HibernateDaoSupport的工具类,以后DAO都可以继承该类!!

           public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

               public void save(Customer c) {

                   System.out.println("持久层...");

                   this.getHibernateTemplate().save(c);

               }

           }

           <bean id="customerDao" class="com.itheima.dao.CustomerDaoImpl">

               <property name="sessionFactory" ref="sessionFactory"/>

           </bean>

    第二种:不带hibernate.cfg.xml配置文件的方式。

              1. Hibernate配置文件中

                 * 数据库连接基本参数(4大参数)

                  * Hibernate相关的属性

                 * 连接池

                 * 映射文件

 

2. 开始进行配置

       在Spring的配置文件中配置

                 * 先配置连接池相关的信息

             <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

            <property name="driverClass" value="com.mysql.jdbc.Driver"/>

            <property name="jdbcUrl" value="jdbc:mysql:///xxx"/>

            <property name="user" value="root"/>

            <property name="password" value="root"/>

              </bean>

 

                 * 修改 LocalSessionFactoryBean 的属性配置,因为已经没有了hibernate.cfg.xml的配置文件,所以需要修改该配置,注入连接池

             <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">

            <property name="dataSource" ref="dataSource"/>

             </bean>

 

                 * 继续在 LocalSessionFactoryBean 中配置,使用hibernateProperties属性继续来配置其他的属性,注意值是properties属性文件

             <!-- 配置其他的属性 -->

             <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/domain/Customer.hbm.xml</value>

            </list>

             </property>

              Hibernate模板类的常用方法:

              1. 增删改的操作:

            * 添加:

               * save(Object obj);

            * 修改:

               * update(Object obj);

            * 删除:

               * delete(Object obj);

 

2. 查询的操作:

         * 查询一条记录:

        * Object get(Class c,Serializable id);

        * Object load(Class c,Serializable id);

 

3. 查询多条记录:

                 * List find(String hql,Object... args);

 

 

Spring配置文件:

       <!--配置链接池-->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <!--配置四大参数-->

        <property name="driverClass" value="com.mysql.jdbc.Driver"/>

        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/ssh"/>

        <property name="user" value="root"/>

        <property name="password" value="root"/>

    </bean>

 

    <!--配置sessionFactory 没有hibernate.cfg.xml配置文件-->

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

        <!--引入数据源-->

        <property name="dataSource" ref="dataSource"/>

        <!--配置方言和可选参数-->

        <property name="hibernateProperties">

            <props>

                <!--配置方言-->

                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</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>domain/Customer.hbm.xml</value>

            </list>

        </property>

    </bean>

 

    <!--事务配置-->

    <!--配置平台事务管理器-->

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <!--开启注解事务-->

    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

 

    <!--配置Customer模块-->

    <bean id="customerAction" class="web.action.CustomerAction" scope="prototype">

        <property name="customerService" ref="customerService"/>

    </bean>

 

    <bean id="customerService" class="service.CustomerServiceImpl">

        <property name="customerDao" ref="customerDao"/>

    </bean>

 

    <bean id="customerDao" class="dao.CustomerDaoImpl">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值