码迷,mamicode.com
首页 > 编程语言 > 详细

SSH(Spring+Struts2+Hibernate)整合

时间:2016-08-21 01:07:43      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:hibernate   spring   st   mysql   

本博文介绍目前较流行的三大(Spring+Struts2+Hibernate)框架的整合。

一般整合需要以下几个步骤:

1、首先导入相应的jar包

  Spring所需的jar包如下图:
技术分享  

  Struts所需的jar包如下图:

技术分享

   hibernate所需的jar包如下图:

技术分享

   一些共同所需的jar包如下图:

技术分享

其中mysql-connector-java-5.1.33-bin.jar是连接mysql数据库所需的jar包。

将上述的jar包拷贝到项目的lib目录下。

2、spring和struts整合

只需要Struts2-spring-plugin-2.3.24.jar包即可,上面已经引入。

在web.xml加入

<listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

   ContextLoaderListener实现ServletContextListener接口,在服务器启动时加载,

   默认自动载入置于WEB-INF目录下的Spring配置文件applicationContext.xml

3、spring和hibernate整合

在Src目录下添加SpringContext.xml文件。

(1)引入声明式事务支持的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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.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/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee

(2)注解配置以及读取外部文件

    <!-- 打开基于注解的配置 --> 
    <context:annotation-config/> 
     
    <!-- 开启组件自动扫描 -->
    <context:component-scan base-package="com.icon.packSample"/>  
    <!-- 读取外部属性文件 -->
    <context:property-placeholder location="classpath:dataSource.properties"/>

(3)dataSource配置

    常用的连接池技术
    (i)apache的数据源 org.apache.commons.dbcp.BasicDataSource        
    需要引入这些jar包commons-dbcp-1.4.jar  commons-pool-1.6.jar
    (ii)阿里巴巴的数据源 com.alibaba.druid.pool.DruidDataSource
    (iii)c3p0 com.mchange.v2.c3p0.ComboPooledDataSource
    (iv)Proxool
 <!-- 配置数据源 -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
        init-method="init" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>      
         
         <!-- ${username},${url},${password},${driverClassName}表示占位符,在外部文件中赋值,
         我们在ClassPath目录下创建dataSource.properties对这些变量赋值 -->
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${username}"/>
        <property name="url" value="${url}"/>
        <property name="password" value="${password}"/>
        <property name="driverClassName" value="${driverClassName}"/>
         
    </bean>

(4)sessionFactory配置

<!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model.model"/>
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">${hibernate.dialect}</prop>
              <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
              <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
              <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

(5)配置事务管理器

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

(6)

初始化hibernateTemplate
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

(7)配置事务管理      

    <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>
<!-- 开启事务注解
    <tx:annotation-driven transaction-manager="transactionManager"/>
     -->

附:dataSource.properties

db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/album
db.driverClassName=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true

到此就将完整的SSH环境搭建好了。

本文出自 “爪哇岛上学Java” 博客,请务必保留此出处http://zhangyh8856643.blog.51cto.com/8846643/1840683

SSH(Spring+Struts2+Hibernate)整合

标签:hibernate   spring   st   mysql   

原文地址:http://zhangyh8856643.blog.51cto.com/8846643/1840683

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