美文网首页我爱编程
Hibernate,Struts2,Spring整合

Hibernate,Struts2,Spring整合

作者: 蕊er | 来源:发表于2017-11-10 15:32 被阅读0次

整合原理

  • Struct2与Spring整合:将Action对象交与Spring容器负责创建
  • Hibernate与Spring整合:将SessionFactory交与Spring容器来维护,Spring负责Session维护及aop事务

导包

  • hibernate

    • hibernate/lib/required
    • hibernate/lib/jpa(java persist api java的持久化规范 接口)
    • jdbc包 ---- mysql-connector-java
  • Struts2

    • struts-blank.war/WEB-INF/lib/*

    • struts整合spring插件包 ---- struts2-spring-plugin

      注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常

  • Spring

    • 基本:4+2 ---- core|beans|context|expression|logging|log4j
    • 整合web:web包 ---- spring-web
    • 整合aop:4个 ---- spring-aop|spring-aspect|aop联盟|aopweaving
    • 整合Hibernate和事务:4个 ---- spring-jdbc|spring-tx|c3p0|spring-orm
    • 整合junit4测试:test包 ---- spring-test
  • 标签库 ---- jstl

配置Spring

  • 创建配置文件并导入约束(4个)
    beans|context|aop|tx

  • 配置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>
    

配置Structs2

  • 配置struts2主配置文件struts.xml

  • 配置struts2核心过滤器到web.xml

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
          org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

struts2与spring整合

  • 配置常量

    <!--将action的创建交与Spring容器-->
    <constant name="struts.objectFactory" value="spring"/>
    <!--Spring负责装配Action的依赖,默认打开-->
    <constant name="struts.objectFactory.spring.autoWire" value="name"/>
    
  • 整合

    • struts2自己创建action,spring负责组装依赖属性

      //struts.xml
      <action name="demo" class="com.ssh.demo.view.DemoAction">
          <result name="success">/index.jsp</result>
      </action>
      
      //applicationContext
      <bean name="action" class="com.ssh.demo.view.DemoAction"/>
      <bean name="userService" class="com.ssh.demo.service.UserServiceImpl"/>
      
    • spring负责创建action以及组装

      //struts
      <!--class上填写spring中Action对象的name属性值,完全由Spring管理Action的生命周期,
      包括Action的创建。备注需要手动组装依赖属性-->
      <action name="demo" class="demoAction">
          <result name="success">/index.jsp</result>
      </action>
      
      //applicationContext.xml
      <bean name="userService" class="com.ssh.demo.service.UserServiceImpl"/>
      <!--Action对象作用域一定是多例的,这样才符合struts2框架-->
      <bean name="demoAction" class="com.ssh.demo.view.DemoAction" scope="prototype">
          <property name="userService" ref="userService"/>
      </bean>
      

Hibernate配置

  • 导入实体类或orm元数据

  • 配置主配置文件

    <session-factory>
        <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库url -->
        <property name="hibernate.connection.url">jdbc:mysql://39.106.1.213:3306/hb?                        characterEncoding=utf-8</property>
        <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">xxx</property>
        <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">xxx</property>
        <!-- 数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    
        <!-- 将hibernate生成的sql语句打印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
        <property name="hibernate.format_sql">true</property>
    
        <property name="hibernate.hbm2ddl.auto">update</property>
    
        <!-- 引入orm元数据
            路径书写: 填写src下的路径
         -->
        <mapping class="com.ssh.demo.entity.User"/>
    </session-factory>
    

Spring整合hibernate

将sessionFactory对象交给spring容器管理\

  • 配置方案一:仍然使用外部的hibernate.cfg.xml配置信息
<!--将SessionFactory配置到Spring-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
  • 配置方案二:不使用hibernate.cfg.xml配置文件,在spring中配置hibernate配置信息
<!--将SessionFactory配置到Spring-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="hibernateProperties" >
        <props>
            <!--必选配置-->
            <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:mysql://39.106.1.213:3306/hb?                             characterEncoding=utf-8</prop>
            <prop key="hibernate.connection.username">xxx</prop>
            <prop key="hibernate.connection.password">xxx</prop>
            <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>
    <!--xml配置:引入orm元数据,指定orm元数据所在包路径,spring会自动读取包中所有配置-->
    <property name="mappingDirectoryLocations" value="classpath:com/ssh/demo/entity"/>
    <!--注解配置:spring会自动读取包中注解的配置-->
     <property name="packagesToScan">
        <list>
            <value>com.ssh.demo.entity</value>
        </list>
      </property>
</bean>

Spring整合c3p0连接池

  • 配置db.properties

    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql://39.106.1.213:3306/hb?characterEncoding=utf-8
    jdbc.user=xxx
    jdbc.password=xxx
    
  • 引入连接池到spring中

    <!--配置c3bO连接池-->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
  • 将连接池注入给SessionFactory

    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
       <!-- 将连接池注入到SessionFactory中,hibernate通过连接池获得连接-->
        <property name="dataSource" ref="dataSource"/>
        <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>
        <!--引入orm元数据,指定orm元数据所在包路径,spring会自动读取包中所有配置-->
        <!--property name="mappingDirectoryLocations" value="classpath:com/ssh/demo/entity"/>-->
        <property name="packagesToScan">
            <list>
                <value>com.ssh.demo.entity</value>
            </list>
        </property>
    
    </bean>
    

Spring整合hibernate事务管理

  • Dao类创建:继承HibernateDaoSupport

    UserDaoImpl extends HibernateDaoSupport

  • hibernate模板的操作

    getHibernateTemplate()

  • spring中配置dao

    <bean name="userDao" class="com.ssh.demo.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <bean name="userService" class="com.ssh.demo.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
    
    <!--Action对象作用域一定是多例的,这样才符合struts2框架-->
    <bean name="demoAction" class="com.ssh.demo.view.DemoAction" scope="prototype">
        <property name="userService" ref="userService"/>
    </bean>
    

Spring的aop操作

  • 将核心事务管理器配置到spring

    <bean name="transactionManager"                                                           class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
  • 配置事务

    • xml

      • 配置通知

        <!-- 配置事务通知 -->
        <tx:advice transaction-manager="transactionManager" id="txadvice">
            <tx:attributes>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        
      • 配置织入

        <!-- 配置织入-->
        <aop:config>
            <!-- 配置切点表达式-->
            <aop:pointcut id="txPc" expression="execution(* com.ssh.demo.service.*.*(..))"/>
            <!-- 配置切面:通知+切点-->
            <!-- advice-ref:通知名称-->
            <!-- pointcut-ref:切点名称-->
            <aop:advisor advice-ref="txadvice" pointcut-ref="txPc"/>
        </aop:config>
        
    • 注解

      • 开启注解事务

        <tx:annotation-driven  transaction-manager="transactionManager"/>
        
      • Service类中使用注解(类或方法)

        @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)
        

扩大session作用范围(配置filter)

​ 为了避免使用懒加载时出现no-session问题.需要扩大session的作用范围

任何Filter一定要在strutsFilter之前调用

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

相关文章

网友评论

    本文标题:Hibernate,Struts2,Spring整合

    本文链接:https://www.haomeiwen.com/subject/jdibmxtx.html