码迷,mamicode.com
首页 > 其他好文 > 详细

SSH2整合学习笔记

时间:2015-11-27 15:09:13      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:hibernate   配置文件   开发平台   数据库   spring   

我使用的是spring3+hibernate3+Struts2;开发平台eclipse,数据库MySQL。

  1. 在eclipse中新建一个web项目。

  2. 搭建spring的环境:加入spring的核心jar包,加入spring的配置文件,在配置文件中配置一个bean,测试是否能够成功注入;

  3. 搭建hibernate环境:加入hibernate的核心jar包以及MySQL数据库的jar包,测试是否能够像数据库中增加数据;

  4. 整合hibernate和spring:加入spring和hibernate整合的相关jar包,配置spring的配置文件:

  5. <?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:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <!-- 配置数据源  -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>    
        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!--配置通知-->
        <tx:advice id="advice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="*" propagation="SUPPORTS"/>
            </tx:attributes>
        </tx:advice>
        <!--配置AOP切面表达式-->
        <aop:config>
            <!-- 配置哪些包的类要切入事务 -->
            <aop:pointcut expression="execution(* cn.it.shop.service.impl.*.*(..))" id="pointcut"/>
            <!-- 通知+切面表达式,才是完整的切入路径 -->
            <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>    
        </aop:config>
    </beans>

  6.把service交给spring管理,新建一个spring-service.xml配置文件,并在里面配置service的bean。

<bean id="accountService" class="cn.it.shop.service.impl.AccountServiceImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

7.测试spring和hibernate是否整合成功

protected Session getSession(){
        //从当前线程获取session,如果没有则创建一个新的sessionhttp://93511286.blog.51cto.com/addblog.php
        return sessionFactory.getCurrentSession();
    }
    @Override
    public void update(Account account) {
        getSession().update(account);
    }
@Test
    public void testSpringAndHibernate(){
        Account account = new Account(4,"Lucy","露西","1");
        accountService.update(account);
    }

8.搭建Struts2的开发环境:加入Struts2开发的jar包;在web.xml文件中配置Struts拦截器

<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>*.action</url-pattern>
  </filter-mapping>

在struts.xml文件中配置action,测试通过jsp页面是否能成功访问到action;

9.整合spring与Struts2:添加spring和web相关的jar包spring-web-3.2.0.RELEASE.jar以及spring和Struts2整合的jar包struts2-spring-plugin-2.1.6.jar包;

10.在web.xml文件中配置spring的监听器

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

11.测试是否整合成功。

源代码下载地址:http://down.51cto.com/data/2122609



本文出自 “冬眠” 博客,请务必保留此出处http://93511286.blog.51cto.com/6742816/1717443

SSH2整合学习笔记

标签:hibernate   配置文件   开发平台   数据库   spring   

原文地址:http://93511286.blog.51cto.com/6742816/1717443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!