Spring整合Hibernate4,Struts2(ssh)01

Spring4 整合 Hibernate4
Spring4 接管 Hibernate4 所有 Bean 实例,以及 SessionFactory,事务管理器; 泛型注入;

Spring4 整合 Struts2

Spring4 接管 Struts2 所有 Bean 实例;

需要整合的jar包

数据库驱动包

1ecd13dc26ec1c486406cc09e9684303ef4.jpg

Struts2.3.16

a0efa080b566c8dd133eb4894452cfbad4c.jpg

Spring4.0.6

be191c3b3e00bade1724d8ed4b8e2aed7d1.jpg

Hibernate4.3.5

 

bb4e1f8ae3e3d26a79f3a01ede26e932664.jpg

一、创建web项目,选择2.5,因为3.0没有web.xml文件

 

560773fbe40c02311b4a7a1f9614112d080.jpg

然后将以上那些jar复制到lib下

39bf331f9a5aad9161cf7e647c119ff255d.jpg

二、然后配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSH</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 添加对spring的支持 -->  
  <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:applicationContext.xml</param-value>  
  </context-param>  
    
    <!-- 定义Spring监听器,加载Spring  -->
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
      
  <!-- 添加对struts2的支持 -->  
  <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>  
  
  <!-- Session延迟加载到页面  --> 
  <!-- 没有这个的话,Hibernate后台取数据的时候,Hibernate对象关联了,
       Session被关闭了,页面是取不到数据的加了openSessionInViewFilter
       Session就能延迟到页面
   -->
   <filter>  
    <filter-name>openSessionInViewFilter</filter-name>  
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    <init-param>  
      <param-name>singleSession</param-name>  
      <param-value>true</param-value>  
    </init-param>  
  </filter>  
    
   <filter-mapping>  
    <filter-name>openSessionInViewFilter</filter-name>  
    <url-pattern>*.action</url-pattern>  
  </filter-mapping>  
  
</web-app>

三、配置Struts2(在src文件夹下)

4b8ee4ae016005bc74801c50e0fbd43d081.jpg

<?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>  
    <!-- 后缀,用action -->
    <constant name="struts.action.extension" value="action" />  
      
   
      
</struts>   

四、配置hibernate.cfg.xml(在src文件夹下)

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>  
    <session-factory>  
		
		<!--方言-->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  

        <!-- 显示sql语句 -->  
        <property name="show_sql">true</property>  
        
		<!-- 自动更新 -->
        <property name="hbm2ddl.auto">update</property>
  
  
    </session-factory>  
</hibernate-configuration>

五、配置applicationContext.xml(在src文件夹下)

<?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:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
  
    <!-- 定义数据源,使用hibeinate时是由hibernate.cfg.xml定义,现在是由Spring定义 -->
    <!-- 要创建数据库test -->
    <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/test">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
	
      
    <!-- session工厂,注入数据源和hibernate.cfg.xml文件 -->  
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource">  
            <ref bean="dataSource" />  
        </property>  
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>  
        <!-- 自动扫描注解方式配置的hibernate类文件 -->  
        <property name="packagesToScan">  
            <list>  
                <value>com.java.entity</value>  
            </list>  
        </property>  
    </bean>  
  
    <!-- 配置事务管理器 -->  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
  
    <!-- 配置事务通知属性 -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <!-- 定义事务传播属性 -->  
        <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>  
      
   
    <!-- 配置事务切面 -->  
    <aop:config>  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.java.service..*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    </aop:config>  
  
    <!-- 自动加载构建bean 
    	后面我们配置的类用注解的方式实现,不用手工配置
    -->  
    <context:component-scan base-package="com.java" />  
  
</beans>  

六、创建数据库test

b091fb6e71f862ac131e4527470c1e15ca1.jpg

七、包层次

d03ffbd7a10ecf07187dd52b793a98881b6.jpg

com.java.entity(实体)在sessionFactory配置好了

八、创建一个jsp文件看看ssh是否配置成功

c9fb278031e0e8f070996a660f962b24194.jpg

475a336e664d921c32be52eda17188fd6b7.jpg

 

转载于:https://my.oschina.net/u/3848699/blog/2245418

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值