设为首页 收藏本站
查看: 497|回复: 0

[经验分享] SSH(Spring+Struts2+Hibernate)整合

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-22 09:44:42 | 显示全部楼层 |阅读模式
一般整合需要以下几个步骤:
1、首先导入相应的jar包
  Spring所需的jar包如下图:
wKiom1e4XVuCzjZ9AAA_8KhJWmM066.jpg   
  Struts所需的jar包如下图:

wKioL1e4XY-ANgTUAAA-AbNJedE939.jpg
   hibernate所需的jar包如下图:

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

wKiom1e4XcuTbM4kAAApu7KykDg609.jpg
其中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命名空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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/s ... ing-context-3.0.xsd
        http://www.springframework.org/schema/jee



(2)注解配置以及读取外部文件
?
1
2
3
4
5
6
7
    <!-- 打开基于注解的配置 -->
    <context:annotation-config/>

    <!-- 开启组件自动扫描 -->
    <context:component-scan base-package="com.icon.packSample"/>  
    <!-- 读取外部属性文件 -->
    <context:property-placeholder location="classpath:dataSource.properties"/>



(3)dataSource配置
1
2
3
4
5
6
    常用的连接池技术
    (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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!-- 配置数据源 -->

    <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配置
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 配置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)配置事务管理器
1
2
3
4
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>



(6)
1
2
3
4
初始化hibernateTemplate
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>



(7)配置事务管理      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    <!-- 定义切面 -->
    <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
1
2
3
4
5
6
7
8
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环境搭建好了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-261204-1-1.html 上篇帖子: varnish构建高速缓存 下篇帖子: createrepo安装yum和源码包安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表