Struts+Spring+Hibernate整合
这段笔记三两年前写的,一直因为一些琐事,或者搞忘记了,没有发。今天偶然翻出了它,就和大家一起分享下吧。
1.导入jar包
Struts的jar包:
Spring的jar包:
Hibernate的jar包:
注意:只保留一个高版本的common-logging.jar包,Struts的jar包中struts2-spring-plugin-2.1.8.jar是必须要导入的,因为其是整合Struts和Spring的一个关键性jar包
2.Hibernate和Spring的整合
建议先整合Spring和Hibernate。
2.1方法一 保留hibernate.cfg.xml文件
Hibernate的hibernate.cfg.xml代码不变,如下示例:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property> <property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">1</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property> <property name="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/wzm/bean/User.hbm.xml"/> </session-factory> </hibernate-configuration>
Spring的application.xml文件整合Hibernate代码如下:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
</beans>
2.2 方法二 废弃使用hibernate.cfg.xml文件
将数据库的配置放在Spring的配置文件application.xml文件中,代码如下:
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.wzm"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="user" value="admin"/>
<property name="password" value="admin"/>
<property name="maxPoolSize" value="20"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/wzm/bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>
</beans>
2.3 使用properties文件提供数据库接口
这里的接口并非Java中的接口,而是指提供可扩展的接入点,properties文件中的代码如下:
URL=jdbc:oracle:thin:@localhost:1521:orcl
DRIVER=oracle.jdbc.driver.OracleDriver
USERNAME=admin
PASSWORD=admin #URL=jdbc:jtds:sqlserver://localhost:1433/qhit02
#DRIVER=net.sourceforge.jtds.jdbc.Driver
#USERNAME=sa
#PASSWORD=sa1234
该文件中提供了Oracle11g和SQL Server两种数据库接口(接入点),甚至可以写入更多的数据库的信息,“#”是注销,即其后的代码不可用。
Spring的application.xml中的配置代码如下:
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.wzm"/>
<context:property-placeholder location="classpath:DBOption.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${DRIVER}"/>
<property name="jdbcUrl" value="${URL}"/>
<property name="user" value="${USERNAME}"/>
<property name="password" value="${PASSWORD}"/>
<property name="maxPoolSize" value="20"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/wzm/persistence/entiy/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>
</beans>
3.整合Struts和Spring
整合Struts和Spring时,只需在WebRoot\WEB-INF\web.xml文件中加入Struts的过滤器和Spring的监听器即可,代码如下:
<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> <!-- 配置监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
(注意:配置多个Spring Bean文件时,这样写classpath:applicationContext.xml,classpath:bean.xml)
版权声明:本文为博主原创文章,未经博主允许不得转载。
Struts+Spring+Hibernate整合的更多相关文章
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- Struts+Spring+Hibernate整合笔记一
OpenSessionInview: 1.如果当前方法没有事物环境,则调用完毕getHibernate以后.session关闭: 说明:1如果测试dao层,没有事物环境 2如果测试service层,但 ...
- Struts+Spring+Hibernate、MVC、HTML、JSP
javaWeb应用 JavaWeb使用的技术,比如SSH(Struts.Spring.Hibernate).MVC.HTML.JSP等等技术,利用这些技术开发的Web应用在政府项目中非常受欢迎. 先说 ...
- 用eclipse搭建SSH(struts+spring+hibernate)框架
声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...
- Struts2+Spring+Hibernate整合开发(Maven多模块搭建)
Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...
- SSH(struts+spring+hibernate)常用配置整理
SSH(struts+spring+hibernate)常用配置整理 web.xml配置 <?xml version="1.0" encoding="UTF-8&q ...
- MyEclipse下Spring+Hibernate整合
目前,SSH(Struts+Spring+Hibernate)是Web开发的一种常用框架组合,Struts实现了MVC,Hibernate实现了关系对象映射,Spring实现了基于Bean的配置管理. ...
- Struts+Spring+Hibernate项目的启动线程
在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等.要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个 ...
- Struts,spring,hibernate三大框架的面试
Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...
随机推荐
- 为什么既要有IP地址还要有MAC地址
在脑海中一直有个疑问,在网络上发送信息的时候为什么既要有IP地址还要有MAC地址,IP是唯一的,MAC地址也是唯一的,用一个难道不行么? 既然每个以太网设备在出厂时都有一个唯一的MAC地址了,那为什么 ...
- 机器学习The Learning Problem——coursera简要总结
1.人类及动物的学习模式:观察->学习->技能 机器学习的模式:data->ML(机器学习)->skill 2.那什么是skill:技能是某种表现方法的增进 eg:stac ...
- pyinstaller的使用方法 by 王大龙
---------------------------------------------------------------------------------------------------- ...
- 使用JS获取当前地理位置方法汇总(如用谷歌接口,会出再以上报错,必须申请密钥并设置接受服务器IP!!!)
RefererNotAllowedMapError 错误 加载 Google Maps JavaScript API 的当前 URL 尚未添加到允许的引用站点列表中.请在 Google API Con ...
- mongodb与SQL常见语句对照
inert into users value(3,5) db.users.insert({a:3,b:5}) select a,b from users db.users.find({}, { ...
- Linux操作系统-基本命令(一)
熟悉Linux命令基础 Linux系统的终端窗口 字符终端为用户提供了一个标准的命令行接口,在字符终端窗口中,会显示一个Shell提示符,通常为$. 用户可以在提示符后输入带有选项和参数的字符命令,并 ...
- 为Web页中的Table对象创建一个映射表
HTML对象中的TABLE是我们常用的网页元素,在DHTML编程中,我们可以通过它的rows和cells方法方便的访问表格对象里面的每一个单元格,而且表格对象(table)的每个单元行(tr)和每个单 ...
- OpenMP 简单的规约
▶ 简单的计算和规约 ● 第一种方法,将全局和的指针传入工作函数中进行加和,使用 critical 来控制临界区的访问 #include <stdio.h> #include <st ...
- "sc.exe create/delete" - Create or Delete Services
"sc.exe" can also be used to create and delete services. If you want to create a new servi ...
- c# 之Web.config
Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式), 它可以出现在应用程序的每一个目录 ...