SSH 环境配置(Strut2+Spring3.2+Hibernate3)

SSH非注解配置
先配置hibernate.cfg.xml,在项目中导入的Hibernate框架的时候自动帮我们生成好了

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/message?characterEncoding=utf-8
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">@)!$</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">
            com.mysql.jdbc.Driver
        </property>

        <mapping resource="com/dao/CourseGrade.hbm.xml" />
        <mapping resource="com/dao/Course.hbm.xml" />
        <mapping resource="com/dao/Student.hbm.xml" />

    </session-factory>

</hibernate-configuration>

然后是application.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">   

        <!-- 配置数据库连接 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/message?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="@)!$"></property>
        </bean>
          <!-- 事务配置管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!--配置hibernatesessionfaxtory-->
    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <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>
       </props>
     </property>
     <property name="mappingDirectoryLocations">
       <list>
       <value>classpath:com/po</value>
       </list>
     </property> 
    </bean>

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />

            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 那些类的哪些方法参与事务 -->
    <aop:config >
        <aop:pointcut id="allServiceMethod"
            expression="execution(* com.dao.*.*(..))" />
        <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
    </aop:config>
     <!-- 将DAO实现注入到Spring -->
     <bean id="StudentDAO" class="com.dao.StudentDAO"></bean>
     <!-- Action -->
     <bean id="studentQueryAction"
      class="com.action.StudentAction" scope="prototype">
     <property name="StudentDAO" ref="StudentDAO"></property>
     </bean>
      <bean id="BD" class="com.dao.IBaseHibernateDAO">
     <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
    <beans>

在struts.xml中配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--开发模式中常用,提示错误-->
<constant name="struts.devMode" value="true"></constant>
<!--将Struts交给Spring管理,这里需要3个重要的包-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="struts2" extends="struts-default">

<action name="studentquery" class="studentQueryAction">
<result name="success">/views/student.jsp</result>
</action>

</package>
</struts>    

配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>ssh</display-name>

  <!-- Sprring装载器 -->
  <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>


  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

自此完成了SSH环境的配置和实现类DAO,sessFactory的注入。其中相关的包需要自己导入,Spring的包是我自己添加的,Strut2和Hibernate3是采用系统导入,但是一定要勾选相关的功能,报错一般是缺少相应的包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值