struts-2.3+spring-4.0+hibernate-4.0整合项目

1.加入spring
  1)加入jar包
  2)配置web.xml applicationContext.xml (监听器)
  3)加入spring的配置文件:aop context tx bean

1.加入hibernate
  1.1建立持久化类,和其对应的xxx,hbm。xml文件,生产对应的数据表
  1.2spring 整合hibernate

1)加入jar包
  2)类路径下加入hibernate.cfg.xml文件.其中配置hibernate的基本配置
  3)建立持久化类和对应的xxx.hbm。xml
  4)和spring整合
    a)加入c3p0和mysql的驱动
    b)在spring的配置文件中配置:数据源,sessionFactory,声明式事务

  5)启动项目,会看到生产对应的数据表

3.加入struts2
  1)加入jar包,有重复的包,删除版本低的包
  2)在web。xml文件中配置Struts2的Filter
  3)加入配置文件Struts.xml
  4)整合Spring
    a)加入Struts2的Spring插件的jar包
    b)在Spring的配置文件中正常配置Action,并设置Scope为prototype
    c)在Struts2的配置文件中Action时,class属性指向该Action在IOC中的id

注:以下代码的首行仅仅起到标记注释作用,开发时请去掉!

<!-- 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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<!-- 配置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>

	<!-- 配置Struts2的Filter  -->
	<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>

</web-app>

  

<!-- applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="user" value="${jdbc.user}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    	<property name="driverClass" value="${jdbc.driverClass}"></property>
    	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    	<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"></property>
    	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    	<property name="mappingLocations" value="classpath:xby/ssh/entities/*.hbm.xml"></property>
    </bean>

    <!-- 配置spring声明式事务 -->
    <!-- 1.配置hibernate的事务管理器 -->
    <bean  id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 2.配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>
    		<tx:method name="get*" read-only="true"/>
    		<tx:method name="*"/>
    	</tx:attributes>
    </tx:advice>
    <!-- 3.配置事务切入点->再把事务属性和事务切入点关联起来  -->
    <aop:config>
    	<aop:pointcut expression="execution(* xby.ssh.service.*.*(..))" id="txPointcut"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

</beans>

  

<!-- applicationContext-beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="employeeDao" class="xby.ssh.dao.EmployeeDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="employeeService" class="xby.ssh.service.EmployeeService">
		<property name="employeeDao" ref="employeeDao"></property>
	</bean>

	<bean id="employeeAction" class="xby.ssh.actions.EmployeeAction"
		scope="prototype">
		<property name="employeeService" ref="employeeService"></property>
	</bean>

</beans>

  

<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- 该属性设置Struts2是否支持动态方法调用,该属性的默认值是true -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true" />

	<package name="default" namespace="/" extends="struts-default">

        <action name="emp-*" class="employeeAction"
         		method="{1}">
         		<result name="list">/WEB-INF/views/emp-list.jsp</result>
         </action>

    </package>

</struts>

  

<!-- hibernate.cfg.xml -->
<?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">
<hibernate-configuration>
	<session-factory>
	<!-- 配置基本熟属性 -->
	<!-- 数据库方言 -->
	<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect </property>
	<!-- 显示sql语句 -->
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<!--  生产数据表的策略-->
	<property name="hibernate.hbm2ddl.auto">update</property>
	<!--  二级缓存-->
	</session-factory>
</hibernate-configuration>
//db.propertites
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssh

jdbc.initPoolSize=5
jdbc.maxPoolSize=10

  

  以上是所有的jar包,完全版,经测试没有问题。有需要者,可私信我~

时间: 2024-08-29 16:44:07

struts-2.3+spring-4.0+hibernate-4.0整合项目的相关文章

Spring,JPA与Hibernate的最新整合范例视频

和大家分享我最新制作的视频课程:Spring,JPA与Hibernate的最新整合范例视频讲解(含源代码),视频教程观看网址: http://www.javathinker.net/lesson.jsp?type=3&number=15 在网页上:选择[课程下载]链接,就能下载源代码,本节课的源代码在chapter24目录下. 本视频的配套书籍是我写的<精通JPA与Hibernate:Java对象持久化技术详解>第3版,已经完稿.预计2020年出版. 原文地址:https://blog

springMVC3+apache CXF+spring security3+mybatis3(proxool)整合项目

整合出现很多问题,这里就不例举了,大家各自修炼吧,这里我只提供demo架包,可以在里面折腾.这里我说一下为什么会有这样的框架:我们项目要求是为子系统提供权限认证和管理(web service),同时对这些web service进行权限管理.所以demo中对security做了url和方法级的认证做了扩展,但没做具体实现. 1.web.xml <?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns

SpringMVC+Spring4.0+Hibernate 简单的整合

学习的初始 一个 Hello World. 1.搭建好环境 工欲善其事,必先利其 这是需要的jar 简单的说下 : standard.jar  这个jar包是我们在jsp中使用JSTL标签的时候用到的.你也可以使用SpringEL . servlet-api.jar  这是你在SpringMvc中 用到HttpServletRequest 等这些类. 导入的包都OK了,上传下载的包我没有导.现在就应该到配置文件了. 2.配置文件  2.1web.xml   项目使用了什么框架.看这个配置就知道了

SpringMvc(4.0)+Hibernate(4.0)

1.项目结构截图: 整个项目的项目结构就如图上所示,文档后面的代码demo里面的类就可以对应放入路径即可,项目的成败还有一个很重要的因素就是包的问题,调试包是个很痛苦的过程,这里我也将我这个项目lib里面的包上传到百度云里面供大家下载,希望大家能成功把自己的Demo项目搭建起来. 百度云分享下载链接: http://pan.baidu.com/s/1c0xOCNY 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"

重新学习之spring第四个程序,整合struts2+hibernate+spring

第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4) 第二步:编写web.xml 和struts.xml和applicationContext.xml和applicationContext-service.xml和application-actionContext.xml和applicationContext-dao.xml web.xml 1 <?xml version="1.0" encoding="UTF

Struts2+Hibernate+Spring(SSH)三大框架整合jar包

Struts2 + Spring3 + Hibernate3 框架整合 1. 每个框架使用 (开发环境搭建 )* 表现层框架 struts2 1) jar包导入: apps/struts2_blank.war 包含struts2 开发最基本的jar包 struts2-convention-plugin-2.3.7.jar用于struts使用注解 (如果不使用注解开发,无需导入) struts2-json-plugin-2.3.7.jar 用于struts2整合Ajax struts2-sprin

struts2,spring,hibernate三大框架整合

本文利用mvc三层架构来讲述S2SH三大框架整合的步骤: 1.建立好包结构,建立好的包结构如下: 2.导入相应的jar包 (1)struts2用到的jar包 (2)spring用到的jar包 (3)hibernate用到的jar包 (4)mysql驱动包,junit和struts2插件 (5)公共包,例如日志 commons-logging-1.1.3.jar log4j-1.2.17.jar slf4j-log4j12-1.5.0.jar 最后经过整理的jar包清单如下: antlr-2.7.

SSH(struts2+spring+hibernate)三大框架整合

SSH框架整合理论: 在SSH框架的JAVAweb项目的开发过程中,WEB层通常使用的是Struts2+jsp,service层使用的是javaBean,DAO层使用的是hibernate,而spring的使用覆盖三层. 使用了spring框架之后,我们可以把对象交给spring来管理.在WEB层中,所有action对象的创建和管理都可以交给spring来完成,这样Struts2就不用自己来new一个action,这一切都可以交给spring,直接向spring来要action对象. 在DAO层

flex3+struts 1.3+spring+ibatis 2.x整合代码实例

原创整理不易,转载请注明出处:flex3+struts 1.3+spring+ibatis 2.x整合代码实例 代码下载地址:http://www.zuidaima.com/share/1778657261997056.htm 经过两天工作闲余时间的奋战,终于flex3+struts 1.3+spring +ibatis 2.x 整合成功,下面介绍下详细的步骤和核心代码: IDE:myeclipse (当然:前提是FLEX+java整合成功的情况下,关于flex+java整合的文章就比较多,go

整合Spring框架和Hibernate框架

-------------------siwuxie095 整合 Spring 框架和 Hibernate 框架 1.导入相关 jar 包(共 28 个) (1)导入 Spring 的核心 jar 包和日志相关的 jar 包(6 个) Commons Logging 下载链接: http://commons.apache.org/proper/commons-logging/download_logging.cgi LOG4J 下载链接: https://www.apache.org/dist/