Struts2 + spring + hibernate 框架环境搭建

Struts2

  1. 导入相应的jar包
    这里写图片描述

  2. 配置web.xml(WEB-INF目录下)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
        <display-name>TBS</display-name>
        <filter>
            <filter-name>struts</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <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>
    </web-app>
  3. 配置struts.xml(src目录下)

    <?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="default" namespace="/" extends="struts-default">
            <default-action-ref name="welcome"></default-action-ref>
    
            <action name="welcome">
                <result>/WEB-INF/welcome.jsp</result>
            </action>
            <action name="SigninAction" class="TBS.Action.Signin">
                <result name="success">/WEB-INF/homepage.jsp</result>
                <result name="input">/WEB-INF/welcome.jsp</result>
            </action>
        </package>
    </struts>

spring + hibernate

spring对hibernate配置文件hibernate.cfg.xml的集成相当好,可以在Spring中配置Hibernate的SessionFactory从而取代Hibernate.cfg.xml和HibernateSessionFactory.java

Spring在集成Hibernate时又分为两种形式:(集成后就不需要Hibernate.cfg.xml了)

1. 继续使用Hibernate的映射文件*.hbm.xml时扫描映射文件的方法

Spring集成Hibernate时去掉了Hibernate.cfg.xml,此时如果还继续使用Hibernate的映射文件*.hbm.xml的话,在配置Hibernate的 SessionFactory 时就要配置以何种方式寻找Hibernate映射文件*.hbm.xml

此时spring中配置SessionFactory bean时它对应的class应为org.springframework.orm.hibernate.LocalSessionFactoryBean
例如:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" /><!-- 引用数据源 -->
    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:com/cn/nos/services/pojo/</value><!-- 加载hibernate的映射文件*.hbm.xml -->
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <!--<prop key="hibernate.current_session_context_class">thread</prop>-->
        </props>
    </property>
</bean>

LocalSessionFactoryBean有好几个属性用来查找hibernate映射文件:mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations

他们的区别:

  1. mappingResources:指定classpath下具体映射文件名

    <property name="mappingResources">    
        <value>petclinic.hbm.xml </value>    
    </property>    
  2. mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等

    <property name="mappingLocations">    
        <value>/WEB-INF/petclinic.hbm.xml </value>    
    </property>    
    <property name="mappingLocations">    
        <value>classpath:/com/company/domain/petclinic.hbm.xml </value>    
    </property>

    也可以用通配符指定,’‘指定一个文件(路径)名,’*‘指定多个文件(路径)名,例如:

    <property name="mappingLocations">    
        <value>classpath:/com/company/domainmaps/*.hbm.xml </value>    
    </property>    
     上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件 
  3. mappingDirectoryLocations:指定映射的文件路径

    <property name="mappingDirectoryLocations">  
     <list>    
        <value>WEB-INF/HibernateMappings</value>    
      </list>    
    </property>

    也可以通过classpath来指出

    <property name="mappingDirectoryLocations">    
      <list>    
      <value>classpath:/XXX/package/</value>    
      </list>    
    </property>   
  4. mappingJarLocations:指定加载的映射文件在jar文件中

2. 使用jpa注解形式的pojo对象,而去掉*.hbm.xml的Hibernate映射文件 时配置的方法

Spring集成Hibernate时去掉了Hibernate.cfg.xml,此时如果使用jpa注解形式的pojo对象,而去掉Hibernate的映射文件*.hbm.xml的话,在配置Hibernate的SessionFactory时就要配置以何种方式寻找jpa注解形式的pojo映射对象

此时spring中配置SessionFactory bean时它对应的class应为org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" /><!-- 引用数据源 -->
    <property name="packagesToScan">
        <list>
            <value>com.cn.nos.services.pojo*</value><!-- 加载hibernate的jpa注解形式的实体类 -->
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <!--<prop key="hibernate.current_session_context_class">thread</prop>-->
        </props>
    </property>
</bean>

AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射对象的属性有:annotatedClasses、packagesToScan

  1. annotatedClasses:指定classpath下指定的注解映射实体类的类名

    <property name="annotatedClasses">
         <list>
           <value>com.test.ObjectBean</value><!-- 可以在这个list中配置多个 -->
         </list>
    </property>
  2. packagesToScan指定映射文件的包名

    <property name="packagesToScan">
        <list>
           <value>com.cn.nos.services.pojo*</value><!-- 加载hibernate的jpa注解形式的实体类 -->
        </list>
    </property>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值