配该步骤的例子一个:请参看:Struts2,Spring,Hibernate整合例子一个
1.建立web项目
2.在web.xml 配置 struts2,和 spring ,
<!-- 配置Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher(struts的过滤器)
</filter-class>
</filter>
<filter-mapping>(加载struts2)
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener(spring监听器)
</listener-class>
</listener>
<!-- 查找spring配置文件 -->
<context-param>(程序从此处开始加载.xml 文件,并执行)
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*.xml</param-value>
</context-param>
3.在src根目录下创建struts.xml文件
<include file="struts-default.xml"/>固定格式,用到此包
4.在src根目录 创建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="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="bytecode.use_reflection_optimizer">true</property>
<property name="max_fetch_depth">2</property>
<property name="default_batch_fetch_size">8</property>
<property name="jdbc.batch_size">20</property>
</session-factory>
</hibernate-configuration>
系统给的文件 只需要拷贝,都是一些数据库库的基本配置
5.在src根目录 拷贝struts.properties文件
struts.objectFactory = spring 该项是告诉系统 用spring来 控制hibernate和Action
struts.action.extension=action 告诉系统访问时以什么结尾。可以改为: do 例如:login.do ,此处为:login.action
6.在src根目录下 创建applicationContext.xml
配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/>
<property name="username" value ="test"/>
<property name="password" value ="test"/>
</bean>
创建sessionFactory 工厂
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
</property>
</bean>
考来用
7.创建 实体类
就是写一个bean 含有get/set 方法
8.在实体类所在的目录下,写一个 类名.hbm.xml文件,在配置文件中添加属性
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zx.test.model.User" table="LB_USER">
<id name="id" type="java.lang.Long">主键的类型为Long
<column name="ID"/>
<generator class="sequence">
<param name="sequence">SEQ_LB_USER</param>指定主键生成方式,必须在pl/sql创建主键生成方式
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20"/>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="2"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20"/>
</property>
所有属性的name 必须与 bean中的set后的名称一样
</class>
</hibernate-mapping>
9.创建一个 Dao 实现操作方法
10 创建 service 调用 Dao中的方法
11.创建 Action 将service的得到的user 加到Action中
12.在applicationContext.xml 中配置
将userDao注入到sessionFactory中
将userService 注入到UserDao中
将userAction 注入到 userService中
13.在struts.xml中 配置Action
<package name="user" extends="struts-default(固定与include对应)" namespace="/use(访问时路径)r">
<action name="userAdd(访问时路径)" class="userAction" method="userAdd">
<result name="success">/userShow.jsp</result>
</action>
</package>
此处配置了访问时的路径:/user/userAdd
14.将8中的配置文件 加入到 4 中
<mapping resource="com/zx/test/model/User.hbm.xml" />
引用配置文件
15.写jsp页面 表单提交到 Action
9.10.11:是 Action 调用 Service ,Service调用 Dao 也是分层的体现
Web.xml 是web工程的配置文件
ApplicationContext 是 spring的配置文件
Hibernate.hbm.xml 是hibernate的配置文件
建表是 注意创建主键时 name 为 表明
配该步骤的例子一个:请参看:Struts2,Spring,Hibernate整合例子一个
分享到:
相关推荐
轻量级J2EE企业应用实战--Struts+Spring+Hibernate整合开发笔记
使用Struts + Spring + Hibernate完成用户登陆笔记使用Struts + Spring + Hibernate完成用户登陆笔记
Struts+Spring+Hibernate整合笔记
struts+spring+hibernate配置笔记
使用Struts + Spring + Hibernate完成分页笔记使用Struts + Spring + Hibernate完成分页笔记使用Struts + Spring + Hibernate完成分页笔记
SSH笔记-整合Spring2、Struts2、Hibernate4的完整步骤
NULL 博文链接:https://gaojiewyh.iteye.com/blog/988190
Struts2_hibernate_spring学习笔记,郑大刘铭皓老师课堂笔记
JDK5.0 应用服务器:JBOSS4.0.4GA Spring:2.5 Hibernate:3.1 Struts:1.2 学习心德 作者:姚赪海
struts2+hibernate+spring三大框架知识点笔记
本文档是在整合struts2,spring4,hiernate4后所做的笔记以及怎么整合技巧
留言管理程序_Struts + Spring + Hibernate笔记留言管理程序_Struts + Spring + Hibernate笔记
Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记.doc
此资料集合Struts Spring Hibernate三大框架笔记文档 此文档文字清晰且重点突出
我自己在学习过程中做的电子笔记和一些要点的整理,希望发感兴趣的朋友下载,才一分,看在我辛辛苦苦整理的份上,不要嫌要下载分哦!
Struts+Spring+Hibernate整合笔记
非常详细的SSH笔记,Struts2笔记+Hibernate3.6笔记+Spring3笔记,word文档,内容非常详细,可以直接看我的笔记就能轻松学会框架
Struts+spring+hibernate学习笔记! - Struts原理与实践
java(1) 整合入门(spring,struts,hibernate的整合)资料(1) 中大软件工厂项目前培训资料(1) 完整笔记+源码(1) ssh(1) C#查询参数化例子(1) 学习笔记+完整源码(1) 介绍与深入(1) 学习笔记(不含整合)(1) C#(1) .CHM格式...
SSH开发笔记,特别适合入门者学习