zoukankan      html  css  js  c++  java
  • java深入探究12-框架整合

    1.Spring与Hibernate整合

      需要配置的就是hibernate和bean.xml

      1)关键点:sessionFactory创建交给SpringIOC;session的事务处理交给Spring的事务处理

      2)jar包:  

        连接池/数据库驱动包

        Hibernate相关jar

        Spring 核心包(5)

        Spring aop (4)

        spring-orm-3.2.5.RELEASE.jarspringhibernate的支持】

    spring-tx-3.2.5.RELEASE.jar【事务相关】

      3)整合步骤:A:创建sessionFactory 的bean节点;B:对逻辑层的业务方法添加事务

        A:sessionFactory整合在Spring bean中的三种方式:

        1.全部hibernate.cfg.xml引入bean.xml中

        2.部分引入:dataSource连接池写在Spring中

        3.全部在bean.xml中写

        B:对逻辑层添加事务用aop事务方式

      4)例子:  

    1.bean.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:p="http://www.springframework.org/schema/p"
        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/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- dao 实例 -->
        <bean id="deptDao" class="dao.DeptDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!-- service 实例 -->
        <bean id="deptService" class="cn.itcast.service.DeptService">
            <property name="deptDao" ref="deptDao"></property>
        </bean>
        <!-- 数据源配置 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
            <property name="initialPoolSize" value="3"></property>
            <property name="maxPoolSize" value="10"></property>
            <property name="maxStatements" value="100"></property>
            <property name="acquireIncrement" value="2"></property>
        </bean>
        
        <!-- ###########Spring与Hibernate整合  start########### -->
        <!-- 方式一:直接加载hibernate.cfg.xml文件的方式整合   这种方式 -->
        <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean> -->
        
        <!-- 方式二:连接池交给Spring来生成【一部分配置写到hibernate中,一份分在spring中完成】 -->
        <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
            <property name="dataSource" ref="dataSource"></property>
        </bean> -->
        
        <!-- 推荐 方式三:所有配置全部写到Spring配置中 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <!-- 连接池配置 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- hibernate常用配置会 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            <!-- hibernate映射配置 -->
            <!-- hibernate映射配置 
            <property name="mappingLocations">
                <list>
                    <value>classpath:entity/*.hbm.xml</value>
                </list>
            </property>
            -->
            <property name="mappingDirectoryLocations">
                <list>
                    <value>classpath:entity/</value>
                </list>
            </property>
            
            
        </bean>
        
        <!-- ###########Spring与Hibernate整合  end########### -->
        <!-- 事务配置 -->
        <!-- a. 配置事务管理器类 -->
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        <!-- c. Aop配置 -->
        <aop:config>
             <aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
             <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
        </aop:config>
    </beans>     
    View Code

    2.SSH:三大框架整合在一起

      1)引入SSH Jar

          Struts 核心jar

          Hibernate 核心jar

          Spring

            Core  核心功能

            Web  web模块支持

            Aop   aop支持

            Orm   hibernate支持

            Jdbc/tx  jdbc支持包、事务相关包

      2)配置

        Web.xml

          初始化struts功能、spring容器

          Struts.xml   配置请求路径与映射action的关系

        Spring.xml  IOC容器配置

        bean-base.xml     【公用信息】

        bean-service.xml

        bean-dao.xml

        bean-action.xml

      3)例子:

    web.xml配置

      1.配置spring的OpenSessionInView模式 ;2.struts2配置 3.Spring配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
        
        <!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
        <!-- 注意:访问struts时候需要带上*.action后缀 -->
        <filter>
            <filter-name>OpenSessionInView</filter-name>
            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>OpenSessionInView</filter-name>
            <url-pattern>*.action</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配置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:bean*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
      <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    View Code

      2.配置src下的各层bean.xml

    1.bean-base.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:p="http://www.springframework.org/schema/p"
        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/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        
        <!-- 所有配置的公共部门 -->
        
        <!-- 1) 连接池实例 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
            <property name="initialPoolSize" value="3"></property>
            <property name="maxPoolSize" value="6"></property>
        </bean>
    
        <!-- 2) SessionFactory实例创建 -->
        <!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <!-- a. 连接池 -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            
            <!-- c. 映射配置 -->
            <property name="mappingLocations">
                <list>
                    <value>classpath:entity/*.hbm.xml</value>
                </list>
            </property>
        </bean>
        
        <!-- 3) 事务配置 -->
        <!-- # 事务管理器 -->
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!-- # 事务增强 -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        <!-- # AOP配置 -->
        <aop:config>
            <aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
        </aop:config>
        
    </beans>     
    View Code

     2.bean.dao.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:p="http://www.springframework.org/schema/p"
        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/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        
        <bean id="employeeDao" class="cn.itcast.dao.EmployeeDao">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
    </beans>     
    View Code

      3.bean-service.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:p="http://www.springframework.org/schema/p"
        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/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        
        <bean id="employeeService" class="cn.itcast.service.EmployeeService">
            <property name="employeeDao" ref="employeeDao"></property>
        </bean>
        
    </beans>     
    View Code

     4.bean-action.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:p="http://www.springframework.org/schema/p"
        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/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <bean id="employeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype">
            <property name="employeeService" ref="employeeService"></property>
        </bean>
        
    </beans>     
    View Code

      3.配置structs

    <?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="emp" extends="struts-default">
    
            <!-- action实例交给spring容器创建 -->
            <action name="show" class="employeeAction" method="execute">
                <result name="success">/index.jsp</result>
            </action>
    
        </package>
    
    </struts>
    View Code

     4.ssh在用的时候要注意的

      1)三者结合在一起时,对数据层进行操作本来是有hibernate的session对象来做的,现在可由spring来获得hibernate的得到hibernateTemple对象来代替springDao中的jdbcTemple和hibernate中的session和dbutil等工具

       使用它时需要实现HibernateDaoSupport接口;使用此接口的类也需要在bean中添加sessionFactory对象

    spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作,此方法是在配置了spring以后,hibernate由spring接管,不直接使用hibernate的session了 
    HibernateTemplate提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作,Spring 2.0更增加对命名SQL查询的支持,也增加对分页的支持。大部分情况下,使用Hibernate的常规用法,就可完成大多数DAO对象的CRUD操作。下面是HibernateTemplate的常用方法简介:
    q      void delete(Object entity):删除指定持久化实例
    q      deleteAll(Collection entities):删除集合内全部持久化类实例
    q      find(String queryString):根据HQL查询字符串来返回实例集合
    q      findByNamedQuery(String queryName):根据命名查询返回实例集合
    q      get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例
    q      save(Object entity):保存新的实例
    q      saveOrUpdate(Object entity):根据实例状态,选择保存或者更新
    q      update(Object entity):更新实例的状态,要求entity是持久状态
    q      setMaxResults(int maxResults):设置分页的大小
    getHibernateTemplate已经封装好了一些基本的方法,可以直接去用,也就是template嘛, 
    而getSession只是获取一个数据工厂的session,然后大部分方法都需要自己写,加hql语句,然后用query方法执行 
    
    谈不上什么优点缺点,类似添加删除更新这样的可以直接用getHibernateTemplate而大部分带条件查询的就需要用getSession自己写了
    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
    主流技术到底就是主流技术,效率就不一般。
    Hibernate封装了对数据库的例行操作,比单纯的jdbc的DAO,开发效率要高很多了。而Springframework对Hibernate的操作又进行了进一步的包装,又将开发效率提升不少。下面的例子是Spring自己给的petclinic的样本程序。
    原来b/s架构的大量数据库操作可以这么轻松搞定
    View Code
  • 相关阅读:
    流水账
    还是有希望啊
    The Java Tutorials:Collections
    绘图框架新需求
    Fractal Tree扩展
    js获取字符串最后一个字符代码
    js保留小数点后N位的方法介绍
    JS 实现 ResizeBar,可拖动改变两个区域(带iframe)大小
    js获取浏览器高度和宽度值,尽量的考虑了多浏览器。
    jquery自动将form表单封装成json的具体实现
  • 原文地址:https://www.cnblogs.com/xiaoping1993/p/6930700.html
Copyright © 2011-2022 走看看